2

I have the following code which is a JFrame for 25 buttons which each button needs to be assigned randomly a color from the color array. I just put in an array for colors and I am getting an error.

import java.awt.event.*; // Needed for ActionListener and ActionEvent
import javax.swing.*; // Needed for JFrame and JButton
import java.awt.Color;
import java.awt.Graphics;

public class ColorToggleGui extends JFrame implements ActionListener {

  // This stores all buttons
  JButton[][] buttons;
  //Stores colors
  Color[] colors;

  public ColorToggleGui(String title) {
    super(title);
    setLayout(null);

    //Allocate the size of the array
    colors = new Color[4];

        //Initialize the values of the array
    colors[0] = new Color(Color.red);
    colors[1] = new Color(Color.blue);
    colors[2] = new Color(Color.yellow);
    colors[3] = new Color(Color.green);


    buttons = new JButton[5][5];
    String[] buttonLabels = { "", "", "", "", "", "", "", "", "", "", "","","","","","","","","","","","","","","" };
    for(int row=0; row<5; row++) {
      for (int col=0; col<5; col++) {
        buttons[row][col] = new JButton(buttonLabels[row*3+col]);
        buttons[row][col].setLocation(10+col*55, 10+row*55);
        buttons[row][col].setSize(50,50);
        buttons[row][col].addActionListener(this);
        add(buttons[row][col]);
      }
    }
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(300,450);
  }


  // This is the single event handler for all the buttons
  public void actionPerformed(ActionEvent e) {
    System.out.println("Button " + e.getActionCommand() + " was pressed." );
  }

  public static void main(String args[]) {
    ColorToggleGui frame = new ColorToggleGui("Color Toggle");
    frame.setVisible(true);
  }

}

The part with the color array I get this error :

Error: incompatible types: java.awt.Color cannot be converted to int

Why is the error happening? I am also in need of help with a way to use the color array to randomly assign each button with a color from the color array? is there a way I can do this?

gyackuet
  • 21
  • 2

3 Answers3

1

use Color.red without new Color()

colors[0] = Color.red;

use new constructor if you want to make color from rgb values like

colors[1] = new Color(100,100,100);

Color constructor has overloaded constructor which accept one integer as a parameter .

public Color(int i) {
        // compiled code
}

that's why you get error

Error: incompatible types: java.awt.Color cannot be converted to int

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
1

The constructor for Java Color with a single argument is:

Color (int rgb)

However, what you did here:

colors[0] = new Color(Color.red);

You are providing a Color object instead of an int, hence receiving the error java.awt.Color cannot be converted to int.


You can simply do this:

colors[0] = Color.RED;

Note: It is preferred to use Color.RED instead of Color.red.

user3437460
  • 17,253
  • 15
  • 58
  • 106
0

As you asked here before and no one close it:

I give you one good answer to both questions, and is here: https://stackoverflow.com/a/36004026/982161

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97