Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Changing button colors in a loop

Thread Tools

Changing button colors in a loop

No spam
Posts: n/a
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
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()

JFrame frame = new JFrame("Elevator3");

setFont(new Font("SansSerif", Font.PLAIN, Math.round(n/3)));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(width, n*(floors+2) );

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;
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;
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)


//draw buttons
for (int i=0; i < buttons.length; i++)
g.fillOval(buttons[i].getX(), buttons[i].getY(),
buttons[i].getWidth(), buttons[i].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;

c = Color.LIGHT_GRAY;


public Color getColor()
return c;

public int getX()
return x;

public int getY()
return y;

public int getWidth()
return width;


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Is there any library that can convert RGB colors to ANSI colors? ZelluX Python 3 12-01-2008 11:08 AM
TreeNode colors come from anchor colors AAaron123 ASP .Net 1 08-07-2008 07:56 PM
Radio Button Loop inside Form Element Loop wreed Javascript 9 10-16-2006 01:39 PM
TextBox changing colors Randy ASP .Net 1 10-26-2005 06:41 PM