Results 1 to 6 of 6
  1. #1
    Cutter is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default One controller with multiple buttons and colors

    Hi all,

    Right now I have an array of colors cycling in the "North" button, with the user clicking the controller "Center" Button.

    Instead...

    I'd like to have the central controller button, and have the colors cycle round the North,South,East and West buttons one step for each press of the controller "Center" button.

    I'm getting frustrated and wonder if I could I have a bit of help please and thanks .


    Java Code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ButtonColor extends JFrame implements ActionListener
    {
    private JButton center;
    private JButton north;
    private JButton south;
    private JButton east;
    private JButton west;
    private Color[] colors = new Color[] {Color.red, Color.orange, Color.yellow, Color.green, Color.blue, new Color(138, 43, 226)};
    private int index;
    
    public ButtonColor()
    {
    initialize();
    }
    
    private void initialize()
    {
    index = 0;
    
    north = new JButton("North");
    center = new JButton("CLick Me");
    south = new JButton("South");
    east = new JButton("East");
    west = new JButton("West");
    
    center.addActionListener(this);
    setLayout(new BorderLayout());
    add(center, BorderLayout.CENTER);
    add(north, BorderLayout.NORTH);
    add(south, BorderLayout.SOUTH);
    add(east, BorderLayout.EAST);
    add(west, BorderLayout.WEST);
    
    }
    
    public void actionPerformed(ActionEvent e)
    {
    if(index < (colors.length - 1))
    {
    index++;
    }
    else
    {
    index = 0;
    }
    north.setBackground(colors[index]);
    
    }
    
    
    
    public static void main(String[] p)
    {
    ButtonColor thisFrame = new ButtonColor();
    thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisFrame.setSize(200,200);
    thisFrame.setVisible(true);
    }
    }

    Cutter

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd put the compass buttons into an array and use the two arrays (buttons and colors) to help you cycle through the colors.

    In your actionPerformed, have a for loop that cycles through the button array. You'll need to use another int variable, say "j" that = index + i (the for loop index), and then you'll need to make sure that j isn't larger than colors.length and use this to set each button's background color. Play with it and you'll figure it out, I'm sure!

  3. #3
    Cutter is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Red face

    Thanks for the direction Fubarable :)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ButtonColor extends JFrame implements ActionListener
    {
    private JButton center;
    private JButton north;
    private JButton south;
    private JButton east;
    private JButton west;
    private Color[] colors = new Color[] {Color.red, Color.orange, Color.yellow, Color.green};
    private JButton button[]=new JButton[5];
    private int index;
    private int i;
    
    
    public ButtonColor()
    {
    initialize();
    }
    
    private void initialize()
    {
    index = 0;
    
    north = new JButton("North");
    center = new JButton("CLick Me");
    south = new JButton("South");
    east = new JButton("East");
    west = new JButton("West");
    
    center.addActionListener(this);
    setLayout(new BorderLayout());
    add(center, BorderLayout.CENTER);
    add(north, BorderLayout.NORTH);
    add(south, BorderLayout.SOUTH);
    add(east, BorderLayout.EAST);
    add(west, BorderLayout.WEST);
    
    }
    
    public void actionPerformed(ActionEvent e)
    {
    if(index <= (colors.length-1))
    {
    index++;
    }
    else
    {
    index = 0;
    }
    
    
    
    for (int j=index+i; j < colors.length;j++ )
    north.setBackground(colors[index]);
    east.setBackground(colors[index-1]);
    south.setBackground(colors[index-2]);
    west.setBackground(colors[index-3]);
    
    
    
    
    }
    public static void main(String[] p)
    {
    ButtonColor thisFrame = new ButtonColor();
    thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisFrame.setSize(200,200);
    thisFrame.setVisible(true);
    }
    }

    seems I'm almost there...

    One color seems to cycle, but I'm not getting something...probably right there in front of me.

    a hint on what I'm doing wrong please...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, so you create a JButton array,... but never use it. Whatever, I guess it's your choice.

    Then your current program is not working but you're not telling us how. If you are getting error messages, they will tell you what is wrong (hint, hint). Listen to them. Also step through your program mentally as you look at the code. Imagine what steps happen with each button press, and you'll also see your bug. It's mainly a basic math error, and again the error messages (which you should show us) will tell you where it's occurring and which array is blowing up.

    But again for my money, a button array is easiest. The actionPerformed would be simplified greatly...
    Again, I would create a button array that holds only the 4 compass buttons (length of 4, not 5),
    Java Code:
      public void actionPerformed(ActionEvent e) {
        index++;
        index %= colors.length;
        
        for (int i = 0; i < compassBtns.length; i++) {
          int j = //... you should be able to figure this out
          j %= //... likewise;
          compassBtns[i].setBackground(colors[j]);
        }
        
      }
    Last edited by Fubarable; 01-23-2010 at 07:41 PM.

  5. #5
    Cutter is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    Your right, math is not my strong suit at all...

    some aspects I find quite easy, others I do not.

    I'm still working through it...but here is the error message

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1



    Thanks for the hints.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. How do I use multiple background colors?
    By trbox in forum New To Java
    Replies: 1
    Last Post: 09-02-2008, 02:00 PM
  2. how to set multiple colors in a JtextArea
    By valery in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2007, 06:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •