I'm trying to make an interactive grid but the colors won't change when I click on the squares. The grid has to be multicolored and when one clicks on the grid, the boxes inside of it should change color to a random color of five colors. The grid isn't changing colors however, and I'm not sure how to make each object carry its own color.
I have a square class that constructs an object to be a rectangle
public class square extends JPanel {
private int sizeOfSquare = 10;
private Color theColor;
private int theXindexcode;
private int theYindexcode;
public void constructSquare (Graphics g){
g.setColor(theColor);
g.drawRect(theXindexcode, theYindexcode, sizeOfSquare, sizeOfSquare);
g.fillRect(theXindexcode, theYindexcode, sizeOfSquare, sizeOfSquare);
}
square (int theXindex, int theYindex ){
sizeOfSquare = 10;
theXindexcode = theXindex;
theYindexcode = theYindex;
}
public void setTheColor(Color theColor) {
this.theColor = theColor;
}
}
I also have a drawing panel with this code inside it.
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel implements ActionListener, MouseListener, MouseMotionListener {
private final int rows = 50;
private final int columns = 50;
private final int pixelsize = 10;
private final int rowIndex = 0;
private final int colIndex = 0;
private final boolean thisSquare = false;
int trackX;
int trackY;
private boolean setColor;
int [][] theGrid = new int[rows][columns];
square [][] squareArray = new square[50][50];
private int setInitialColor;
private Color either1;
public DrawingPanel() {
super();
//thisSquare = false;
this.addMouseListener(this);
//Creates a random number, if that number is greater than 5, the square in the grid will be red
//if anything else the square will be black
for (int i = 0; i < 50; i++){
for (int i2 = 0; i2 < 50; i2++){
square newSquareObject = new square(i2*10, i2*10);
squareArray[rowIndex][colIndex] = newSquareObject;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int whatisitX = e.getX()/10*10;
int whatisitY = e.getY()/10*10;
trackX = whatisitX;
trackY = whatisitY;
System.out.println(whatisitX +","+ whatisitY);
if(trackX>= 0 && trackX <= 500 && trackY >=0 && trackY <=500){
System.out.println(setColor);
setColor = true;
}
}
//Create 2D array for Grid
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Loop through grid with 2D array and nested for Loop.
for (int rowIndex = 0; rowIndex <= 50; rowIndex++) {
for (int colIndex = 0; colIndex < 50; colIndex++) {
Random random = new Random();
int j = random.nextInt();
if (j >= 2) {
g.setColor(Color.white);
}
else {
g.setColor(Color.black);
}
g.drawRect(rowIndex * 10, colIndex * 10, pixelsize, pixelsize);
g.fillRect(rowIndex * 10, colIndex * 10, pixelsize, pixelsize);
//Creates border for grid
g.setColor(Color.pink);
g.drawRect(rowIndex * 10, colIndex * 10, pixelsize, pixelsize);
}
}
if (setColor == true) {
Random random = new Random();
Color thecolorsetter = Color.orange;
if (random.nextInt(5) == 0){
thecolorsetter = Color.green;
}
else if (random.nextInt(5) == 1){
thecolorsetter = Color.red;
}
else if (random.nextInt(5) == 2){
thecolorsetter = Color.yellow;
}
if (random.nextInt(5) == 3){
thecolorsetter = Color.CYAN;
}
if (random.nextInt(5) == 4){
thecolorsetter = Color.MAGENTA;
}
squareArray[trackX][trackY].setTheColor(thecolorsetter);
repaint();
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
public int getRowIndex() {
return rowIndex;
}
public int getColIndex() {
return colIndex;
}
}
I attempted constructing the drawing panel with the rando
The following is a one-file mre demonstrating the functionality described in the question.
Copy-paste the entire code into DrawingPanel.java
and run.
Please read the comments and do not hesitate to ask for clarifications as needed:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel{
private static final int ROWS = 50, COLS = 50, PIXLE_SIZE = 10,
GAP = 0; //change gap to 1 for example if you want to set a gap between squares
public DrawingPanel() {
super();
setBackground(Color.WHITE);
setLayout(new GridLayout(ROWS, COLS, GAP, GAP));
Random random = new Random();
for (int i = 0; i < ROWS; i++){
for (int i2 = 0; i2 < COLS; i2++){
Square newSquareObject = new Square(PIXLE_SIZE);
//if random number is greater than 5, the Square in the grid will be red
//if anything else the Square will be black
newSquareObject.setTheColor(random.nextInt(10)>5 ? Color.RED : Color.BLACK);
//you need to add each square to get it painted
add(newSquareObject);
}
}
setPreferredSize(new Dimension(ROWS*(PIXLE_SIZE+GAP),COLS*(PIXLE_SIZE+GAP)));
}
public static void main(String[] args0) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingPanel());
frame.pack();
frame.setVisible(true);
}
}
class Square extends JPanel implements MouseListener {
private final int sizeOfSquare;
private static final Color DEFAULT_COLOR = Color.BLACK;
private static final Color[] colors = {Color.ORANGE, Color.GREEN, Color.RED, Color.YELLOW, Color.CYAN, Color.MAGENTA};
Square (int sizeOfSquare){
this.sizeOfSquare = sizeOfSquare;
setPreferredSize(new Dimension(sizeOfSquare , sizeOfSquare));
setTheColor(DEFAULT_COLOR);
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
setTheColor(colors[ new Random().nextInt(colors.length)]);
}
void setTheColor(Color theColor) {
setBackground(theColor);
}
int getSizeOfSquare(){
return sizeOfSquare;
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.