使用timer,然后在每次timer触发时重画程序,为防止闪烁,可使用双缓冲
解决方案 »
- 如何在JTextArea内弹出框
- 初学基础作业里一个抽象类构造方法的问题,我这个写不出来,所以也不能compile
- 选用swt还是swing?
- 怎么对时间进行加减操作?
- 获取radio值的问题
- 询问修改文件某一行数据的java方法
- 求救啊~~~~~~~~~~~~~~~~求救
- 哪有画图的applet源码下?急用!高分相送!
- 谁有jbuilder5的注册机
- 菜鸟级问题,望赐教!
- java作为client调用远程的corba接口(C++写的server)时候出现Data conversion异常!
- 在unix中怎么布JDK和JDBC呀,怎么在其中写代码可以读SQL server中的表,请大家帮忙一下
不知道你能不能访问
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;/**
Shows an animated bouncing ball.
*/
public class Bounce
{
public static void main(String[] args)
{
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
The frame with canvas and buttons.
*/
class BounceFrame extends JFrame
{
/**
Constructs the frame with the canvas for showing the
bouncing ball and Start and Close buttons
*/
public BounceFrame()
{
setSize(WIDTH, HEIGHT);
setTitle("Bounce"); Container contentPane = getContentPane();
canvas = new BallCanvas();
contentPane.add(canvas, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start",
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
addBall();
}
}); addButton(buttonPanel, "Close",
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
contentPane.add(buttonPanel, BorderLayout.SOUTH);
} /**
Adds a button to a container.
@param c the container
@param title the button title
@param listener the action listener for the button
*/
public void addButton(Container c, String title,
ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
} /**
Adds a bouncing ball to the canvas and makes
it bounce 1,000 times.
*/
public void addBall()
{
try
{
Ball b = new Ball(canvas);
canvas.add(b); for (int i = 1; i <= 1000; i++)
{
b.move();
Thread.sleep(5);
}
}
catch (InterruptedException exception)
{
}
} private BallCanvas canvas;
public static final int WIDTH = 450;
public static final int HEIGHT = 350;
}/**
The canvas that draws the balls.
*/
class BallCanvas extends JPanel
{
/**
Add a ball to the canvas.
@param b the ball to add
*/
public void add(Ball b)
{
balls.add(b);
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < balls.size(); i++)
{
Ball b = (Ball)balls.get(i);
b.draw(g2);
}
} private ArrayList balls = new ArrayList();
}/**
A ball that moves and bounces off the edges of a
component
*/
class Ball
{
/**
Constructs a ball in the upper left corner
@c the component in which the ball bounces
*/
public Ball(Component c) { canvas = c; } /**
Draws the ball at its current position
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
g2.fill(new Ellipse2D.Double(x, y, XSIZE, YSIZE));
} /**
Moves the ball to the next position, reversing direction
if it hits one of the edges
*/
public void move()
{
x += dx;
y += dy;
if (x < 0)
{
x = 0;
dx = -dx;
}
if (x + XSIZE >= canvas.getWidth())
{
x = canvas.getWidth() - XSIZE;
dx = -dx;
}
if (y < 0)
{
y = 0;
dy = -dy;
}
if (y + YSIZE >= canvas.getHeight())
{
y = canvas.getHeight() - YSIZE;
dy = -dy;
} canvas.paint(canvas.getGraphics());
} private Component canvas;
private static final int XSIZE = 15;
private static final int YSIZE = 15;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
}
如何初始化一个Graphics?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;public class HorseTest extends JApplet implements ActionListener
{
private JButton btnStart,btnPause;
private JPanel buttonPanel;
private int xGreen=20,yGreen=20,xBlue=60,yBlue=20,xRed=100,yRed=20,count=2;
private Container c;
private Timer t;
public void init()
{
c=getContentPane();
t=new Timer(50,this);
buttonPanel=new JPanel();
buttonPanel.setBackground(Color.magenta);
btnStart=new JButton("开始");
btnStart.addActionListener(this); btnPause=new JButton("暂停");
btnPause.addActionListener(this); buttonPanel.add(btnStart);
buttonPanel.add(btnPause);
c.add(buttonPanel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnPause)
t.stop();
else if(e.getSource()==btnStart)
t.start();
else
{
if(xRed>=20 && xRed<340 && yRed==20) //第一转向
xRed+=40;
else if(xRed==340 && yRed>=20 && yRed<340) //第二
yRed+=40;
else if(yRed>=340 && xRed<=340 && xRed>20) //第三
xRed-=40;
else if( xRed==20 && yRed>20 && yRed<=340 ) //第四
yRed-=40;
if(xBlue>=20 && xBlue<340 && yBlue==20)
xBlue+=40;
else if(xBlue==340 && yBlue>=20 && yBlue<340)
yBlue+=40;
else if(yBlue>=340 && xBlue<=340 && xBlue>20)
xBlue-=40;
else if(yBlue>20 && yBlue<=340 && xBlue==20 )
yBlue-=40; if(xGreen>=20 && xGreen<340 && yGreen==20)
xGreen+=40;
else if(xGreen==340 && yGreen>=20 && yGreen<340)
yGreen+=40;
else if(yGreen>=340 && xGreen<=340 && xGreen>20)
xGreen-=40;
else if(yGreen>20 && yGreen<=340 && xGreen==20 )
yGreen-=40;
}
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g; g2.setColor(Color.magenta);
g2.drawRect(20,20,360,360);
g2.setColor(Color.yellow);
g2.drawRect(60,60,280,280); count++;
if(count%3==0)
g2.setColor(Color.blue);
else if(count%3==1)
g2.setColor(Color.green);
else if(count%3==2)
g2.setColor(Color.red);
g2.fill(new Ellipse2D.Double(xGreen,yGreen,40,40));
if(count%3==0)
g2.setColor(Color.green);
else if(count%3==1)
g2.setColor(Color.red);
else if(count%3==2)
g2.setColor(Color.blue);
g2.fill(new Ellipse2D.Double(xBlue,yBlue,40,40));
if(count%3==0)
g2.setColor(Color.red);
else if(count%3==1)
g2.setColor(Color.blue);
else if(count%3==2)
g2.setColor(Color.green);
g2.fill(new Ellipse2D.Double(xRed,yRed,40,40));
}
}
Java 2 图形设计卷Ⅰ:AWT
http://ecapital.myetang.com/java/tutorial/awt01/index.htmlJava 2 图形设计卷Ⅱ:SWING
http://ecapital.myetang.com/swing/tutorial/s02/index.html里面专门讲了几章动画