teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記

2018/01/14 04:14

投稿

aiueoaiueoaiue
aiueoaiueoaiue

スコア94

title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,6 @@
2
2
  Javaのアプレットビューワーでタイマーを使った簡単な描画を行いたいのですが、途中までは動くのですが、急に止まってしまいます。自分なりに調べたところペイントメソッドが急に呼ばれなくなっているみたいです。この原因がわかる方、おられましたら回答お願い致します。
3
3
  #ソース
4
4
  ```Java
5
-
6
5
  import java.applet.*;
7
6
  import java.io.*;
8
7
  import javax.swing.*;
@@ -18,65 +17,37 @@
18
17
  */
19
18
 
20
19
  public class test extends Applet implements ActionListener{
21
- int X = 75, Y = 30, W = 30, H = 50, K = 10 ;
20
+ int X = 75, Y = 30, W = 30, H = 50, K = 10 ;
22
- Timer timer, timer2;
21
+ Timer timer, timer2;
23
- int x;
22
+ int x;
24
- Button btn = new Button("スタート");
23
+ Button btn = new Button("スタート");
25
- boolean btnClicked = false;
24
+ boolean btnClicked = false;
26
25
 
27
- public void actionPerformed(ActionEvent e){
26
+ public void actionPerformed(ActionEvent e){
28
- if (e.getSource() == timer2&&this.btnClicked==true){
27
+ if (e.getSource() == timer2&&this.btnClicked==true){
29
- x+= 2;
28
+ x+= 2;
30
- if(x == 8){
29
+ if(x == 8){
31
- x = 0;
30
+ x = 0;
31
+ }
32
32
  }
33
+ if (e.getSource() == timer&&this.btnClicked==true){
34
+
35
+ timer2.start();
36
+ }
37
+ if (e.getSource()==this.btn){
38
+ this.btnClicked = true;
39
+ this.timer.start();
40
+ }
41
+ repaint() ;
33
42
  }
43
+ @Override
44
+ public void init(){
34
- if (e.getSource() == timer&&this.btnClicked==true){
45
+ timer = new Timer(20, this);
35
- timer2 = new Timer(2, this);
46
+ timer2 = new Timer(2, this);
36
- timer2.start();
47
+ add(this.btn);
48
+ this.btn.addActionListener(this);
37
49
  }
38
- if (e.getSource()==this.btn){
39
- this.btnClicked = true;
40
- this.timer.start();
41
- }
42
- repaint() ;
43
- }
44
- @Override
45
- public void init(){
46
- timer = new Timer(20, this);
47
- add(this.btn);
48
- this.btn.addActionListener(this);
49
- }
50
50
 
51
- public void start(){
52
- System.out.println("start");
53
- }
51
+ 省略
54
-
55
- public void stop(){
56
- System.out.println("stop");
57
- }
52
+ #追記
58
-
59
- public void destroy(){
60
- System.out.println("destroy");
61
- }
62
- public void update(Graphics g){
63
- paint(g);
64
- }
65
- public void paint(Graphics g){
66
- Dimension size = getSize();
67
- Image back = createImage(size.width, size.height);
68
- Graphics buffer = back.getGraphics();
69
- buffer.clearRect(0, 0, 150, 150);
70
- buffer.drawRect(X, Y, W, H);
71
- buffer.drawLine(X, Y+(K*1)-x, X+W, Y+(K*1)-x);
53
+ timer2のインスタンスの生成をinitの中で行うことでアプレットは止まらなくなりました。
72
- buffer.drawLine(X, Y+(K*2)-x, X+W, Y+(K*2)-x);
73
- buffer.drawLine(X, Y+(K*3)-x, X+W, Y+(K*3)-x);
74
- buffer.drawLine(X, Y+(K*4)-x, X+W, Y+(K*4)-x);
75
- buffer.drawLine(X, Y+(K*5)-x, X+W, Y+(K*5)-x);
76
- g.drawImage(back, 0, 0, this);
77
-
78
-
79
- }
80
-
81
- }
82
- ```

1

追加

2018/01/14 04:14

投稿

aiueoaiueoaiue
aiueoaiueoaiue

スコア94

title CHANGED
File without changes
body CHANGED
File without changes