大家好,从今天开始,有计划的恶补一下并发知识。
目录见:目录
示例代码来源
《Java核心技术 卷1 第10版》 Core Java Volume I-Fundamentals(10th Edition)
[美] Cay S.Horstmann 著
周立新 陈波 叶乃文 邝劲筠 杜永萍 译
一、效果图
上图的却显示无法通过多次点击Start出现多个弹球,Close也没法关闭。
上图用到了线程,可发射多个弹球并且可Close。
二、示例代码
git仓库 git@github.com:cmhhcm/guiAndConcurrent.git
1、无线程代码
package com.cmh.concurrent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
/**
* 核心类-绘制图形并使球弹跳
*
* Author: cmh
* Date:2021/4/18 1:39 上午
*/
public class Bounce {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
static class BounceFrame extends JFrame {
private BallComponent comp;
public static final int STEPS = 5000000;
public static final int DELAY = 8;
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
*/
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 N 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) {
e.printStackTrace();
}
}
}
}
Ball
package com.cmh.concurrent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
/**
* 控制弹球移动
*
* Author: cmh
* Date:2021/4/18 1:39 上午
*/
public 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 positionon
*/
public Ellipse2D getShape() {
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
}
}
BallComponent
package com.cmh.concurrent;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
/**
* 弹球面板类-填充颜色和大小
*
* Author: cmh
* Date:2021/4/18 1:40 上午
*/
public class BallComponent extends JPanel {
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
/**
* ? 这个是干嘛用的?为啥要存在List里面?
*/
List<Ball> balls = new ArrayList<>();
/**
* Add a ball to the component
*/
public void add(Ball b) {
balls.add(b);
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //erase background
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls) {
System.out.println("List的size是:"+balls.size());
g2.fill(b.getShape());
}
}
public Dimension getPreferredSize() {
return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}
2、有线程代码
package com.cmh.concurrent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
/**
* Author: cmh
* Date:2021/4/18 2:35 下午
*/
public class BounceThread {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new BounceFrame();
frame.setTitle("BounceTread");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
static class BounceFrame extends JFrame {
private BallComponent comp;
public static final int STEPS = 1000000;
public static final int DELAY = 3;
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();
}
public void addButton(Container container, String title, ActionListener listener) {
JButton button = new JButton(title);
container.add(button);
button.addActionListener(listener);
}
public void addBall() {
Ball ball = new Ball();
comp.add(ball);
Runnable runnable = () -> {
try {
for (int i = 1; i <= STEPS; i++) {
ball.move(comp.getBounds());
// comp.repaint();
comp.paint(comp.getGraphics());
Thread.sleep(DELAY);
}
} catch (InterruptedException interruptedException) {
}
};
Thread thread = new Thread(runnable);// status:NEW
thread.start();//status:RUNNABLE
}
}
/**
* 总结:
* 1、多次连续点击start的时候,部分球就原地不动了,为什么?
* 2、
*/
}
三、核心
1、控制球的移动
代码很简单,效果比较炫
2、画弹球
paintComponent()方法super.paintComponent(g);
这行代码去除了背景,否则每次移动的弹球轨迹都会展现出来,效果就像“贪吃蛇”了。
3、并发部分
没个弹球的位置移动都是一个独立的线程来控制
public void addBall() {
Ball ball = new Ball();
comp.add(ball);
Runnable runnable = () -> {
try {
for (int i = 1; i <= STEPS; i++) {
ball.move(comp.getBounds());
comp.repaint();
Thread.sleep(DELAY);
}
} catch (InterruptedException interruptedException) {
}
};
Thread thread = new Thread(runnable);// status:NEW
thread.start();//status:RUNNABLE
}
好了,再会。