如何实现线程的暂停和停止?
之前我们写
while(true){
}
让小球一直运动。如果要让它暂停可以这样写
While(FLAG){
Continue;
Flag;
}
设置一个标识位通过改变它的布尔值来达到让循环暂停的目的。
停止同理。
While(flag){
...
Flag=false;
}
在一定条件下让标志位改为FALSE就可以让循环停止了。停止之后别忘了用remove()
移除线程
编程的基本思路:
要写三个类
1游戏界面类:
添加必要的按钮等组件。写一个让颜色渐变的函数,返回颜色。
然后写重绘方法,在方法里写背景,去锯齿,然后一层一层循环画立体球。
<!--[if !supportLists]-->2.<!--[endif]-->监听器类:
在此类里用可变长的向量存储线程。并用构造方法传递窗体。
主要用于获取按钮上的内容,根据内容作出相应的操作。
注意点添加的时候要创建新线程,停止的时候要移除。
<!--[if !supportLists]-->3.<!--[endif]-->游戏线程类:
继承自线程类。
在此类里面写一个函数初始化各项参数。以及定义两个标识位分别控制暂停和停止。
然后写一个函数move()控制小球运动不越界。
最后在RUN()方法里面调用move()使小球移动。注意添加休眠,否则小球会移动过快。
总结需要注意的事项:
千万不要在几个类里面传参数传来传去传得乱七八糟!!!OTL。
还有在自动生成setter和getter的时候要有选择地去生成。
界面类
package 线程;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GameUI extends JFrame {
public void initUI() {
this.setTitle("我大木叶村的基情对对碰");// 设置窗体标题
this.setSize(500, 400);// 设置窗体大小
this.setDefaultCloseOperation(3);// 设置关闭
this.setLocationRelativeTo(null);// 设置窗体居中
this.setLayout(new FlowLayout());// 设置窗体布局
JButton jb1 = new JButton("添加");//添加按钮
JButton jb2 = new JButton("暂停");
JButton jb3 = new JButton("继续");
JButton jb4 = new JButton("停止");
this.add(jb1);//把按钮加到面板上
this.add(jb2);
this.add(jb3);
this.add(jb4);
ButtonListener l = new ButtonListener(this);//给按钮加监听器
jb1.addActionListener(l);
jb2.addActionListener(l);
jb3.addActionListener(l);
jb4.addActionListener(l);
this.setVisible(true);// 设置窗体可见
}
// 重绘方法
public void paint(Graphics g) {
super.paint(g);
BufferedImage ima = new BufferedImage(500, 400,
BufferedImage.TYPE_4BYTE_ABGR);// 创建缓冲图片,大小是500*400,32位真彩色
Graphics ig = ima.getGraphics();// 获得一个画笔对象
Graphics2D g2 = (Graphics2D) ig;//强制转型
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);//去锯齿
ig.setColor(Color.PINK);// 设置画笔颜色
ig.drawRect(0, 0, 500, 400);// 绘制举行
for (int k = 0; k < ButtonListener.gtv.size(); k++) {
GameThread gt = ButtonListener.gtv.get(k);
Color color = gt.getColor();
for (int i = 0; i < gt.getR(); i++) {//逐层画立体圆
ig.setColor(color);
ig.fillOval(gt.getX() - (gt.getR() - i), gt.getY()
- (gt.getR() - i), (gt.getR() - i) * 2,
(gt.getR() - i) * 2);
}
}
g.drawImage(ima, 0, 0, this);//绘制背景
}
// 让颜色渐变
public Color setColor(Color color, int i) {
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int re = red + i;
int gr = green + i;
int bl = blue + i;
if (re > 255)//处理RGB值越界的情况
re = 255;
if (gr > 255)
gr = 255;
if (bl > 255)
bl = 255;
return new Color(re, gr, bl);
}
public static void main(String[] args) {
GameUI gam = new GameUI();
gam.initUI();
}
}
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GameUI extends JFrame {
public void initUI() {
this.setTitle("我大木叶村的基情对对碰");// 设置窗体标题
this.setSize(500, 400);// 设置窗体大小
this.setDefaultCloseOperation(3);// 设置关闭
this.setLocationRelativeTo(null);// 设置窗体居中
this.setLayout(new FlowLayout());// 设置窗体布局
JButton jb1 = new JButton("添加");//添加按钮
JButton jb2 = new JButton("暂停");
JButton jb3 = new JButton("继续");
JButton jb4 = new JButton("停止");
this.add(jb1);//把按钮加到面板上
this.add(jb2);
this.add(jb3);
this.add(jb4);
ButtonListener l = new ButtonListener(this);//给按钮加监听器
jb1.addActionListener(l);
jb2.addActionListener(l);
jb3.addActionListener(l);
jb4.addActionListener(l);
this.setVisible(true);// 设置窗体可见
}
// 重绘方法
public void paint(Graphics g) {
super.paint(g);
BufferedImage ima = new BufferedImage(500, 400,
BufferedImage.TYPE_4BYTE_ABGR);// 创建缓冲图片,大小是500*400,32位真彩色
Graphics ig = ima.getGraphics();// 获得一个画笔对象
Graphics2D g2 = (Graphics2D) ig;//强制转型
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);//去锯齿
ig.setColor(Color.PINK);// 设置画笔颜色
ig.drawRect(0, 0, 500, 400);// 绘制举行
for (int k = 0; k < ButtonListener.gtv.size(); k++) {
GameThread gt = ButtonListener.gtv.get(k);
Color color = gt.getColor();
for (int i = 0; i < gt.getR(); i++) {//逐层画立体圆
ig.setColor(color);
ig.fillOval(gt.getX() - (gt.getR() - i), gt.getY()
- (gt.getR() - i), (gt.getR() - i) * 2,
(gt.getR() - i) * 2);
}
}
g.drawImage(ima, 0, 0, this);//绘制背景
}
// 让颜色渐变
public Color setColor(Color color, int i) {
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int re = red + i;
int gr = green + i;
int bl = blue + i;
if (re > 255)//处理RGB值越界的情况
re = 255;
if (gr > 255)
gr = 255;
if (bl > 255)
bl = 255;
return new Color(re, gr, bl);
}
public static void main(String[] args) {
GameUI gam = new GameUI();
gam.initUI();
}
}
监听器类
写道
package 线程;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class ButtonListener implements ActionListener {
public static Vector<GameThread> gtv = new Vector<GameThread>();
private GameUI jf;//接收传递过来的窗体
public ButtonListener(GameUI jf){//构造方法
this.jf = jf;
}
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();//获取按钮内容
if (text.equals("添加")) {
GameThread game = new GameThread(jf);//创建新线程
gtv.add(game);//添加到可变长的向量中
game.start();//启动线程
} else if (text.equals("暂停")) {
for (GameThread gt : gtv) {
gt.setPauseflag(false);//把暂停置为FALSE
}
} else if (text.equals("继续")) {
for (GameThread gt : gtv) {
gt.setPauseflag(true);//把暂停置为TRUE
}
} else if (text.equals("停止")) {
for (GameThread gt : gtv) {
gt.setPauseflag(false);//使程序停止
gt.setStopflag(false);
gtv.remove(gt);//移除线程
}
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class ButtonListener implements ActionListener {
public static Vector<GameThread> gtv = new Vector<GameThread>();
private GameUI jf;//接收传递过来的窗体
public ButtonListener(GameUI jf){//构造方法
this.jf = jf;
}
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();//获取按钮内容
if (text.equals("添加")) {
GameThread game = new GameThread(jf);//创建新线程
gtv.add(game);//添加到可变长的向量中
game.start();//启动线程
} else if (text.equals("暂停")) {
for (GameThread gt : gtv) {
gt.setPauseflag(false);//把暂停置为FALSE
}
} else if (text.equals("继续")) {
for (GameThread gt : gtv) {
gt.setPauseflag(true);//把暂停置为TRUE
}
} else if (text.equals("停止")) {
for (GameThread gt : gtv) {
gt.setPauseflag(false);//使程序停止
gt.setStopflag(false);
gtv.remove(gt);//移除线程
}
}
}
}
线程类
写道
package 线程;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import java.util.Vector;
import javax.swing.JFrame;
public class GameThread extends Thread {
private int x = 1, y = 1;// 给横纵坐标初始值
private int movex = 1, movey = 1;// 每次坐标改变的值得大小
private GameUI jf;
private int r;//小球半径
private int width = 500;
private int height = 400;
private Color color;
private boolean pauseflag = true;//暂停标识
private boolean stopflag = true;//停止标志
// 以上是一堆自己生成的函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public GameThread(GameUI jf) {// 构造方法传递窗体
this.jf = jf;
init();
}
public int getR() {
return r;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public boolean isPauseflag() {
return pauseflag;
}
public void setPauseflag(boolean pauseflag) {
this.pauseflag = pauseflag;
}
public boolean isStopflag() {
return stopflag;
}
public void setStopflag(boolean stopflag) {
this.stopflag = stopflag;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Color getColor(){
return color;
}
public void init() {
Random rand = new Random();//随机生成小球的左上角横纵坐标半径移动速度和颜色
x = rand.nextInt(20) + 10;
y = rand.nextInt(20) + 10;
r = rand.nextInt(20) + 10;
movex = rand.nextInt(20) + 1;
movey = rand.nextInt(20) + 1;
color = new Color(rand.nextInt(256), rand.nextInt(256),
rand.nextInt(256));
}
public void move() {//不让小球越界
if (y > jf.getHeight() - r)
movey = -movey;
if (x > jf.getWidth() - r)
movex = -movex;
if (x < 1)
movex = -movex;
if (y < 1)
movey = -movey;
x += movex;// 改变坐标使小球移动
y += movey;
System.out.println(x+" "+y+" "+r);
}
public void run() {// 重写RUN方法
while (stopflag) {// 使小球一直移动
System.out.print(stopflag);
// 使小球碰到边弹回
while (pauseflag) {
System.out.print(pauseflag);
try {
move();
// 调用重绘方法
jf.paint(jf.getGraphics());
Thread.sleep(20);// 线程休眠20毫秒
} catch (Exception e) {// 抛出异常
e.printStackTrace();
}
}
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import java.util.Vector;
import javax.swing.JFrame;
public class GameThread extends Thread {
private int x = 1, y = 1;// 给横纵坐标初始值
private int movex = 1, movey = 1;// 每次坐标改变的值得大小
private GameUI jf;
private int r;//小球半径
private int width = 500;
private int height = 400;
private Color color;
private boolean pauseflag = true;//暂停标识
private boolean stopflag = true;//停止标志
// 以上是一堆自己生成的函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public GameThread(GameUI jf) {// 构造方法传递窗体
this.jf = jf;
init();
}
public int getR() {
return r;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public boolean isPauseflag() {
return pauseflag;
}
public void setPauseflag(boolean pauseflag) {
this.pauseflag = pauseflag;
}
public boolean isStopflag() {
return stopflag;
}
public void setStopflag(boolean stopflag) {
this.stopflag = stopflag;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Color getColor(){
return color;
}
public void init() {
Random rand = new Random();//随机生成小球的左上角横纵坐标半径移动速度和颜色
x = rand.nextInt(20) + 10;
y = rand.nextInt(20) + 10;
r = rand.nextInt(20) + 10;
movex = rand.nextInt(20) + 1;
movey = rand.nextInt(20) + 1;
color = new Color(rand.nextInt(256), rand.nextInt(256),
rand.nextInt(256));
}
public void move() {//不让小球越界
if (y > jf.getHeight() - r)
movey = -movey;
if (x > jf.getWidth() - r)
movex = -movex;
if (x < 1)
movex = -movex;
if (y < 1)
movey = -movey;
x += movex;// 改变坐标使小球移动
y += movey;
System.out.println(x+" "+y+" "+r);
}
public void run() {// 重写RUN方法
while (stopflag) {// 使小球一直移动
System.out.print(stopflag);
// 使小球碰到边弹回
while (pauseflag) {
System.out.print(pauseflag);
try {
move();
// 调用重绘方法
jf.paint(jf.getGraphics());
Thread.sleep(20);// 线程休眠20毫秒
} catch (Exception e) {// 抛出异常
e.printStackTrace();
}
}
}
}
}