概述
本文基于“Java核心技术卷I -- 第14章 并发”实验程序-弹跳小球。如果运行程序球就会自如地来回弹跳,但是在单线程版本这个程序完全控制了整个应用程序,你甚至都不能把它占用的资源释放,当你关闭这个程序的时候,你不得不等待小球运动完毕。在球弹跳结束前你无法再与程序进行交互,更无法让多个小球同时运行,这意味着在单线程的世界里你启动程序等待结果,而无法控制程序运行的过程。这种无法交互的程序几乎没有意义。在多线程版本中使用线程分割不同任务,即将任务拆分成子任务并放在独立的线程中,多个小球可以同时运行,不仅大大提高跳球的运动能力,还可以提高程序的交互性,更可给予重负载任务更多线程资源的支持。每个球都在自己的线程中,用户界面操作事件线程也独立运行,由于每个线程都有机会运行,所以在多线程版本中,在球弹跳期间,点击关闭按钮,事件调度线程将会注意到该事件并处理“关闭”动作,事件得以及时响应。多任务拆分多线程运行非常重要!
版本1-单线程版
程序模拟了一个在窗口中反弹的球,主要分为以下几个类:
-
Ball 类:表示一个球对象,包含球的位置、速度等属性,以及球的移动和绘制方法。
-
Bounce 类:程序的主入口,包含
main
方法,用于启动图形界面。 -
BounceFrame 类:表示主窗口,包含一个球组件和几个按钮,用于控制球的运动。
-
BallComponent 类:表示球的组件,负责绘制球,并管理球的添加和移动。
详细类说明
1. Ball 类
-
属性:
-
x
和y
:球的当前位置。 -
dx
和dy
:球的水平和垂直速度。 -
XSIZE
和YSIZE
:球的大小。
-
-
方法:
-
move
:根据当前速度移动球的位置,如果碰到边界则反弹。 -
getShape
:获取球的形状,用于绘制。
-
2. Bounce 类
-
方法:
-
main
:程序的入口,创建并显示主窗口。
-
3. BounceFrame 类
-
属性:
-
comp
:一个球组件,用于显示球的运动。 -
STEPS
和DELAY
:球的运动步数和每步的延迟时间。
-
-
方法:
-
addButton
:向窗口添加按钮,并设置按钮的事件监听器。 -
addBall
:向组件中添加一个球,并控制球的运动。
-
4. BallComponent 类
-
属性:
-
balls
:一个球的列表,用于管理多个球对象。 -
DEFAULT_WIDTH
和DEFAULT_HEIGHT
:组件的默认大小。
-
-
方法:
-
add
:向组件中添加一个球。 -
paintComponent
:重写paintComponent
方法,用于绘制球。 -
getPreferredSize
:获取组件的首选大小。
-
程序运行流程
-
通过
Bounce
类的main
方法启动程序。 -
创建一个
BounceFrame
对象并显示出来。 -
用户可以通过窗口中的按钮来添加球。
-
球在窗口中移动,并在碰到边界时反弹。
-
用户可以观察多个球在窗口中反弹的动画效果。
程序清单
package bounce;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
/*
* A ball that moves and bounces off the edges of a rectangle
* @version 1.33 2007-05-17
* @author Cay Horstmann
*/
class Ball
{
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;
/*
* 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);
}
}
/*
* Shows an animated bouncing ball.
* @version 1.34 2015-06-21
* @author Cay Horstmann
*/
public class Bounce
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/*
* The frame with ball component and buttons.
*/
class BounceFrame extends JFrame
{
private BallComponent comp;
public static final int STEPS = 1000;
public static final int DELAY = 3;
/*
* Constructs the frame with the component for showing the bouncing ball and
* Start and Close buttons
*/
public BounceFrame()
{
setTitle("Bounce");
comp = new BallComponent();
add(comp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", event -> addBall());
addButton(buttonPanel, "Close", event -> System.exit(0));
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
/*
* 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 panel and makes it bounce 1,000 times.
*/
public void addBall()
{
try
{
Ball ball = new Ball();
comp.add(ball);
for (int i = 1; i <= STEPS; i++)
{
ball.move(comp.getBounds());
comp.paint(comp.getGraphics());
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
}
}
/*
* The component that draws the balls.
* @version 1.34 2012-01-26
* @author Cay Horstmann
*/
class BallComponent extends JPanel
{
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
private java.util.List<Ball> balls = new ArrayList<>();
/*
* Add a ball to the component.
* @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());
}
}
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}
版本2-多线程版
任务拆分
拆分任务,将小球的移动重绘从事件调度线程中拆分处理,放在独立的线程中,使之互不关联。如下:
Runnable r = () -> {
try
{
for (int i = 1; i <= STEPS; i++)
{
ball.move(comp.getBounds());
comp.repaint();
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
};
Thread t = new Thread(r);
t.start();
程序清单
package bounceThread;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.*;
/**
* Shows animated bouncing balls.
* @version 1.34 2015-06-21
* @author Cay Horstmann
*/
public class BounceThread
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new BounceFrame();
frame.setTitle("BounceFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/**
* The frame with panel and buttons.
*/
class BounceFrame extends JFrame
{
private BallComponent comp;
public static final int STEPS = 1000;
public static final int DELAY = 5;
/**
* Constructs the frame with the component for showing the bouncing ball and
* Start and Close buttons
*/
public BounceFrame()
{
comp = new BallComponent();
add(comp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", event -> addBall());
addButton(buttonPanel, "Close", event -> System.exit(0));
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
/**
* 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 starts a thread to make it bounce
*/
public void addBall()
{
Ball ball = new Ball();
comp.add(ball);
Runnable r = () -> {
try
{
for (int i = 1; i <= STEPS; i++)
{
ball.move(comp.getBounds());
comp.repaint();
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
};
Thread t = new Thread(r);
t.start();
}
}
/*
* The component that draws the balls.
* @version 1.34 2012-01-26
* @author Cay Horstmann
*/
class BallComponent extends JPanel
{
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
private java.util.List<Ball> balls = new ArrayList<>();
/*
* Add a ball to the component.
* @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());
}
}
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}
/*
* A ball that moves and bounces off the edges of a rectangle
* @version 1.33 2007-05-17
* @author Cay Horstmann
*/
class Ball
{
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;
/*
* 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);
}
}