public class BelishaBeacon {
public class Design extends JPanel {
private boolean alternateColors = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(163, 180, 16, 45);
Rectangle box2 = new Rectangle(163, 225, 16, 45);
Rectangle box3 = new Rectangle(163, 270, 16, 45);
Rectangle box4 = new Rectangle(163, 315, 16, 45);
Rectangle box5 = new Rectangle(163, 360, 16, 45);
Rectangle box6 = new Rectangle(163, 405, 16, 45);
//drawing the shapes
Ellipse2D.Double ball = new Ellipse2D.Double(a, b, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.YELLOW);
g2.fill(ball);
if (alternateColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(a, b, 100, 100));
}
alternateColors = false;
}
public void alternateColors() {
alternateColors = true;
repaint();
}
}
public BelishaBeacon() {
//frame
JFrame frame = new JFrame();
frame.setSize(330, 550);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Design shapes = new Design();
JButton jbtFlash = new JButton("Flash");
jbtFlash.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Runnable r = new Runnable(){
@Override
public void run(){
while(/* user stops / toggleButton state*/ true)
{
swapColors(); // some method using static boolean
try{
Thread.sleep(500);
}catch(Exception e){}
}
}
private void swapColors() {
boolean swapColors;
Graphics g2;
if (swapColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(a, b, 100, 100));
} else {
g2.setColor(Color.YELLOW);
g2.fill(new Ellipse2D.Double(a, b, 100, 100));
}
}
};
Thread t = new Thread(r);
t.start();
}});
JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapes.alternateColors();
}
});
I have created an action listener for my jbutton steady and flash, I am just trying to created a method for swapColors for it to be initialised in the jbutton flash. The swapcolos method initially should alternate between the colours orange and grey
if (alternateColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(a, b, 100, 100));
}
alternateColors = false;
Should be:
if (alternateColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(a, b, 100, 100));
} else {
g2.setColor(Color.YELLOW);
g2.fill(new Ellipse2D.Double(a, b, 100, 100));
}
AND
public void alternateColors() {
alternateColors = true;
repaint();
}
Should be:
public void alternateColors() {
//Flip boolean alternateColors
alternateColors = !alternateColors;
repaint();
}
You wrote that you want it to flash every 0.5 seconds.
To do so you need to start new thread for example
jbtFlash.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Runnable r = new Runnable(){
@Override
public void run(){
while(/* user stops / toggleButton state*/ true)
{
swapColors(); // some method using static boolean
try{
Thread.sleep(500);
}catch(Exception e){}
}
}
};
Thread t = new Thread(r);
t.start();
}});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.