简体   繁体   English

更改网格中矩形的颜色

[英]Change color of rectangle in grid

I need help changing the color of a rectangle in a grid. 我需要更改网格中矩形的颜色的帮助。 So far, I have the rectangles and the grid (which are lines drawn separating the rectangles), and I want to change the color of a single rectangle when it's clicked. 到目前为止,我已经有了矩形和网格(将矩形分开绘制的线),并且我想在单击单个矩形时更改其颜色。

I added the line "System.out.println("Something")" in the loop where the color is supposed to change, and it always returns "Something." 我在应该改变颜色的循环中添加了行“ System.out.println(“ Something”)“,该行始终返回“ Something”。 That's why I'm confused. 这就是为什么我很困惑。 Thanks for any and all help! 感谢您提供的所有帮助!

Class Grid: 类网格:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;

 public class Grid extends JPanel{

private int a = 50;
private int b = 50;
private Lifeform[][] Life;
private boolean[][]life = new boolean[a][b];
private Timer t;

Grid(){
//creates grid of rectangles
    Life = new Lifeform[a][b];

    int ypos = 0;
    for(int i = 0; i < Life.length; i++){
        int xpos = 0;
        for(int j = 0; j < Life[0].length; j++){

            Rectangle r = new Lifeform();
            r.setBounds(xpos, ypos, 50, 50);
            Life[i][j] = (Lifeform) r;
            xpos += 50;
        }
        ypos += 50;
    }
    t = new Timer(64, new Movement());
    this.addMouseListener(new mouse());
}

public void paintComponent(Graphics g){

    for(Lifeform[] n : Life){
//makes rectangles white
        g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
        g.setColor(Color.white);
    }
    for (int i = 0; i <= 25; i++){
        g.drawLine(0, 50*i, 1500, 50*i);
        g.setColor(Color.black);
    }
    for (int i = 0; i <= 25; i++){
        g.drawLine(50*i, 0, 50*i, 750);
        g.setColor(Color.black);
    }
}

private JFrame createGrid(){

    JFrame frame = new JFrame("Alveolate");
    frame.add(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700,700);
    frame.setVisible(true);     
    return frame;
}

public class mouse implements MouseListener{
//colors rectangles (it doesn't work)
    public void mouseClicked(MouseEvent e) {
        Point p = new Point(e.getX(),e.getY());
        for(int i = 0; i < Life.length; i++){
            for(int j = 0; j < Life[i].length; j++){
                Lifeform spot = Life[i][j];

                if (spot.contains(p)){
                    spot.setColor(Color.red);
                    System.out.println("Something");

                }
            }

        }   

    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}       
    public void mouseExited(MouseEvent e) {}

}

public class Movement implements ActionListener{
    public void actionPerformed ( ActionEvent e ){

        for (int i = 0; i < Life.length; i++){
            for (int j = 0; j < Life[i].length; j++){
                if(Life[i][j].getColor().equals(Color.black)){
                    life[i][j] = true;
                }
                else{
                    life[i][j] = false;
                }

            }       

        }

        repaint();

    }

}

public void startTimer(){
    t.start();
}

public void stopTimer(){
    t.stop();
}

public static void main(String[] args) {

    Grid ABC = new Grid();
    ABC.createGrid();
    ABC.startTimer();
}
}

Class Lifeform: 类生命形式:

import java.awt.Color; 导入java.awt.Color; import java.awt.Rectangle; 导入java.awt.Rectangle;

public class Lifeform extends Rectangle {
    private Color c;
    public Lifeform() {
        c = Color.WHITE;
    }
    public Color getColor() {
        return c;
    }

    public boolean setColor( Color c ) {
        boolean rtn = false;
        if( c != null ) {
            this.c = c;
            rtn = true;
        }

        return rtn;
    }

    }

try this: 尝试这个:

for(int i = 0; i < 50; i++){
   for(int j = 0; j < 50; j++){
    ....  

   }
}

try 尝试

for(Lifeform[] n : Life){
    for(Lifeform lf : n){
        g.setColor(lf.getColor());
        g.fillRect((int)lf.getX(), (int)lf.getY(), (int)lf.getWidth(), (int)lf.getHeight());
    }
}

in your paintComponent method 在您的paintComponent方法中

Didn't read all you code but it seems like you definitely set the color of your Lifeform to red. 没读完所有代码,但看来您确实将Lifeform的颜色设置为红色。 This means that your rendering code never actually uses this fact to display that Lifeform. 这意味着您的渲染代码从不实际使用此事实来显示该Lifeform。 It seems like the only time you draw your rectangles is in paintComponent. 似乎唯一绘制矩形的时间是在paintComponent中。 This paint needs to be called each time you want a redraw to happen. 每次要重绘时都需要调用此绘画。 You can use repaint on your JFrame or JPanel to get this to happen. 您可以在JFrame或JPanel上使用重新绘制来实现此目的。 Also, the render code you have there seems to only ever display white rectangles 另外,您那里的渲染代码似乎只显示白色矩形

g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
g.setColor(Color.white);

This is in a loop through your Lifeform[][]. 这是通过Lifeform [] []循环进行的。 Instead you should probably have a double for loop to get every Lifeform and use the x and y and color from that to draw. 相反,您可能应该有一个for循环来获取每个Lifeform,并使用x和y以及其中的颜色进行绘制。 Something like: 就像是:

g.fillRect(Life[i][j].getX(), Life[i][j].getY(), ..., ..., Life[i][j].getColor())

Also, Life should be named life with a lowercase 'l'. 同样,生命应以小写的“ l”命名为生命。 It's way confusing seeing names without conventions. 这很容易看到没有约定的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM