Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. package myDrawingSoftware;
  2.  
  3. import graphicalObjects.Shapes;
  4.  
  5. import java.awt.Color;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutputStream;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. import javax.swing.JFileChooser;
  18.  
  19. public class Memory {
  20.  
  21. static ArrayList<Shapes> myShapes = new ArrayList<Shapes>();
  22. private static int shapeSelector = 1;
  23. private static Color colorSelector = Color.RED;
  24.  
  25. public Memory() {
  26. }
  27.  
  28. public static int getShape(){
  29. return shapeSelector;
  30. }
  31.  
  32. public static void setShape(int choice){
  33. shapeSelector = choice;
  34. }
  35.  
  36. public void setColor(Color choice){
  37. this.colorSelector = choice;
  38. }
  39.  
  40. public Color getColor(){
  41. return colorSelector;
  42. }
  43.  
  44. public ArrayList<Shapes> getArrayList(){
  45. return myShapes;
  46. }
  47.  
  48. public void saveToFile(){
  49. JFileChooser chooser = new JFileChooser();
  50. chooser.showSaveDialog(null);
  51.  
  52. File f = chooser.getSelectedFile();
  53. FileOutputStream fos = null;
  54. try {
  55. fos = new FileOutputStream(f);
  56. } catch (FileNotFoundException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. }
  60. ObjectOutputStream oos = null;
  61. try {
  62. oos = new ObjectOutputStream(fos);
  63. } catch (IOException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. }
  67. try {
  68. oos.writeObject(myShapes);
  69. } catch (IOException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73. try {
  74. oos.close();
  75. } catch (IOException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }
  79. }
  80.  
  81. public void openFromFile(File file){
  82. FileInputStream fis = null;
  83. try {
  84. fis = new FileInputStream(file);
  85. } catch (FileNotFoundException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. ObjectInputStream ois = null;
  90. try {
  91. ois = new ObjectInputStream(fis);
  92. } catch (IOException e) {
  93. // TODO Auto-generated catch block
  94. e.printStackTrace();
  95. }
  96. try {
  97. myShapes = (ArrayList<Shapes>) ois.readObject();
  98. } catch (ClassNotFoundException e) {
  99. // TODO Auto-generated catch block
  100. e.printStackTrace();
  101. } catch (IOException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. try {
  106. ois.close();
  107. } catch (IOException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. }
  111. }
  112.  
  113. public void chooseFile() {
  114.  
  115. JFileChooser chooser = new JFileChooser();
  116. chooser.showSaveDialog(null);
  117. File f = chooser.getSelectedFile();
  118. if (f != null){
  119. openFromFile(f);
  120. }
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128. package myDrawingSoftware;
  129. import java.awt.BorderLayout;
  130. import java.awt.Color;
  131. import java.awt.Dimension;
  132. import java.awt.event.ActionEvent;
  133. import java.awt.event.ActionListener;
  134. import java.util.ArrayList;
  135. import javax.swing.BorderFactory;
  136. import javax.swing.BoxLayout;
  137. import javax.swing.JButton;
  138. import javax.swing.JLabel;
  139. import javax.swing.JPanel;
  140.  
  141. public class MyPanel extends JPanel {
  142.  
  143. public MyPanel(){
  144.  
  145. final Memory memory = new Memory();
  146.  
  147. setLayout(new BorderLayout());
  148.  
  149. final ArrayList<JButton> colorButtonList = new ArrayList();
  150. final ArrayList<JButton> shapeButtonList = new ArrayList();
  151.  
  152. // skapa högra panelen
  153. JPanel eastPanel = new JPanel();
  154. eastPanel.setPreferredSize(new Dimension(60, 1000));
  155. eastPanel.setBackground(Color.LIGHT_GRAY);
  156. eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
  157.  
  158. eastPanel.add(new JLabel(" Color"));
  159. final JButton blue = new JButton(" ");
  160. blue.setBackground(Color.BLUE);
  161. blue.setPreferredSize(new Dimension(60,30));
  162. blue.addActionListener(new ActionListener() {
  163. public void actionPerformed(ActionEvent e) {
  164. memory.setColor(Color.BLUE);
  165. for (JButton button : colorButtonList){
  166. button.setBorder(BorderFactory.createEmptyBorder());
  167. }
  168. blue.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  169. }
  170. });
  171. colorButtonList.add(blue);
  172. eastPanel.add(blue);
  173.  
  174. final JButton red = new JButton(" ");
  175. red.setBackground(Color.RED);
  176. red.setPreferredSize(new Dimension(60,30));
  177. red.addActionListener(new ActionListener() {
  178. public void actionPerformed(ActionEvent e) {
  179. memory.setColor(Color.RED);
  180. for (JButton button : colorButtonList){
  181. button.setBorder(BorderFactory.createEmptyBorder());
  182. }
  183. red.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  184. }
  185. });
  186. colorButtonList.add(red);
  187. eastPanel.add(red);
  188.  
  189. final JButton yellow = new JButton(" ");
  190. yellow.setBackground(Color.YELLOW);
  191. yellow.setPreferredSize(new Dimension(60,30));
  192. yellow.addActionListener(new ActionListener() {
  193. public void actionPerformed(ActionEvent e) {
  194. memory.setColor(Color.YELLOW);
  195. for (JButton button : colorButtonList){
  196. button.setBorder(BorderFactory.createEmptyBorder());
  197. }
  198. yellow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  199. }
  200. });
  201. colorButtonList.add(yellow);
  202. eastPanel.add(yellow);
  203.  
  204. final JButton black = new JButton(" ");
  205. black.setBackground(Color.BLACK);
  206. black.setForeground(Color.WHITE);
  207. black.setPreferredSize(new Dimension(60,30));
  208. black.addActionListener(new ActionListener() {
  209. public void actionPerformed(ActionEvent e) {
  210. memory.setColor(Color.BLACK);
  211. for (JButton button : colorButtonList){
  212. button.setBorder(BorderFactory.createEmptyBorder());
  213. }
  214. black.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
  215. }
  216. });
  217. colorButtonList.add(black);
  218. eastPanel.add(black);
  219.  
  220. final JButton green = new JButton(" ");
  221. green.setBackground(Color.GREEN);
  222. green.setPreferredSize(new Dimension(60,30));
  223. green.addActionListener(new ActionListener() {
  224. public void actionPerformed(ActionEvent e) {
  225. memory.setColor(Color.GREEN);
  226. for (JButton button : colorButtonList){
  227. button.setBorder(BorderFactory.createEmptyBorder());
  228. }
  229. green.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  230. }
  231. });
  232. colorButtonList.add(green);
  233. eastPanel.add(green);
  234.  
  235. final JButton orange = new JButton(" ");
  236. orange.setBackground(Color.ORANGE);
  237. orange.setPreferredSize(new Dimension(60,30));
  238. orange.addActionListener(new ActionListener() {
  239. public void actionPerformed(ActionEvent e) {
  240. memory.setColor(Color.ORANGE);
  241. for (JButton button : colorButtonList){
  242. button.setBorder(BorderFactory.createEmptyBorder());
  243. }
  244. orange.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  245. }
  246. });
  247. colorButtonList.add(orange);
  248. eastPanel.add(orange);
  249.  
  250. final JButton gray = new JButton(" ");
  251. gray.setBackground(Color.GRAY);
  252. gray.setPreferredSize(new Dimension(60,30));
  253. gray.addActionListener(new ActionListener() {
  254. public void actionPerformed(ActionEvent e) {
  255. memory.setColor(Color.GRAY);
  256. for (JButton button : colorButtonList){
  257. button.setBorder(BorderFactory.createEmptyBorder());
  258. }
  259. gray.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  260. }
  261. });
  262. colorButtonList.add(gray);
  263. eastPanel.add(gray);
  264.  
  265. final JButton cyan = new JButton(" ");
  266. cyan.setBackground(Color.CYAN);
  267. //cyan.setPreferredSize(new Dimension(60,30));
  268. cyan.addActionListener(new ActionListener() {
  269. public void actionPerformed(ActionEvent e) {
  270. memory.setColor(Color.CYAN);
  271. for (JButton button : colorButtonList){
  272. button.setBorder(BorderFactory.createEmptyBorder());
  273. }
  274. cyan.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  275. }
  276. });
  277. colorButtonList.add(cyan);
  278. eastPanel.add(cyan);
  279.  
  280. eastPanel.add(new JLabel("Shapes"));
  281.  
  282. final JButton circle = new JButton("Circle");
  283. circle.addActionListener(new ActionListener() {
  284. public void actionPerformed(ActionEvent e) {
  285. memory.setShape(1);
  286. for (JButton button : shapeButtonList){
  287. button.setBorder(BorderFactory.createEmptyBorder());
  288. }
  289. circle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  290. }
  291. });
  292. shapeButtonList.add(circle);
  293. eastPanel.add(circle);
  294.  
  295. final JButton square = new JButton("Square");
  296. square.addActionListener(new ActionListener() {
  297. public void actionPerformed(ActionEvent e) {
  298. memory.setShape(2);
  299. for (JButton button : shapeButtonList){
  300. button.setBorder(BorderFactory.createEmptyBorder());
  301. }
  302. square.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  303. }
  304. });
  305. shapeButtonList.add(square);
  306. eastPanel.add(square);
  307.  
  308. final JButton triangle = new JButton("Triangle");
  309. triangle.addActionListener(new ActionListener() {
  310. public void actionPerformed(ActionEvent e) {
  311. memory.setShape(3);
  312. for (JButton button : shapeButtonList){
  313. button.setBorder(BorderFactory.createEmptyBorder());
  314. }
  315. triangle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  316. }
  317. });
  318. shapeButtonList.add(triangle);
  319. eastPanel.add(triangle);
  320.  
  321. for (JButton button : colorButtonList){
  322. button.setBorder(BorderFactory.createEmptyBorder());
  323. }
  324. for (JButton button : shapeButtonList){
  325. button.setBorder(BorderFactory.createEmptyBorder());
  326. }
  327. blue.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  328. circle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  329.  
  330. //skapa nedre panelen
  331. JPanel southPanel = new JPanel();
  332. southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
  333. southPanel.setBackground(Color.LIGHT_GRAY);
  334.  
  335. eastPanel.setBackground(Color.LIGHT_GRAY);
  336.  
  337.  
  338. //lägg till allt i fönstret och skapa det
  339. this.add(new PaintingArea());
  340. this.add(eastPanel, BorderLayout.EAST);
  341. this.add(southPanel, BorderLayout.SOUTH);
  342. //this.add(text, BorderLayout.CENTER);etBackground(Color.LIGHT_GRAY);
  343.  
  344. JButton reset = new JButton("Reset");
  345. reset.addActionListener(new ActionListener() {
  346. public void actionPerformed(ActionEvent e) {
  347. memory.getArrayList().clear();
  348. }
  349. });
  350. southPanel.add(reset);
  351.  
  352. JButton save = new JButton("Save");
  353. save.addActionListener(new ActionListener() {
  354. public void actionPerformed(ActionEvent e) {
  355. memory.saveToFile();
  356. }
  357. });
  358. southPanel.add(save);
  359.  
  360. JButton open = new JButton("Open");
  361. open.addActionListener(new ActionListener() {
  362. public void actionPerformed(ActionEvent e) {
  363. memory.chooseFile();
  364. }
  365. });
  366. southPanel.add(open);
  367. }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement