与矩形互动时变色

时间:2019-03-21 14:00:37

标签: java jframe jpanel

该程序应该制作一个网格,其中所有矩形均以黑色开头,如果单击或拖动,它们将变为白色。到目前为止,我的程序能够使您单击或拖动的矩形变成白色,但是它不会记住或记住我单击哪个矩形使它们保持白色,直到再次单击它们。也许我会使用鼠标侦听器或通过创建LinkedList来调用更改,但我无法找出最简单的解决方案。

public class Clicky extends JFrame {

private static class Board extends JPanel {

private double BRICK_WIDTH = 20;
private double BRICK_HEIGHT = 20;

public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public double width = screenSize.getWidth();
public double height = screenSize.getHeight();

private int COLS = (int)(width/BRICK_WIDTH);
private int ROWS =(int)(height/BRICK_HEIGHT);
private Color CO = Color.BLACK;


public Board() {
    System.out.println("WIdth:" + COLS + "Height:" + ROWS);
    setBackground(Color.BLACK);
    addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            mx = e.getX();
            my = e.getY();
            System.out.printf("X: %d Y: %d ", mx, my);
            repaint();
        }
    });

    addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            mx = e.getX();
            my = e.getY();

        }

        @Override
        public void mouseMoved(MouseEvent e) {

        }
    });
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawBricks(g);
}


private double x;
private double y;

private void drawBricks(Graphics g) {

    Graphics2D brick = (Graphics2D) g.create();
    x = 0;
    y = 0;
    for (int j = 0; j <= ROWS; j++)
    {
        for (int a = 0; a <= COLS; a++) {
            brick.setColor(Color.BLACK);
            Rectangle2D.Double rect = new Rectangle2D.Double(x, y, BRICK_WIDTH, BRICK_HEIGHT);
            brick.fill(rect);
            if (mx > x && mx < x + BRICK_WIDTH && my > y && my < y + BRICK_HEIGHT) {
                if (brick.getColor() == Color.BLACK) {
                    CO = Color.white;
                    brick.setColor(CO);
                    brick.fill(rect);
                repaint();
                }
                else {
                    CO = Color.BLACK;
                    brick.setColor(CO);
                    brick.fill(rect);
                repaint();
                }
            }


            brick.setColor(Color.gray);
            brick.draw(rect);
            x += BRICK_WIDTH;
        }
        repaint();
        x = 0;
        y += BRICK_HEIGHT;
        }

}
public int mx = -100;
public int my = -100;
}

public Clicky() {

setDefaultCloseOperation(EXIT_ON_CLOSE);    //mai bine cu exit on close
setSize(800, 820);
add(new Board());
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
        new Clicky().setVisible(true);
    }
});

}

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是使用一个二维布尔数组,它表示矩形的状态。颜色更改时,请更改数组元素的值并根据其设置颜色:

public class Clicky extends JFrame {

private static class Board extends JPanel {

    private double BRICK_WIDTH = 20;
    private double BRICK_HEIGHT = 20;

    public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    public double width = screenSize.getWidth();
    public double height = screenSize.getHeight();

    private int COLS = (int) (width / BRICK_WIDTH);
    private int ROWS = (int) (height / BRICK_HEIGHT);
    private Color CO = Color.BLACK;

    private boolean[][] isWhite = new boolean[COLS + 1][ROWS + 1];

    public Board() {
        System.out.println("WIdth:" + COLS + "Height:" + ROWS);
        setBackground(Color.BLACK);
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                mx = e.getX();
                my = e.getY();
                System.out.printf("X: %d Y: %d ", mx, my);
                isWhite[(int) (mx / BRICK_WIDTH)][(int) (my
                        / BRICK_HEIGHT)] = !isWhite[(int) (mx / BRICK_WIDTH)][(int) (my / BRICK_HEIGHT)];
                repaint();
            }
        });

        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                mx = e.getX();
                my = e.getY();
                isWhite[(int) (mx / BRICK_WIDTH)][(int) (my
                        / BRICK_HEIGHT)] = !isWhite[(int) (mx / BRICK_WIDTH)][(int) (my / BRICK_HEIGHT)];
            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawBricks(g);
    }

    private double x;
    private double y;

    private void drawBricks(Graphics g) {

        Graphics2D brick = (Graphics2D) g.create();
        x = 0;
        y = 0;
        for (int j = 0; j <= ROWS; j++) {
            for (int a = 0; a <= COLS; a++) {
                if (isWhite[a][j]) {
                    brick.setColor(Color.WHITE);
                } else {
                    brick.setColor(Color.BLACK);
                }
                Rectangle2D.Double rect = new Rectangle2D.Double(x, y, BRICK_WIDTH, BRICK_HEIGHT);
                brick.fill(rect);

                brick.setColor(Color.gray);
                brick.draw(rect);
                x += BRICK_WIDTH;
            }
            repaint();
            x = 0;
            y += BRICK_HEIGHT;
        }

    }

    public int mx = -100;
    public int my = -100;
}

public Clicky() {

    setDefaultCloseOperation(EXIT_ON_CLOSE); // mai bine cu exit on close
    setSize(800, 820);
    add(new Board());
}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Clicky().setVisible(true);
        }
    });
}
}