Boa Tarde a todos,
Eu tenho que fazer um trabalho que consiste em executar 150 a 200 threads que pintem pixels na tela de forma aleatória…
eu achei um cógido dei umas alteradas mas estou com um grande problema. Ele só da start em uma thread…
Estou completamente perdido, se alguém puder me ajudar.
Muito obrigado
[code]import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Teste extends JComponent implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int x=0,y=0,dx=15,dy=10;
private Color cor = null;
public Color getCor() {
return cor;
}
public void setCor(Color cor) {
this.cor = cor;
}
public Teste() {
Random aux = new Random();
int number = 150 + aux.nextInt(50);
for (int k = 0; k < number; k++) {
Timer t = new Timer(50, this);
if ((number % 2) == 0){
this.setCor(Color.white);
}else
this.setCor(Color.blue);
t.start();
}
}
protected void paintComponent(Graphics g) {
Dimension sz = getSize();
g.setColor(this.getCor());
g.fillOval(x,y,5,5);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
repaint();
}
public void actionPerformed(ActionEvent evt) {
Random aux = new Random();
x = aux.nextInt(500);
y = aux.nextInt(500);
repaint();
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().setLayout(new GridLayout(1,1));
f.getContentPane().add(new Teste());
f.setSize(800,800);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
} [/code]