62,621
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
public class BufferedImageGUI extends JFrame implements Runnable {
private volatile BufferedImage boardDrawing;
private volatile BufferedImage boardDisplaying;
/**
* 窗体构造函数
*/
public BufferedImageGUI() {
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
boardDrawing = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
boardDisplaying = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
}
public void paint(Graphics g) {
g.drawImage(boardDisplaying, 0, 0, null);
}
public void run() {
Random rand = new Random();
while (true) {
// 复杂的绘制过程
Graphics g = boardDrawing.getGraphics();
g.clearRect(0, 0, boardDrawing.getWidth(), boardDrawing.getHeight());
for (int i = 0; i < 500; i++) {
g.setColor(new Color(rand.nextInt()));
g.drawString(String.valueOf(rand.nextInt()), rand.nextInt(800), rand.nextInt(600));
}
// 切换前景与背景工作区
BufferedImage tmp = boardDisplaying;
boardDisplaying = boardDrawing;
boardDrawing = tmp;
this.repaint();
// Sleep
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 启动主函数
*/
public static void main(String[] args) {
BufferedImageGUI wnd = new BufferedImageGUI();
wnd.setVisible(true);
new Thread(wnd).start();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MultiThreadDraw extends JFrame {
/**
* 窗体构造函数
*/
public MultiThreadDraw() {
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton btnStartThreads = new JButton("StartManyThread");
btnStartThreads.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Graphics g = ((JComponent) e.getSource()).getParent().getGraphics();
System.out.println(g.getClass());
for (int i = 0; i < 50; i++) {
new DrawWorker(g).start();
}
System.out.println("Thread started...");
}
});
this.getContentPane().add(btnStartThreads);
}
/**
* 启动主函数
*/
public static void main(String[] args) {
MultiThreadDraw wnd = new MultiThreadDraw();
wnd.setVisible(true);
}
private class DrawWorker extends Thread {
private Graphics g;
public DrawWorker(Graphics g) {
this.g = g;
}
public void run() {
Random rand = new Random();
while (true) {
g.setColor(new Color(rand.nextInt()));
g.drawString(String.valueOf(rand.nextInt()), rand.nextInt(800), rand.nextInt(600));
g.drawLine(rand.nextInt(800), rand.nextInt(600), rand.nextInt(800), rand.nextInt(600));
}
}
}
}
public class TestInvokeLater extends JFrame {
/**
* 负责绘图
*/
private Runnable doBusyDraw = new Runnable() {
public void run() {
try {
Thread.sleep(5000); // 每次绘图执行花 5 秒
} catch (InterruptedException e) {
}
System.out.println("BusyDraw done.");
}
};
/**
* 负责定时刷新
*/
private Thread refresher = new Thread() {
public void run() {
try {
while (true) {
SwingUtilities.invokeLater(doBusyDraw);
Thread.sleep(1000); // 每秒执行 1 次绘图
}
} catch (InterruptedException e) {
}
}
};
public TestInvokeLater() {
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton btnBusyDraw = new JButton("StartBusyDraw");
btnBusyDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
refresher.start();
System.out.println("Thread started...");
}
});
this.add(btnBusyDraw);
}
/**
* 启动主函数
*/
public static void main(String[] args) {
TestInvokeLater wnd = new TestInvokeLater();
wnd.setVisible(true);
}
}