-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamplez_point.java
102 lines (76 loc) · 2.34 KB
/
examplez_point.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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.util.Random;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComponent;
public class PointsEx extends JFrame {
public PointsEx() {
initUI();
}
private void initUI() {
DrawPanel drawPanel = new DrawPanel();
var quitButton = new JButton("Quit");
//add more button
var blankButton = new JButton("Hi");
var eggButton = new JButton("egg");
quitButton.addActionListener((event) -> System.exit(0));
//createLayout(quitButton, blankButton, eggButton);
add(drawPanel);
setSize(350, 250);
setTitle("Points");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
//add buttons
.addComponent(arg[1])
.addComponent(arg[2])
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
//add buttons
.addComponent(arg[1])
.addComponent(arg[2])
);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
PointsEx ex = new PointsEx();
ex.setVisible(true);
});
}
}
class DrawPanel extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
for (int i = 0; i <= 1000; i++) {
Dimension size = getSize();
Insets insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
Random r = new Random();
int x = Math.abs(r.nextInt()) % w;
int y = Math.abs(r.nextInt()) % h;
g2d.drawLine(x, y, x, y);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}