Random

+2 Developer John · October 25, 2014
How can I use Java Random to loop through 6 variables?
For an example: 
I have a Gui windows, and I want it to choose one color out of the six declared in Java Random.
-Thanks!

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Developer John · November 5, 2014
Nope, the error persists.
0 Developer John · November 5, 2014
W8 m8, I think I would have to make 4 ChangeColor classes so they all don't @Override.
0 Developer John · November 5, 2014
Ok so I tried to make a Grid Layout of 4 buttons:

import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Color2 extends JFrame{

Color[] myColors = {Color.ORANGE, Color.RED, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.PINK};
Random rand = new Random();
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
JPanel panel = new JPanel();

//Constructor. Create this first and test it each step of the way. 
public Color2() {
super("Button1 displays a random color");
setLayout(new GridLayout(4, 4, 0, 0));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new changeColor()); //When I add the action listener I'm creating a new changeColor class and that calls the action performed. Don't add this at first. Do it later once you've tested your program is working as expected.
button2.addActionListener(new changeColor());
button3.addActionListener(new changeColor());
button4.addActionListener(new changeColor());

add(button1);
add(button2);
add(button3);
add(button4);

button1.setBackground(setColor());
button2.setBackground(setColor());
button3.setBackground(setColor());
button4.setBackground(setColor());

}

//Create this second and add it to your constructor
public Color setColor(){
return myColors[rand.nextInt(6)];
}

//Finally add the action listener and test. 
class changeColor implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
button1.setBackground(setColor());
button2.setBackground(setColor());
button3.setBackground(setColor());
button4.setBackground(setColor());
}
}
}

For some reason, if I click one button it changes all the button's colors.
+1 Alex Sweps · October 31, 2014
Yeha I meant type mine out so your fingers are coding and you can try and understand whats going as you type each statement. Thats what I meant by exact. The code I wrote will change to a random color when you click the button. Type it exact and ask me anything you dont understand. 

And keep it in your saved projects so you can refer to it when you need an action handler again. 
0 Developer John · October 30, 2014
Do you even know what I want this program to do?
I want it to display a random color when I click on the Button1 button. If I change the code, all it does is add errors to it. I'm basically to the point where I don't even know what I'm doing anymore.
0 Developer John · October 30, 2014
How is Button1 already added to HandlerClass3?
0 Developer John · October 30, 2014
I was trying to stay away from copy and pasting code. I just don't feel like people really learn anything when they do that.
0 Alex Sweps · October 30, 2014
Copy mine exact. You dont need to implement ActionListener in your HandlerClass3 because the inner class is implementing it. (class changeColor) 

you have public class changeColor. It should be class changeColor with no public infront. Remove that public class button1 alltogether because the button is added to the frame in the HandlerClass3.

Copy it exact and then go through each line and try and work out how it works. Youv got the right idea but your still missing some of the more tricky concepts.  Feel free to ask me any more questions.
0 Developer John · October 30, 2014

public class HandlerClass3 implements ActionListener{
        public void actionPerformed(ActionEvent event){
       
        Random random = new Random();
Color[] colors = {Color.ORANGE, Color.RED, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.BLUE};
       
        Button1 but1 = new Button1();
                but1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                but1.setSize(400,400);
                but1.setVisible(true);
                but1.addActionListener(new changeColor());
                but1.setBackground(setColor());
            }
        }
        
        public Color setColor(){
       
        return colors[random.nextInt(5)];
       
        }


        public class changeColor implements ActionListener{
        @Override
    public void actionPerformed(ActionEvent arg0) {
    but1.setBackground(setColor());
   
        }
        }
       
        public class Button1 extends JFrame{
            public Button1(){
                super("Button1 displays a random color");
                setLayout(new FlowLayout());
            }
        }
                
0 Alex Sweps · October 30, 2014
Post your code.

Java / Android Development

107,207 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator