📄 20f10a5d486a001c1e36e6e62da4b12f
字号:
/*
* Created on 2007-9-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class BounceApplet extends Applet
{
public BounceApplet()
{
BounceExpress bounce=new BounceExpress();
bounce.setSize(300,200);
bounce.show();
}
}
class BounceExpress extends Frame
{
Canvas canvas;
Button blackButton,redButton,closeButton;
public BounceExpress()
{
canvas=new Canvas();
Panel p=new Panel();
blackButton=new Button("添加黑色小球");
redButton=new Button("添加红色小球");
closeButton=new Button("关闭窗口");
Listener listener=new Listener();
setTitle("BounceExpress");
canvas.setBackground(Color.lightGray);
add("Center",canvas);
add("South",p);
p.add(blackButton);
p.add(redButton);
p.add(closeButton);
blackButton.addActionListener(listener);
redButton.addActionListener(listener);
closeButton.addActionListener(listener);
this.addWindowListener(new WListener());
}
class WListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==blackButton)
{
Ball b=new Ball(canvas,Color.black);
b.setPriority(Thread.NORM_PRIORITY);
b.start();
}
else
if(e.getSource()==redButton)
{
Ball b=new Ball(canvas,Color.red);
b.setPriority(Thread.NORM_PRIORITY+2);
b.start();
}
else
if(e.getSource()==closeButton)
{
dispose();
}
}
}
}
class Ball extends Thread
{
private Canvas box;
private static final int XSIZE=10;
private static final int YSIZE=10;
private int x=0,y=0;
private int dx=2,dy=2;
private Color color;
public Ball(Canvas _canvas,Color _color)
{
box=_canvas;
color=_color;
this.setDaemon(true);
}
public void draw()
{
Graphics g=box.getGraphics();
g.setColor(color);
g.fillOval(x+20,y,XSIZE,YSIZE);
}
public void move()
{
Graphics g=box.getGraphics();
if(g==null)
return;
g.setColor(color);
g.setXORMode(box.getBackground());
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);
}
public void run()
{
draw();
for( ; ; )
{
move();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -