質問編集履歴
4
コードの変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -216,33 +216,7 @@
|
|
216
216
|
(int)(2*b2.c(r)), (int)(2*b2.c(r)));
|
217
217
|
}
|
218
218
|
|
219
|
-
|
219
|
+
|
220
|
-
|
221
|
-
|
222
|
-
BallPropagator.dt=0.1;
|
223
|
-
|
224
|
-
|
225
|
-
JFrame frame = new JFrame();
|
226
|
-
|
227
|
-
|
228
|
-
BallMain panel = new BallMain(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
|
229
|
-
|
230
|
-
|
231
|
-
BallPropagator.xmin = 0;
|
232
|
-
BallPropagator.xmax = panel.xPanelSize;
|
233
|
-
BallPropagator.ymin = 0;
|
234
|
-
BallPropagator.ymax = panel.yPanelSize;
|
235
|
-
|
236
|
-
|
237
|
-
frame.pack();
|
238
|
-
//
|
239
|
-
frame.setTitle("反射するボール");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setResizable(true);
|
240
|
-
frame.setVisible(true);
|
241
|
-
|
242
|
-
|
243
|
-
Timer timer = new Timer(10, panel);
|
244
|
-
timer.start();
|
245
|
-
}
|
246
220
|
}
|
247
221
|
```
|
248
222
|
```
|
3
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
画面に出てきているボタンを押すと出力するようにしたいです。
|
3
3
|
ここに質問の内容を詳しく書いてください。
|
4
4
|
現在、自学でjavaをやっています。今回、最終的にボタンを押すとボールがその方向に加速するというプログラムを作ろうと思っています。ですが途中でボタンを押しても反応しなくなりました。なぜなのかわからないので教えていただきたいです
|
5
|
+
Sample.javaでコンパイルするとボタンが反応しないです
|
6
|
+
|
5
7
|
■■な機能を実装中に以下のエラーメッセージが発生しました。
|
6
8
|
|
7
9
|
|
2
付属のソースコードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,6 +11,7 @@
|
|
11
11
|
java
|
12
12
|
ソースコード
|
13
13
|
```
|
14
|
+
//Sample.java
|
14
15
|
import java.awt.*;
|
15
16
|
import java.awt.event.*;
|
16
17
|
import javax.swing.*;
|
@@ -21,22 +22,22 @@
|
|
21
22
|
static int yPanelSize;
|
22
23
|
|
23
24
|
double x,y,r,vx,vy,ax,ay;
|
24
|
-
|
25
|
+
BallPropagator b1;
|
25
|
-
|
26
|
+
BallPropagator b2;
|
26
27
|
Color c1;
|
27
28
|
Color c2;
|
28
29
|
static JButton btn1 = new JButton("↑");
|
29
30
|
static JButton btn2 = new JButton("↓");
|
30
31
|
static JButton btn3 = new JButton("→");
|
31
32
|
static JButton btn4 = new JButton("←");
|
32
|
-
|
33
|
+
Sample(int xPanelSize, int yPanelSize){
|
33
34
|
|
34
35
|
|
35
36
|
this.xPanelSize = xPanelSize;
|
36
37
|
this.yPanelSize = yPanelSize;
|
37
38
|
|
38
|
-
b1 = new
|
39
|
+
b1 = new BallPropagator(10, 100, 100, 30, 40, 0, 9.8);
|
39
|
-
b2 = new
|
40
|
+
b2 = new BallPropagator(10, 10, 100, 50, 20, 0, 9.8); /////////////////////////////////////////////
|
40
41
|
c1=Color.black;
|
41
42
|
c2=Color.blue;
|
42
43
|
/////////////////////////////////////////////
|
@@ -113,13 +114,13 @@
|
|
113
114
|
public static void main(String[] args) {
|
114
115
|
|
115
116
|
|
116
|
-
|
117
|
+
BallPropagator.dt=0.1;
|
117
118
|
|
118
119
|
|
119
120
|
JFrame frame = new JFrame();
|
120
121
|
|
121
122
|
|
122
|
-
|
123
|
+
BallMain panel = new BallMain(500,400);
|
123
124
|
|
124
125
|
panel.setBackground(Color.white);
|
125
126
|
panel.setPreferredSize(new Dimension(xPanelSize,yPanelSize));
|
@@ -128,10 +129,10 @@
|
|
128
129
|
panel.add(btn3);
|
129
130
|
panel.add(btn4);
|
130
131
|
|
131
|
-
|
132
|
+
BallPropagator.xmin = 0;
|
132
|
-
|
133
|
+
BallPropagator.xmax = panel.xPanelSize;
|
133
|
-
|
134
|
+
BallPropagator.ymin = 0;
|
134
|
-
|
135
|
+
BallPropagator.ymax = panel.yPanelSize;
|
135
136
|
|
136
137
|
|
137
138
|
frame.add(panel);
|
@@ -150,29 +151,29 @@
|
|
150
151
|
}
|
151
152
|
```
|
152
153
|
```
|
153
|
-
|
154
|
+
//BallMain.java
|
154
155
|
import java.awt.*;
|
155
156
|
import java.awt.event.*;
|
156
157
|
import javax.swing.*;
|
157
158
|
|
158
|
-
public class
|
159
|
+
public class BallMain extends JPanel implements ActionListener{
|
159
160
|
|
160
161
|
static int xPanelSize;
|
161
162
|
static int yPanelSize;
|
162
163
|
|
163
164
|
double x,y,r,vx,vy,ax,ay;
|
164
|
-
|
165
|
+
BallPropagator b1;
|
165
|
-
|
166
|
+
BallPropagator b2;
|
166
167
|
Color c1;
|
167
168
|
Color c2;
|
168
|
-
|
169
|
+
BallMain (int xPanelSize, int yPanelSize){
|
169
170
|
|
170
171
|
this.xPanelSize = xPanelSize;
|
171
172
|
this.yPanelSize = yPanelSize;
|
172
173
|
|
173
174
|
|
174
|
-
b1 = new
|
175
|
+
b1 = new BallPropagator (10, 100, 100, 30, 40, 0, 9.8);
|
175
|
-
b2 = new
|
176
|
+
b2 = new BallPropagator (10, 10, 100, 50, 20, 0, 9.8);
|
176
177
|
c1=Color.black;
|
177
178
|
c2=Color.blue;
|
178
179
|
|
@@ -216,19 +217,19 @@
|
|
216
217
|
public static void main(String[] args) {
|
217
218
|
|
218
219
|
|
219
|
-
|
220
|
+
BallPropagator.dt=0.1;
|
220
221
|
|
221
222
|
|
222
223
|
JFrame frame = new JFrame();
|
223
224
|
|
224
225
|
|
225
|
-
|
226
|
+
BallMain panel = new BallMain(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
|
226
227
|
|
227
228
|
|
228
|
-
|
229
|
+
BallPropagator.xmin = 0;
|
229
|
-
|
230
|
+
BallPropagator.xmax = panel.xPanelSize;
|
230
|
-
|
231
|
+
BallPropagator.ymin = 0;
|
231
|
-
|
232
|
+
BallPropagator.ymax = panel.yPanelSize;
|
232
233
|
|
233
234
|
|
234
235
|
frame.pack();
|
@@ -243,6 +244,7 @@
|
|
243
244
|
}
|
244
245
|
```
|
245
246
|
```
|
247
|
+
//BallPropagator.java
|
246
248
|
public class BallPropagator_19rd158{
|
247
249
|
|
248
250
|
public static double dt;
|
@@ -259,7 +261,7 @@
|
|
259
261
|
|
260
262
|
double p=0.8;
|
261
263
|
|
262
|
-
|
264
|
+
BallPropagator(double r,double x,double y,double vx,double vy,double ax,double ay){
|
263
265
|
this.r=r;
|
264
266
|
this.x=x;
|
265
267
|
this.y=y;
|
@@ -273,11 +275,11 @@
|
|
273
275
|
this.x = this.x + this.vx * dt;
|
274
276
|
|
275
277
|
if(this.x < (double)xmin+this.r ){
|
276
|
-
|
278
|
+
|
277
279
|
this.vx *= -p;
|
278
280
|
this.x = (double)xmin+this.r;
|
279
281
|
}else if((double)xmax-this.r < x ){
|
280
|
-
|
282
|
+
|
281
283
|
this.vx *= -p;
|
282
284
|
this.x = (double)xmax-this.r;
|
283
285
|
}
|
1
付属のソースコードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
java
|
12
12
|
ソースコード
|
13
|
-
|
13
|
+
```
|
14
14
|
import java.awt.*;
|
15
15
|
import java.awt.event.*;
|
16
16
|
import javax.swing.*;
|
@@ -145,17 +145,175 @@
|
|
145
145
|
|
146
146
|
Timer timer = new Timer(10, panel);
|
147
147
|
timer.start();
|
148
|
+
|
149
|
+
}
|
150
|
+
}
|
151
|
+
```
|
152
|
+
```
|
153
|
+
|
154
|
+
import java.awt.*;
|
155
|
+
import java.awt.event.*;
|
156
|
+
import javax.swing.*;
|
157
|
+
|
158
|
+
public class BallMain_19rd158 extends JPanel implements ActionListener{
|
159
|
+
|
160
|
+
static int xPanelSize;
|
161
|
+
static int yPanelSize;
|
162
|
+
|
163
|
+
double x,y,r,vx,vy,ax,ay;
|
164
|
+
BallPropagator_19rd158 b1;
|
165
|
+
BallPropagator_19rd158 b2;
|
166
|
+
Color c1;
|
167
|
+
Color c2;
|
168
|
+
BallMain_19rd158(int xPanelSize, int yPanelSize){
|
169
|
+
|
170
|
+
this.xPanelSize = xPanelSize;
|
171
|
+
this.yPanelSize = yPanelSize;
|
172
|
+
|
173
|
+
|
174
|
+
b1 = new BallPropagator_19rd158(10, 100, 100, 30, 40, 0, 9.8);
|
175
|
+
b2 = new BallPropagator_19rd158(10, 10, 100, 50, 20, 0, 9.8);
|
176
|
+
c1=Color.black;
|
177
|
+
c2=Color.blue;
|
148
178
|
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
public void actionPerformed(ActionEvent e){
|
183
|
+
|
149
184
|
|
185
|
+
b1.xUpdate();
|
186
|
+
b1.yUpdate();
|
187
|
+
|
188
|
+
b1.vxUpdate();
|
189
|
+
b1.vyUpdate();
|
190
|
+
|
191
|
+
|
192
|
+
b2.xUpdate();
|
193
|
+
b2.yUpdate();
|
194
|
+
|
195
|
+
b2.vxUpdate();
|
196
|
+
b2.vyUpdate();
|
197
|
+
|
198
|
+
repaint();
|
150
199
|
}
|
200
|
+
|
201
|
+
public void paintComponent(Graphics g){
|
202
|
+
super.paintComponent(g);
|
203
|
+
|
204
|
+
g.setColor(c1);
|
205
|
+
|
206
|
+
g.fillOval((int)(b1.a(x) - b1.c(r)), (int)(b1.b(y) - b1.c(r)),
|
207
|
+
(int)(2*b1.c(r)), (int)(2*b1.c(r)));
|
208
|
+
|
209
|
+
|
210
|
+
g.setColor(c2);
|
211
|
+
|
212
|
+
g.fillOval((int)(b2.a(x) - b2.c(r)), (int)(b2.b(y) - b2.c(r)),
|
213
|
+
(int)(2*b2.c(r)), (int)(2*b2.c(r)));
|
214
|
+
}
|
215
|
+
|
216
|
+
public static void main(String[] args) {
|
217
|
+
|
218
|
+
|
219
|
+
BallPropagator_19rd158.dt=0.1;
|
220
|
+
|
221
|
+
|
222
|
+
JFrame frame = new JFrame();
|
223
|
+
|
224
|
+
|
225
|
+
BallMain_19rd158 panel = new BallMain_19rd158(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
|
226
|
+
|
227
|
+
|
228
|
+
BallPropagator_19rd158.xmin = 0;
|
229
|
+
BallPropagator_19rd158.xmax = panel.xPanelSize;
|
230
|
+
BallPropagator_19rd158.ymin = 0;
|
231
|
+
BallPropagator_19rd158.ymax = panel.yPanelSize;
|
232
|
+
|
233
|
+
|
234
|
+
frame.pack();
|
235
|
+
//
|
236
|
+
frame.setTitle("反射するボール");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setResizable(true);
|
237
|
+
frame.setVisible(true);
|
238
|
+
|
239
|
+
|
240
|
+
Timer timer = new Timer(10, panel);
|
241
|
+
timer.start();
|
242
|
+
}
|
151
243
|
}
|
244
|
+
```
|
245
|
+
```
|
246
|
+
public class BallPropagator_19rd158{
|
247
|
+
|
248
|
+
public static double dt;
|
249
|
+
public static int xmin;
|
250
|
+
public static int xmax;
|
251
|
+
public static int ymin;
|
252
|
+
public static int ymax;
|
253
|
+
|
254
|
+
|
255
|
+
private double r;
|
256
|
+
private double x, y;
|
257
|
+
private double vx, vy;
|
258
|
+
private double ax, ay;
|
259
|
+
|
260
|
+
double p=0.8;
|
261
|
+
|
262
|
+
BallPropagator_19rd158(double r,double x,double y,double vx,double vy,double ax,double ay){
|
263
|
+
this.r=r;
|
264
|
+
this.x=x;
|
265
|
+
this.y=y;
|
266
|
+
this.vx=vx;
|
267
|
+
this.vy=vy;
|
268
|
+
this.ax=ax;
|
269
|
+
this.ay=ay;
|
270
|
+
}
|
271
|
+
|
272
|
+
public void xUpdate() {
|
273
|
+
this.x = this.x + this.vx * dt;
|
274
|
+
|
275
|
+
if(this.x < (double)xmin+this.r ){
|
276
|
+
// this.vx = ??????????
|
277
|
+
this.vx *= -p;
|
278
|
+
this.x = (double)xmin+this.r;
|
279
|
+
}else if((double)xmax-this.r < x ){
|
280
|
+
// this.vx = ??????????
|
281
|
+
this.vx *= -p;
|
282
|
+
this.x = (double)xmax-this.r;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
|
286
|
+
public void yUpdate() {
|
287
|
+
this.y = this.y + this.vy * dt;
|
288
|
+
|
289
|
+
if(this.y < (double)ymin + this.r ){
|
290
|
+
|
291
|
+
this.vy *= -p;
|
292
|
+
this.y = (double)ymin+this.r;
|
293
|
+
}else if((double)ymax-this.r < this.y ){
|
294
|
+
|
295
|
+
this.vy *= -p;
|
296
|
+
this.y = (double)ymax-this.r;
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
public void vxUpdate(){
|
301
|
+
|
302
|
+
this.vx=this.ax*dt+this.vx;
|
152
303
|
|
153
|
-
|
154
|
-
|
155
|
-
|
304
|
+
}
|
156
|
-
|
305
|
+
|
157
|
-
|
306
|
+
public void vyUpdate(){
|
158
|
-
|
307
|
+
this.vy=this.ay*dt+this.vy;
|
308
|
+
|
309
|
+
}
|
310
|
+
public double a(double x){
|
311
|
+
return x=this.x;
|
312
|
+
}
|
313
|
+
public double b(double x){
|
314
|
+
return x=this.y;
|
159
|
-
|
315
|
+
} public double c(double x){
|
160
|
-
|
161
|
-
|
316
|
+
return x=this.r;
|
317
|
+
}
|
318
|
+
}
|
319
|
+
```
|