Cook book written in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
8.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package ksiazka.kucharska;
  2. import java.awt.BorderLayout;
  3. import javax.imageio.ImageIO;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFileChooser;
  9. import javax.swing.JSplitPane;
  10. import javax.swing.JButton;
  11. import javax.swing.JTextField;
  12. import java.awt.Graphics2D;
  13. import java.awt.GridLayout;
  14. import java.awt.GridBagLayout;
  15. import java.awt.GridBagConstraints;
  16. import java.awt.Image;
  17. import java.awt.Insets;
  18. import java.awt.RenderingHints;
  19. import javax.swing.JList;
  20. import javax.swing.JTextPane;
  21. import javax.swing.JLabel;
  22. import java.awt.Font;
  23. import javax.swing.border.LineBorder;
  24. import java.awt.SystemColor;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.image.BufferedImage;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import javax.swing.SwingConstants;
  31. public class FrmMain extends JFrame {
  32. /**
  33. *
  34. */
  35. private static final long serialVersionUID = 2634383304749703669L;
  36. private JPanel contentPane;
  37. private JTextField txtSearch;
  38. private JTextField txtEdTitle;
  39. private JTextField txtTags;
  40. private JButton btnSave;
  41. private JButton btnCancel;
  42. private JTextPane txtIngredients;
  43. private JTextPane txtDescription;
  44. private JButton btnAddRecipe;
  45. private JButton btnSearch;
  46. private JButton btnDelRecipe;
  47. private JButton btnLoadImg;
  48. private JLabel lblRecipeTitle;
  49. private JLabel imgRecipe;
  50. private JFileChooser fcImg;
  51. private ImageIcon imgDefaultRecipe;
  52. void DeactivateAddMenu() {
  53. btnCancel.setVisible(false);
  54. btnDelRecipe.setEnabled(true);
  55. btnLoadImg.setVisible(false);
  56. btnSearch.setEnabled(true);
  57. btnSave.setVisible(false);
  58. txtDescription.setEditable(false);
  59. txtEdTitle.setVisible(false);
  60. txtIngredients.setEditable(false);
  61. txtTags.setEditable(false);
  62. lblRecipeTitle.setVisible(true);
  63. }
  64. void ActivateAddMenu() {
  65. btnCancel.setVisible(true);
  66. btnDelRecipe.setEnabled(false);
  67. btnLoadImg.setVisible(true);
  68. btnSearch.setEnabled(false);
  69. btnSave.setVisible(true);
  70. txtDescription.setEditable(true);
  71. txtEdTitle.setVisible(true);
  72. txtIngredients.setEditable(true);
  73. txtTags.setEditable(true);
  74. lblRecipeTitle.setVisible(false);
  75. //czyszczenie pól formularza
  76. txtDescription.setText("");
  77. txtEdTitle.setText("");
  78. txtIngredients.setText("");
  79. txtTags.setText("");
  80. }
  81. /**
  82. * Create the frame.
  83. */
  84. public FrmMain() {
  85. setResizable(false);
  86. setTitle("Książka Kucharska v1.0");
  87. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88. setBounds(100, 100, 800, 549);
  89. contentPane = new JPanel();
  90. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  91. contentPane.setLayout(new BorderLayout(0, 0));
  92. setContentPane(contentPane);
  93. JSplitPane splitPane = new JSplitPane();
  94. splitPane.setDividerSize(5);
  95. splitPane.setEnabled(false);
  96. splitPane.setResizeWeight(0.1);
  97. contentPane.add(splitPane, BorderLayout.CENTER);
  98. JPanel panel = new JPanel();
  99. splitPane.setLeftComponent(panel);
  100. GridBagLayout gbl_panel = new GridBagLayout();
  101. gbl_panel.columnWidths = new int[] {0, 30};
  102. gbl_panel.rowHeights = new int[] {0, 0, 0};
  103. gbl_panel.columnWeights = new double[]{1.0, 0.0};
  104. gbl_panel.rowWeights = new double[]{0.0, 1.0, 0.0};
  105. panel.setLayout(gbl_panel);
  106. txtSearch = new JTextField();
  107. txtSearch.setText("wpisz tytuł/tagi...");
  108. GridBagConstraints gbc_txtSearch = new GridBagConstraints();
  109. gbc_txtSearch.insets = new Insets(0, 0, 5, 5);
  110. gbc_txtSearch.fill = GridBagConstraints.HORIZONTAL;
  111. gbc_txtSearch.gridx = 0;
  112. gbc_txtSearch.gridy = 0;
  113. panel.add(txtSearch, gbc_txtSearch);
  114. txtSearch.setColumns(10);
  115. btnSearch = new JButton("Szukaj");
  116. GridBagConstraints gbc_btnSearch = new GridBagConstraints();
  117. gbc_btnSearch.anchor = GridBagConstraints.NORTH;
  118. gbc_btnSearch.fill = GridBagConstraints.HORIZONTAL;
  119. gbc_btnSearch.insets = new Insets(0, 0, 5, 0);
  120. gbc_btnSearch.gridx = 1;
  121. gbc_btnSearch.gridy = 0;
  122. panel.add(btnSearch, gbc_btnSearch);
  123. JList recipesList = new JList();
  124. GridBagConstraints gbc_recipesList = new GridBagConstraints();
  125. gbc_recipesList.insets = new Insets(0, 0, 5, 0);
  126. gbc_recipesList.gridwidth = 2;
  127. gbc_recipesList.fill = GridBagConstraints.BOTH;
  128. gbc_recipesList.gridx = 0;
  129. gbc_recipesList.gridy = 1;
  130. panel.add(recipesList, gbc_recipesList);
  131. JPanel panel_1 = new JPanel();
  132. GridBagConstraints gbc_panel_1 = new GridBagConstraints();
  133. gbc_panel_1.gridwidth = 2;
  134. gbc_panel_1.gridheight = 0;
  135. gbc_panel_1.insets = new Insets(0, 0, 0, 5);
  136. gbc_panel_1.fill = GridBagConstraints.BOTH;
  137. gbc_panel_1.gridx = 0;
  138. gbc_panel_1.gridy = 2;
  139. panel.add(panel_1, gbc_panel_1);
  140. panel_1.setLayout(new GridLayout(0, 2, 0, 0));
  141. btnAddRecipe = new JButton("Dodaj");
  142. btnAddRecipe.addActionListener(new ActionListener() {
  143. public void actionPerformed(ActionEvent e) {
  144. ActivateAddMenu();
  145. }
  146. });
  147. panel_1.add(btnAddRecipe);
  148. btnDelRecipe = new JButton("Usuń");
  149. panel_1.add(btnDelRecipe);
  150. JPanel panel_2 = new JPanel();
  151. splitPane.setRightComponent(panel_2);
  152. panel_2.setLayout(null);
  153. btnSave = new JButton("Zapisz");
  154. btnSave.addActionListener(new ActionListener() {
  155. public void actionPerformed(ActionEvent e) {
  156. System.out.println("sprawdzam ikonę...");
  157. if (imgRecipe.getIcon() == imgDefaultRecipe.getImage())
  158. System.out.println("Ta sama ikona");
  159. DeactivateAddMenu();
  160. }
  161. });
  162. btnSave.setVisible(false);
  163. btnSave.setBounds(319, 12, 117, 25);
  164. panel_2.add(btnSave);
  165. txtEdTitle = new JTextField();
  166. txtEdTitle.setVisible(false);
  167. txtEdTitle.setBounds(12, 15, 295, 19);
  168. panel_2.add(txtEdTitle);
  169. txtEdTitle.setColumns(10);
  170. txtIngredients = new JTextPane();
  171. txtIngredients.setEditable(false);
  172. txtIngredients.setBounds(26, 88, 228, 126);
  173. panel_2.add(txtIngredients);
  174. txtDescription = new JTextPane();
  175. txtDescription.setEditable(false);
  176. txtDescription.setBounds(26, 254, 410, 196);
  177. panel_2.add(txtDescription);
  178. lblRecipeTitle = new JLabel("Nazwa przepisu");
  179. lblRecipeTitle.setFont(new Font("Dialog", Font.BOLD, 18));
  180. lblRecipeTitle.setBounds(12, 12, 442, 22);
  181. panel_2.add(lblRecipeTitle);
  182. JLabel lblIngredients = new JLabel("Składniki:");
  183. lblIngredients.setBounds(26, 61, 70, 15);
  184. panel_2.add(lblIngredients);
  185. JLabel lblDescription = new JLabel("Opis wykonania:");
  186. lblDescription.setBounds(26, 227, 152, 15);
  187. panel_2.add(lblDescription);
  188. imgRecipe = new JLabel("");
  189. imgRecipe.setHorizontalAlignment(SwingConstants.CENTER);
  190. imgRecipe.setBorder(new LineBorder(SystemColor.textHighlightText));
  191. imgRecipe.setBounds(272, 88, 164, 126);
  192. panel_2.add(imgRecipe);
  193. btnLoadImg = new JButton("Wczytaj");
  194. fcImg = new JFileChooser();
  195. imgDefaultRecipe = new ImageIcon("no_img_recipe.jpg");
  196. imgRecipe.setIcon(imgDefaultRecipe);
  197. btnLoadImg.addActionListener(new ActionListener() {
  198. public void actionPerformed(ActionEvent arg0) {
  199. int returnVal = fcImg.showOpenDialog(FrmMain.this);
  200. if (returnVal == JFileChooser.APPROVE_OPTION) {
  201. File file = fcImg.getSelectedFile();
  202. try {
  203. BufferedImage srcImg = ImageIO.read(file);
  204. int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*imgRecipe.getHeight());
  205. System.out.println(w);
  206. BufferedImage resizedImg = new BufferedImage(w, imgRecipe.getHeight(), BufferedImage.TYPE_INT_ARGB);
  207. Graphics2D g2 = resizedImg.createGraphics();
  208. g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  209. g2.drawImage(srcImg, 0, 0, w, imgRecipe.getHeight(), null);
  210. g2.dispose();
  211. ImageIcon iconLogo = new ImageIcon(resizedImg);
  212. imgRecipe.setIcon(iconLogo);
  213. } catch (IOException e) {
  214. // TODO Auto-generated catch block
  215. e.printStackTrace();
  216. }
  217. }
  218. }
  219. });
  220. btnLoadImg.setVisible(false);
  221. btnLoadImg.setBounds(304, 217, 102, 25);
  222. panel_2.add(btnLoadImg);
  223. txtTags = new JTextField();
  224. txtTags.setEditable(false);
  225. txtTags.setBounds(26, 480, 410, 19);
  226. panel_2.add(txtTags);
  227. txtTags.setColumns(10);
  228. JLabel lblTags = new JLabel("Tagi:");
  229. lblTags.setBounds(26, 462, 70, 15);
  230. panel_2.add(lblTags);
  231. btnCancel = new JButton("Anuluj");
  232. btnCancel.addActionListener(new ActionListener() {
  233. public void actionPerformed(ActionEvent e) {
  234. DeactivateAddMenu();
  235. }
  236. });
  237. btnCancel.setVisible(false);
  238. btnCancel.setBounds(319, 46, 117, 25);
  239. panel_2.add(btnCancel);
  240. }
  241. }