全代码如下,线程那一章的例子:
import java.awt.*;
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.setVisible(true);
}
} /**
A ball that moves and bounces off the edges of a
rectangle
*/
class Ball
{
/**
Moves the ball to the next position, reversing direction
if it hits one of the edges
*/
public void move(Rectangle2D bounds)
{
x += dx;
y += dy;
if (x < bounds.getMinX())
{
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX())
{
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY())
{
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY())
{
y = bounds.getMaxY() - YSIZE;
dy = -dy;
}
} /**
Gets the shape of the ball at its current position.
*/
public Ellipse2D getShape()
{
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
} private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1;
} /**
The panel that draws the balls.
*/
class BallPanel extends JPanel
{
/**
Add a ball to the panel.
@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 (Ball b : balls)
{
g2.fill(b.getShape());
}
} private ArrayList<Ball> balls = new ArrayList<Ball>();
} /**
The frame with panel and buttons.
*/
class BounceFrame extends JFrame
{ public BounceFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setTitle("Bounce"); panel = new BallPanel();
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start",
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
addBall();
}
}); addButton(buttonPanel, "Close",
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
}
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 panel and makes
it bounce 1,000 times.
*/
public void addBall()
{
try
{
Ball ball = new Ball();
panel.add(ball); for (int i = 1; i <= STEPS; i++)
{
ball.move(panel.getBounds());
panel.paint(panel.getGraphics());//各位高手注意这里,调用repaint无效,为什么?????
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
} private BallPanel panel;
public static final int DEFAULT_WIDTH = 450;
public static final int DEFAULT_HEIGHT = 350;
public static final int STEPS = 1000;
public static final int DELAY = 3;
}
import java.awt.*;
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.setVisible(true);
}
} /**
A ball that moves and bounces off the edges of a
rectangle
*/
class Ball
{
/**
Moves the ball to the next position, reversing direction
if it hits one of the edges
*/
public void move(Rectangle2D bounds)
{
x += dx;
y += dy;
if (x < bounds.getMinX())
{
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX())
{
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY())
{
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY())
{
y = bounds.getMaxY() - YSIZE;
dy = -dy;
}
} /**
Gets the shape of the ball at its current position.
*/
public Ellipse2D getShape()
{
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
} private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1;
} /**
The panel that draws the balls.
*/
class BallPanel extends JPanel
{
/**
Add a ball to the panel.
@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 (Ball b : balls)
{
g2.fill(b.getShape());
}
} private ArrayList<Ball> balls = new ArrayList<Ball>();
} /**
The frame with panel and buttons.
*/
class BounceFrame extends JFrame
{ public BounceFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setTitle("Bounce"); panel = new BallPanel();
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start",
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
addBall();
}
}); addButton(buttonPanel, "Close",
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
}
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 panel and makes
it bounce 1,000 times.
*/
public void addBall()
{
try
{
Ball ball = new Ball();
panel.add(ball); for (int i = 1; i <= STEPS; i++)
{
ball.move(panel.getBounds());
panel.paint(panel.getGraphics());//各位高手注意这里,调用repaint无效,为什么?????
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
} private BallPanel panel;
public static final int DEFAULT_WIDTH = 450;
public static final int DEFAULT_HEIGHT = 350;
public static final int STEPS = 1000;
public static final int DELAY = 3;
}
解决方案 »
- JAVA调用DLL错误,请高手寻找错误原因,给出解决办法
- 什么地方有JAVA所有函数的文挡
- 有关继承编译问题,请高人指点!
- 菜鸟问关于paint()的用法
- 有做过类似Editplus编辑器的吗?文本编辑,左边显示1、2、3..所在的行数,是怎么实现的?
- JAVA---------BufferedReader和BufferedWriter
- 为什么我java classpath 就是配置不好???
- 在Applet里面如何打开IE浏览器?
- 使用jdbc-odbc连接mysql.出现java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY!
- 这样的错误是为什么?????
- 帮忙看一下,我运行后得出的结果不太对
- 谁有《java编程语言》中文版的电子书?
paint
public void paint(Graphics g)由 Swing 调用,以绘制组件。应用程序不应直接调用 paint,而是应该使用 repaint 方法来安排重绘组件。
此方法实际上将绘制工作委托给三个受保护的方法:paintComponent、paintBorder 和 paintChildren。按列出的顺序调用这些方法,以确保子组件出现在组件本身的顶部。一般来说,不应在分配给边框的 insets 区域绘制组件及其子组件。子类可以始终重写此方法。只想特殊化 UI(外观)委托的 paint 方法的子类只需重写 paintComponent。然后官方的相关文档上有说明
Components that render complex output should invoke repaint() with arguments defining only the region that requires updating. A common mistake is to always invoke the no-arg version, which causes a repaint of the entire component, often resulting in unnecessary paint processing. 也就是说用无参数的repaint是不可取的而事实上,看过源码之后,我认为是因为无参数的repaint仅仅是调用了parent容器的repaint
但是这个repaint是没有对ball进行绘画处理的
所以最后什么都看不见所以尽量一定要使用有参数的repaint方法