?? ball.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Ball extends Thread
{ public Ball(JPanel b, Color c) { box = b; color = c; }
public void draw()
{ Graphics g = box.getGraphics();
g.setColor(color);
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move()
{ if (!box.isVisible()) return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.setColor(color);
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
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()
{ try
{ draw();
for (int i = 1; i <= 1000; i++)
{ move();
sleep(5);
}
}
catch(InterruptedException e) {}
}
private JPanel box;
private static final int XSIZE = 10;
private static final int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
private Color color;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -