Changing button colors in a loop

Discussion in 'Java' started by No spam, Mar 4, 2005.

  1. No spam

    No spam 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;
    }

    }
     
    No spam, Mar 4, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randy

    TextBox changing colors

    Randy, Oct 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    372
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 26, 2005
  2. Anders Christiansen

    Changing colors

    Anders Christiansen, Mar 3, 2004, in forum: HTML
    Replies:
    8
    Views:
    496
    Ryan Stewart
    Mar 3, 2004
  3. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    554
    darrel
    Aug 7, 2008
  4. ZelluX
    Replies:
    3
    Views:
    1,920
    Peter Otten
    Dec 1, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page