Olá Pessoal
Tenho uma janela Swing, uma imagem do tipo ImageIcon e dentro desta imagem preciso criar alguns círculos. Até aí tudo bem, o problema é que preciso, através de threads, criar primeiro 1 circulo e depois de alguns segundos (posso usar sleep ou wait) outro circulo em outra posição da imagem. Ja tentei de várias maneiras , procurei no google mas tenho muita dificuldade com Threads. Alguém pode me ajudar
Muito Obrigado…
Segue código
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Exemplo extends JFrame implements ActionListener {
private JButton botaoCriar;
private JButton botaoFechar;
private JLabel labelImagem;
private Graphics ig;
Graphics g;
private Image img;
private JPanel painelCentral;
private JPanel painelSul;
public Exemplo() {
super("Animação Darley");
setSize(400, 400);
setLocation(300, 150);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// Área de conteúdo
Container conteudo = getContentPane();
// Adicionando Painel Sul
painelSul = new JPanel(new FlowLayout(FlowLayout.RIGHT));
painelCentral = new JPanel(new FlowLayout(FlowLayout.CENTER));
conteudo.add(painelCentral, BorderLayout.CENTER);
conteudo.add(painelSul, BorderLayout.SOUTH);
botaoCriar = new JButton("Criar");
painelSul.add(botaoCriar);
botaoFechar = new JButton("Fechar");
painelSul.add(botaoFechar);
botaoFechar.addActionListener(this);
labelImagem = new JLabel(criarImagem());
painelCentral.add(labelImagem);
}
//Metodo para criar Imagem
private static ImageIcon criarImagem() {
int width=300, height=300;
BufferedImage buffer = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
Graphics g = buffer.createGraphics();
g.setColor( Color.black );
g.fillRect( 300, 300, 300, 300 );
g.setColor( Color.green );
//g.fillOval(Horizontal, Vertical, Horizontal, Vertical);
// Primeiro Circulo
g.fillOval(30, 30, 15, 15);
// Segundo Círculo
g.fillOval(45, 45, 15, 15);
return new ImageIcon( buffer );
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == botaoFechar) {
int resposta = JOptionPane.showConfirmDialog(null,
"TEM CERTEZA QUE DESEJA SAIR ?");
if (resposta == 0) {
System.exit(0);
}
}
}
}