#内容
JavaのTimerを用いてアプレットの描画を行いたいのですがその描画のスピードを自動的に変化させたいです。例えばnew Timer(1000,this)で1秒感覚でタイマーを動かしながら別のタイマーを使い、最初の5秒はnew Timer2(10,this)で描画、次は3秒間new Timer2(500,this)で描画、という風にしたいです。
#ソース
Java
1import java.applet.*; 2import javax.swing.*; 3import java.awt.*; 4import java.awt.event.*; 5 6 7/* 8<applet code="Test.class" width="150" height="150"> 9</applet> 10*/ 11 12public class Test extends Applet implements ActionListener{ 13 int X = 75, Y = 30, W = 30, H = 50, K = 10 ; 14 Timer timer, timer2; 15 int x; 16 Button btn = new Button("スタート"); 17 boolean btnClicked = false; 18 19 public void actionPerformed(ActionEvent e){ 20 if (e.getSource() == timer2&&this.btnClicked==true){ 21 x+= 2; 22 if(x == 150){ 23 x = 0; 24 } 25 } 26 if (e.getSource() == timer&&this.btnClicked==true){ 27 timer2.start(); 28 } 29 if (e.getSource()==this.btn){ 30 this.btnClicked = true; 31 this.timer.start(); 32 } 33 repaint() ; 34 } 35 @Override 36 public void init(){ 37 timer = new Timer(20, this); 38 timer2 = new Timer(2, this); 39 add(this.btn); 40 this.btn.addActionListener(this); 41 } 42 public void paint(Graphics g){ 43 g.drawString("Helio", x, 75); 44 } 45 46}
#試したこと
最初は2つ目のタイマーのインスタンスの生成を
if (e.getSource() == timer&&this.btnClicked==true){ timer2.start(); }
の中で行い、それに変数を入れていけばいいかなと思ったのですが、それだとインスタンスが無限に生成されてしまい、途中でアプレットが止まってしまいました。ですので、現在はtimer2のインスタンスの生成をinitの中で行いアプレットが止まるという事だけは回避している状態です。
回答1件
あなたの回答
tips
プレビュー