RDV+DIAL chat illimit� dans ta r�g!
no save
Assistance
Achat
News

Forum | programmation
JProgressBar java
nico, le jeu. 10 août 2006 à 14:32:29
merci !
En fait j'ai deja commencé a bosser dessus, mais sans passer pas SwingWorker !
J'ai un probleme lié a l'EDT et au fait que j'utilise des fonctions de dessins ... je met le code au cas ou si quelqu'un aurait une solution pour le faire marcher correctement !


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.repaint();
}
});
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));
}
}



}
PrécédentHackTrack
août 06


REPONSES
HackTrack
août 06
nico
août 06
Version Web
Réalisé par RedShift
no save