//yakinsan.java 安澤出海 import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ //appletviewer yakinsan.java public class yakinsan extends Applet{ public int x =-80,y=20,a,b,c;//円のx座標. a:画像関連. b:移動方向. //0:右.1:左.2:下.3:上.4:停.c:speed public Dimension d; //アプレット領域の幅と高さ public Image off_buf; //オフスクリーンのバッファ public Graphics off_g; //オフスクリーンのグラフィックスコンテキスト public Image img[] = new Image[3]; //3枚の画像を管理 public Image current; //現在表示されている画像 public Button bt1,bt2,bt3,bt4,bt5,bt6,bt7;//ボタン public void init(){ bt1=new Button("右"); bt1.addActionListener(new MyActionListener1() ); bt2 = new Button("左"); bt2.addActionListener(new MyActionListener1() ); add(bt2); add(bt1); bt3 = new Button("上"); bt3.addActionListener(new MyActionListener1() ); add(bt3); bt4 = new Button("下"); bt4.addActionListener(new MyActionListener1() ); add(bt4); bt5 = new Button("停"); bt5.addActionListener(new MyActionListener1() ); add(bt5); bt6 = new Button("緩"); bt6.addActionListener(new MyActionListener1() ); add(bt6); bt7 = new Button("急"); bt7.addActionListener(new MyActionListener1() ); add(bt7); d = getSize(); a=0; b=0; c=1; img[0] = getImage(getDocumentBase(),"y1.gif");// "人1.gif"); //画像1の取り込み img[1] = getImage(getDocumentBase(),"y2.gif");// "人2.gif"); //画像2の取り込み img[2] = getImage(getDocumentBase(),"y3.gif");// "人2.gif"); //画像2の取り込み current = img[0]; //最初の画像をセット off_buf = createImage(d.width, d.height);//オフスクリーンのサイズ設定。 off_g = off_buf.getGraphics(); MyThread mt = new MyThread(); //スレッドの起動 mt.start(); } public void paint(Graphics g) //描画処理 { off_g.clearRect(0, 0, d.width, d.height);//画面クリアー off_g.drawImage(current, x, y, this); //描画処理 g.drawImage(off_buf, 0, 0, this); //オフスクリーンを画面に描画 } public void update(Graphics g) { paint(g); } class MyThread extends Thread{ //スレッドクラスの定義(内部クラス) public void run(){ try{ for(;;){ if(b==0){ x=x+1+c; } else if(b==1){ x=x-1-c; } else if(b==2){ y=y+2+c; } else if(b==3){ y=y-2-c; } else{} if( x > d.width-74){ b=1;//x = -80; } else if(x<0){ b=0;//x=d.width+5; } else if(y>d.height-80){ b=3; } else if(y<16){ b=2; } if(a==0){ current = img[0]; } else if(a==1){ current = img[2]; } else if(a==2){ current = img[1]; } else{ current = img[2]; } a++; if(a>3){a=0;} repaint(); sleep(100); } } catch(InterruptedException e){ } } } class MyActionListener1 implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==bt1){ b=0; } else if(e.getSource()==bt2){ b=1; } else if(e.getSource()==bt3){ b=3; } else if(e.getSource()==bt4){ b=2; } else if(e.getSource()==bt5){ b=4; } else if(e.getSource()==bt6){ c--; if(c<1){c=1;} } else if(e.getSource()==bt7){ c++; if(c>5){c=5;} } } } }