アットウィキロゴ

しゅう

import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;

class pro extends Frame implements Runnable{

Frame f;
Thread th;
int gun;
int dg;

public static void main(String[] args){
Frame f=new pro();
f.setTitle("pro");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.show();
}

pro() {
addWindowListener(new stopwin());
setLayout(new FlowLayout());

Button bu1 = new Button( "M" );
bu1.addActionListener(new beck());
add(bu1, BorderLayout.NORTH );
bu1.setActionCommand("c1");

Button bu2 = new Button("H");
bu2.addActionListener(new beck());
add(bu2,BorderLayout.SOUTH );
bu2.setActionCommand("c2");

setVisible( true );
gun=250;
dg=10;
th=new Thread(this);
th.start();
}


class beck implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ("c1".equals(e.getActionCommand()))dg=10 ;
if ("c2".equals(e.getActionCommand()))dg=-10;
}
}


class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}


public void paint( Graphics g ) {
g.setColor(Color.yellow);
g.fillRect(0,0,700,700);
gun=gun+dg;
g.setColor(Color.red);
g.fillRect(gun,350,10,10);

}

public void update(Graphics g) {
paint(g);
}

public void run() {
int t;
t=0; ;
while (t<200){
repaint();
try{th.sleep(100);} catch(InterruptedException e) { }
t=t+1;
}

}
}
最終更新:2011年01月27日 12:31