[我试图制作一个交互式网格,但是当我单击正方形(Java)时颜色不会改变]]

问题描述 投票:0回答:1

我正在尝试制作交互式网格,但是当我单击正方形时颜色不会改变。网格必须是彩色的,单击网格时,其内部的框应将颜色更改为五种颜色的随机颜色。但是,网格并没有改变颜色,我不确定如何使每个对象都具有自己的颜色。

我有一个将对象构造成矩形的正方形类

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

我也有一个绘图面板,其中包含此代码。

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

我尝试使用rando构建绘图面板

我正在尝试制作交互式网格,但是当我单击正方形时颜色不会改变。网格必须是彩色的,并且单击网格时,其内部的框应会发生变化...

java swing awt
1个回答
0
投票

以下是一个文件mre,演示了问题中描述的功能。将整个代码复制粘贴到DrawingPanel.java中并运行。请阅读评论,如有需要,请随时澄清:

© www.soinside.com 2019 - 2024. All rights reserved.