1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class TestProgressBar extends JFrame implements ActionListener {
private JProgressBar jj;
private DrawClass dc;
private JButton b1;
private final static int MAX_POINTS = 100;
public TestProgressBar() {
super();
jj = new JProgressBar(0, MAX_POINTS);
b1 = new JButton("Start");
b1.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(b1);
dc=new DrawClass(jj);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(buttonPanel, BorderLayout.WEST);
getContentPane().add(dc, BorderLayout.CENTER);
getContentPane().add(jj, BorderLayout.SOUTH);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestProgressBar dialog = new TestProgressBar();
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog.setSize(500,500);
dialog.setVisible(true);
}
});
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == b1) {
b1.setEnabled(false);
dc.setAction();
jj.setValue(0);
Thread t = new Thread(new Runnable() {
public void run() {
dc.generatePoints();
}
});
t.start();
}
}
class DrawClass extends JPanel{
private JProgressBar jpb;
private boolean action=false;
Graphics2D g2;
public DrawClass(JProgressBar j){
jpb=j;
}
private void printPoint(final Point p, final Color c) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
g2.setColor(c);
Rectangle2D.Float rd = new Rectangle2D.Float(p.x,p.y,5,5);
g2.fill(rd);
System.out.println("point add: " + p);
}
});
}
public void setAction(){
action=true;
}
public void paint(Graphics g){
g2 =(Graphics2D)g;
if (action){
g2.clearRect(0,0,getSize().width,getSize().height);
generatePoints();
}
else
g2.clearRect(0,0,getSize().width,getSize().height);
}
private Random r = new Random();
private void generatePoints() {
for (int i = 0; i < MAX_POINTS; i++) {
Point p = randomPoint();
Color c = randomColor();
printPoint(p,c);
updateBar(i+1);
System.out.println("n:" + i);
if (!SwingUtilities.isEventDispatchThread()) {
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("generate Point from EDT");
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
b1.setEnabled(true);
}
});
}
private void updateBar(final int i) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jpb.setValue(i);
jpb.repaint();
System.out.println("bar updated: " + i);
}
});
}
private Point randomPoint() {
return new Point(r.nextInt(getSize().width), r.nextInt(getSize().height));
}
private Color randomColor() {
return new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
}
}
} |
Partager