This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawFrame.java
142 lines (124 loc) · 3.83 KB
/
DrawFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Jaired Jawed
* Dec 10, 2017
* DrawFrame.java
*/
import javax.swing.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
public class DrawFrame extends JFrame
{
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private String[] fileOptions = {"Undo", "Redo", "Clear", "Export To", "Import From"};
private JMenu shapeMenu = new JMenu("Shapes");
private String[] shapeOptions = {"Line", "Rectangle", "Oval"};
private JMenu methodMenu = new JMenu("Method");
private String[] methodOptions = {"Fill", "Draw"};
private JPanel colorPanel = new JPanel();
private String[] colors = {"Black", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow"};
DrawPanel panel = new DrawPanel();
public DrawFrame()
{
super("Jaired's Paint Program");
setJMenuBar(menuBar);
colorPanel.setLayout(new GridLayout( 1, 6, 10, 10 ));
add(colorPanel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
addButtonsToJPanel(colorPanel, colors);
addMenuItemsToJMenu(fileMenu, fileOptions);
addMenuItemsToJMenu(shapeMenu, shapeOptions);
addMenuItemsToJMenu(methodMenu, methodOptions);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void addMenuItemsToJMenu(JMenu menu, String[] arr) {
MenuHandler handler = new MenuHandler();
for (int i = 0; i < arr.length; i++) {
JMenuItem menuItem = new JMenuItem(arr[i]);
menu.add(menuItem);
menuItem.addActionListener(handler);
}
menuBar.add(menu);
}
public void addButtonsToJPanel(JPanel panel, String[] arr) {
ColorHandler handler = new ColorHandler();
for (int i = 0; i < arr.length; i++) {
JButton button = new JButton(arr[i]);
button.addActionListener(handler);
colorPanel.add(button);
}
}
private class ColorHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
switch (actionCommand) {
case "Black":
panel.setCurrentShapeColor(Color.BLACK);
break;
case "Red":
panel.setCurrentShapeColor(Color.RED);
break;
case "Green":
panel.setCurrentShapeColor(Color.GREEN);
break;
case "Blue":
panel.setCurrentShapeColor(Color.BLUE);
break;
case "Cyan":
panel.setCurrentShapeColor(Color.CYAN);
break;
case "Magenta":
panel.setCurrentShapeColor(Color.MAGENTA);
break;
case "Yellow":
panel.setCurrentShapeColor(Color.YELLOW);
break;
}
}
}
private class MenuHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
switch (actionCommand) {
case "Undo":
panel.clearLastShape();
break;
case "Redo":
panel.redoLastShape();
break;
case "Clear":
panel.clearDrawing();
break;
case "Export To":
panel.exportImageFromJPanel();
break;
case "Import From":
panel.importImageToJPanel();
break;
case "Line":
panel.setCurrentShapeType("Line");
break;
case "Rectangle":
panel.setCurrentShapeType("Rectangle");
break;
case "Oval":
panel.setCurrentShapeType("Oval");
break;
case "Fill":
panel.setCurrentShapeFilled(true);
break;
case "Draw":
panel.setCurrentShapeFilled(false);
break;
}
}
}
}