public Bounce() { setTitle("Bounce"); canvas = new Canvas(); add("Center", canvas); Panel p = new Panel(); p.add(new Button("Start")); p.add (new Button("Express")); p.add(new Button("Close")); add("South", p); }
public boolean handleEvent(Event evt) { if(evt.id == Event.WINDOW_DESTROY) System.exit(0);
return super.handleEvent(evt); }
public boolean action(Event evt, Object arg) { if(arg.equals ("Start")) { Ball b = new Ball(canvas, Color.BLACK); b.setPriority (Thread.NORM_PRIORITY); b.start(); } else if(arg.equals ("Express")) { Ball b = new Ball(canvas, Color.YELLOW); b.setPriority (Thread.MAX_PRIORITY); b.start (); } else if(arg.equals ("Close")) System.exit(0); else return super.action(evt, arg);
return true; }
public static void main(String[] args) { Frame f = new Bounce(); f.resize (500, 350); f.show(); } }class Ball extends Thread{ private Canvas box; private final static int XSIZE = 10; private final static int YSIZE = 10; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2; private Color color;
public Ball(Canvas c, Color co) { box = c; color = co; }
public void draw() { Graphics g = box.getGraphics (); g.setColor(color); g.fillOval (x, y, XSIZE, YSIZE); g.dispose (); }
public void move() { Graphics g = box.getGraphics (); g.setXORMode (box.getBackground ()); g.setColor(color); g.fillOval (x, y, XSIZE, YSIZE);
x += dx; y += dy;
Dimension d = box.size ();
if(x < 0) { x = 0; dx = -dx; } if(x +XSIZE >= d.width) { x = d.width - XSIZE; dx = -dx; } if(y < 0) { y = 0; dy = -dy; } if(y + YSIZE >= d.height) { y = d.height - YSIZE; dy = -dy; } g.fillOval (x, y, XSIZE, YSIZE); g.dispose (); }
import java.awt.*;
import java.util.*;class Bounce extends Frame {
private Canvas canvas;
public Bounce() {
setTitle("Bounce");
canvas = new Canvas();
add("Center", canvas);
Panel p = new Panel();
p.add(new Button("Start"));
p.add (new Button("Express"));
p.add(new Button("Close"));
add("South", p);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(evt);
}
public boolean action(Event evt, Object arg) {
if(arg.equals ("Start")) {
Ball b = new Ball(canvas, Color.BLACK);
b.setPriority (Thread.NORM_PRIORITY);
b.start();
}
else if(arg.equals ("Express")) {
Ball b = new Ball(canvas, Color.YELLOW);
b.setPriority (Thread.MAX_PRIORITY);
b.start ();
}
else if(arg.equals ("Close"))
System.exit(0);
else return super.action(evt, arg);
return true;
}
public static void main(String[] args)
{
Frame f = new Bounce();
f.resize (500, 350);
f.show();
}
}class Ball extends Thread{
private Canvas box;
private final static int XSIZE = 10;
private final static int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
private Color color;
public Ball(Canvas c, Color co) {
box = c;
color = co;
}
public void draw() {
Graphics g = box.getGraphics ();
g.setColor(color);
g.fillOval (x, y, XSIZE, YSIZE);
g.dispose ();
}
public void move() {
Graphics g = box.getGraphics ();
g.setXORMode (box.getBackground ());
g.setColor(color);
g.fillOval (x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.size ();
if(x < 0) {
x = 0;
dx = -dx;
}
if(x +XSIZE >= d.width) {
x = d.width - XSIZE;
dx = -dx;
}
if(y < 0) {
y = 0;
dy = -dy;
}
if(y + YSIZE >= d.height) {
y = d.height - YSIZE;
dy = -dy;
}
g.fillOval (x, y, XSIZE, YSIZE);
g.dispose ();
}
public void run() {
draw();
for(; ;) {
move();
try {
Thread.sleep (10);
} catch(InterruptedException e) {
}
}
}
}
这又是为什么呢?