Changing button colors in a loop

G

Guest

Hi all:
I am working on a task that should imitate an elevator. I have two vertical
rows of round buttons "Up" and "Down" When a circle is selected randomly by
the program, the circle becomes yellow and the elevator moves to that
button.
Here is what I did:
1. created a class Circle where I save buttons' parameters
2. saved Circle objects in an array
3. drew the buttons depending on their parameters
4. generated a random number, matched it with an array index and assigned
the object color to yellow.

Everything is fine except that I can't figure out how to change colors of my
buttons in a loop. Help would be greatly appreciated!

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Elevator3 extends JPanel
{
private int n = 40;
private int width = 200;
private int floors = 10;
private int interval = 1000;
private boolean selected = false;
private Circle[] buttons = new Circle[2*(floors-1)];

public Elevator3()
{
build();

JFrame frame = new JFrame("Elevator3");

setBackground(Color.WHITE);
setFont(new Font("SansSerif", Font.PLAIN, Math.round(n/3)));
frame.getContentPane().add(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, n*(floors+2) );
frame.setVisible(true);
}

public void build()
{
Random r = new Random();
int buttonPick;

int timeUntilNextButton = r.nextInt(interval);

for (int k =0; ; k++)
{
if (timeUntilNextButton-- ==0)
{
buttonPick = r.nextInt(2*(floors-1));

//populate my buttons array here - how??

timeUntilNextButton = r.nextInt(interval);

}

}


//adding "Down" buttons
for (int i=1, count=0; i < floors; i++, count++)
{
if (count == buttonPick)
selected = true;
else
selected = false;

buttons[count]= new Circle(n*2, n*i, selected, Math.round(n/2));
}

//build an array of "Up" circles
for (int i=2, count=floors-1; i < floors+1; i++, count++)
{
if (count == buttonPick)
selected = true;
else
selected = false;

buttons[count]= new Circle(n, n*i, selected, Math.round(n/2));
}

}


public static void main(String[] args)

{
new Elevator3();

}

protected void paintComponent(Graphics g)

{
super.paintComponent(g);

//draw buttons
for (int i=0; i < buttons.length; i++)
{
g.setColor(buttons.getColor());
g.fillOval(buttons.getX(), buttons.getY(),
buttons.getWidth(), buttons.getWidth());
}


}


}


class Circle
{
private int x;
private int y;
private Color c;
private boolean pressed;
private int width;

public Circle(int xCoordinate, int yCoordinate, boolean selected,
int diameter)
{
x = xCoordinate;
y = yCoordinate;
pressed = selected;
width = diameter;

if (pressed)
c = Color.YELLOW;

else
c = Color.LIGHT_GRAY;

}


public Color getColor()
{
return c;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public int getWidth()
{
return width;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top