[j03040:java/rep#8/kan] j03040% java List test.java
File name:test.java
1:import java.awt.*;
2:import java.awt.event.*;
3:import java.applet.*;
4:
5:public class test extends Applet implements Runnable,
6:ActionListener{ //Appletを継承、Runnable,ActionListenerを実装
7:
8: int x,y;
9: static int sp1,sp2; //スピード変数
10: Image i,m; //イメージを格納する変数
11: Thread thread; //スレッド
12: back th=new back(); //back()のインスタンスを生成(backは下記参照)
13:
14: Label x0=new Label("Zoomer"); //GUI部品ラベルを生成
15: Label x1=new Label("DioZx98'ver");
16: Button acz=new Button("Full Throttle"); //GUI部品ボタンを生成
17: Button acx=new Button("Full Throttle");
18: Button brz=new Button("Durm Brake");
19: Button brx=new Button("Disk Brake");
20: Button engz=new Button("Engine Trouble!!");
21: Button engx=new Button("Engine Trouble!!");
22:
23: public void init(){ //最初に呼ばれるメソッド
24: setLayout(null); //レイアウトを自分で決める
25: sp1=4;sp2=6;
26: setBackground(Color.white); //背景の色を設定
27: w=getSize().width; //アプレットの横の長さを取得
28: i=getImage(getDocumentBase(),"Zm.gif"); //画像の場所を取得
29: m=getImage(getDocumentBase(),"Zx.gif");
30: //GUI部品の登録&張り付け
31: add(x0); x0.setBounds(10,205,100,30);
32: add(x1); x1.setBounds(130,205,100,30);
33: add(acz); acz.setBounds(10,230,100,30);
34: add(acx); acx.setBounds(130,230,100,30);
35: add(brz); brz.setBounds(10,255,100,30);
36: add(brx); brx.setBounds(130,255,100,30);
37: add(engz); engz.setBounds(10,290,120,30);
38: add(engx); engx.setBounds(130,290,120,30);
39: acz.addActionListener(this); //AcrionListenerに登録
40: acx.addActionListener(this);
41: brz.addActionListener(this);
42: brx.addActionListener(this);
43: engz.addActionListener(this);
44: engx.addActionListener(this);
45: }
46:
47: public void start(){ //スレッドを開始させる
48: thread = new Thread( this ); //インスタンス生成
49: thread.start(); //thred開始
50: th.start(); //back開始
51: }
52:
53: public void stop(){ //Applet終了の時の処理
54: thread = null; //スレッドを空にする
55: }
56:
57: public void paint(Graphics g){ //描写処理
58: th.paint(g); //back()のpaintを呼び出し
59: g.drawImage(i,x,60,this); //画像張り付け
60: g.drawImage(m,y,130,this);
61: }
62:
63: public void actionPerformed(ActionEvent e){ //イベント処理(ボタンを押した)
64: if(e.getSource()==acz){sp1=9;}
65: else if(e.getSource()==acx){sp2=8;}
66: else if(e.getSource()==brz){sp1=4;}
67: else if(e.getSource()==brx){sp2=6;}
68: else if(e.getSource()==engz){sp1=1;}
69: else if(e.getSource()==engx){sp2=1;}
70: }
71: public void update(Graphics g){
72: paint(g);
73: }
74: int w;
75: public void run(){ //threadの処理
76: while(true){ //無限ループ
77: x=x+sp1;
78: y=y+sp2;
79: if(x>w){x=0;}
80: if(y>w){y=0;}
81: repaint();
82: try{Thread.sleep(100);
83: }catch (InterruptedException e){
84: }
85: }
86: }
87:
88:}
|