This repository was archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
112 lines (79 loc) · 2.3 KB
/
Main.java
File metadata and controls
112 lines (79 loc) · 2.3 KB
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main{
JFrame frame;
Color currentColor = Color.BLACK;
int xSize = 10;
int ySize = 10;
Main(){
frame = new JFrame();
frame.addMouseMotionListener(new MouseInput());
frame.setSize(750, 750);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
topPanel.add(new ColorChooserButton(currentColor, this));
topPanel.add(new JLabel("Size: "));
JTextField xSizeF = new JTextField(2);
xSizeF.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int newXS = 0;
try{
newXS = Integer.parseInt(xSizeF.getText());
}catch(Exception ex){
ex.printStackTrace();
return;
}
xSize = newXS;
}
});
topPanel.add(xSizeF);
JTextField ySizeF = new JTextField(2);
ySizeF.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int newYS = 0;
try{
newYS = Integer.parseInt(ySizeF.getText());
}catch(Exception ex){
ex.printStackTrace();
return;
}
ySize = newYS;
}
});
topPanel.add(ySizeF);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics g2 = frame.getGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 40, frame.getWidth(), frame.getHeight());
}
});
topPanel.add(clearButton);
frame.add(BorderLayout.NORTH, topPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class MouseInput implements MouseMotionListener{
public void mouseDragged(MouseEvent e){
Graphics g = frame.getGraphics();
g.setColor(currentColor);
g.fillOval(e.getX(), e.getY(), xSize, ySize);
}
public void mouseMoved(MouseEvent e){}
}
public static void main (String[]args){
new Main();
}
}