-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjbc1.java
188 lines (163 loc) · 5.62 KB
/
jbc1.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package jbc;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JTextField;
public class jbc1 extends JPanel {
private static final long serialVersionUID = 1L;
private int []cx;
private int []cy;
private int cw = 50;
private int ch = 50;
private int xinc = 1;
private int yinc = 1;
private int count=0;
private int n=0;
Timer t;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
jbc1 panel = new jbc1();
panel.setPreferredSize(new Dimension(900, 700));
// panel.animate();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public jbc1() {
setLayout(null);
setBackground(Color.BLACK);
setForeground(Color.RED);
setOpaque(true);
t=new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
Rectangle oldCircle = new Rectangle(cx[i] - 1, cy[j] - 1, cw + 2, ch + 2);
cx[i] += xinc;
// cy += yinc;
if (cx[i] >= getWidth() - cw || cx[i] <= 0) {
xinc *= -1;
}
if (cy[j] >= getHeight() - ch || cy[j] <= 0) {
yinc *= -1;
}
repaint(oldCircle);
repaint(cx[i] - 1, cy[i] - 1, cw + 2, ch + 2);
}}
}
});
JButton btnClickMe = new JButton("|>");
btnClickMe.setBounds(105, 341, 45, 36);
btnClickMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
count++;
if(count%2==0)
t.stop();
else
t.start();
}
});
add(btnClickMe);
textField = new JTextField();
textField.setBounds(310, 349, 86, 20);
add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Submit");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s=new String();
s=textField.getText();
t.stop();
if(s.length()!=0)
{
repaint();
count=0;
n=Integer.parseInt(s);
cx=new int[n];
cy=new int[n];
cx[0]=0;
cy[0]=0;
for(int i=1;i<n;i++)
{
cx[i]=cx[i-1]+100;
cy[i]=cy[i-1]+100;
}
//Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2);
//repaint(oldCircle);
}
}
});
btnNewButton.setBounds(313, 392, 86, 23);
add(btnNewButton);
}
/*public void animate() {
new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2);
cx += xinc;
cy += yinc;
if (cx >= getWidth() - cw || cx <= 0) {
xinc *= -1;
}
if (cy >= getHeight() - ch || cy <= 0) {
yinc *= -1;
}
repaint(oldCircle);
repaint(cx - 1, cy - 1, cw + 2, ch + 2);
}
}).start();
}*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
//g.drawOval(cx[i], cy[j], cw, ch);
/* FontMetrics fm = g.getFontMetrics();
double textWidth = fm.getStringBounds("1", g).getWidth();
g.setColor(Color.WHITE);
g.drawString("1",cx[i],cy[j]
);
// g.drawString("1",cx[i],cy[j]-5);
*/
String text = "1";
//int centerX = 150, centerY = 100;
//int ovalWidth = 200, ovalHeight = 100;
// Draw oval
g.setColor(Color.BLUE);
g.fillOval(cx[i], cy[j],
cw, ch);
// Draw centered text
FontMetrics fm = g.getFontMetrics();
double textWidth = fm.getStringBounds(text, g).getWidth();
g.setColor(Color.WHITE);
g.drawString(text, (int) (cx[i]+cw/2),
(int) (cy[j] +ch/2));
}
}
}