I’m trying to learn how to draw a shape, and be able to a) draw it, “freeze” the process, draw it in the color of the background, and then re-draw it in the original color and b) draw a shape and change its color. All I have so far is (for blinking):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Carlight extends JPanel{
Thread th=new Thread();
public void paintComponent (Graphics g){
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillOval(25, 25, 10, 10);
try{
th.sleep(10);
}catch(InterruptedException e){}
repaint();
g.setColor(Color.yellow);
g.fillOval(25, 25, 10, 10);
try{
th.sleep(10);
}catch(InterruptedException e){}
repaint();
g.setColor(Color.yellow);
g.fillOval(25, 25, 10, 10);
}
public Carlight(){
JFrame frame=new JFrame();
frame.setTitle("Carlights");
frame.add(this);
frame.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,150);
frame.setVisible(true);
}
public static void main(String[] args){
new Carlight();
}
}
How do I make this code work and how do I get a shape to change color?
Ok,
forcing the UI to hang/stall. This is very bad.