无法将图形应用于 if 语句

发布于 2024-12-01 17:35:37 字数 4900 浏览 3 评论 0原文

我正在编写一个程序,它有一个正方形网格,当您单击其中一个网格时,它的颜色会从黑色变为红色。我正在为第一个方块编写 if 语句,但它不允许我对图形执行任何操作。我如何 1) 将颜色更改为红色,2) 在同一位置绘制一个新正方形,以及 3) 为什么它不像 MouseHandler 类声明?

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class toggle extends JPanel{
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.fillRect(0,0,20,20);
        g.fillRect(21,0,20,20);
        g.fillRect(42,0,20,20);
        g.fillRect(63,0,20,20);
        g.fillRect(84,0,20,20);
        g.fillRect(105,0,20,20);
        g.fillRect(126,0,20,20);
        g.fillRect(147,0,20,20);
        g.fillRect(168,0,20,20);
        g.fillRect(189,0,20,20);
        g.fillRect(190,0,20,20);
        //
        g.fillRect(0,21,20,20);
        g.fillRect(21,21,20,20);
        g.fillRect(42,21,20,20);
        g.fillRect(63,21,20,20);
        g.fillRect(84,21,20,20);
        g.fillRect(105,21,20,20);
        g.fillRect(126,21,20,20);
        g.fillRect(147,21,20,20);
        g.fillRect(168,21,20,20);
        g.fillRect(189,21,20,20);
        g.fillRect(190,21,20,20);
        //
        g.fillRect(0,42,20,20);
        g.fillRect(21,42,20,20);
        g.fillRect(42,42,20,20);
        g.fillRect(63,42,20,20);
        g.fillRect(84,42,20,20);
        g.fillRect(105,42,20,20);
        g.fillRect(126,42,20,20);
        g.fillRect(147,42,20,20);
        g.fillRect(168,42,20,20);
        g.fillRect(189,42,20,20);
        g.fillRect(190,42,20,20);
        //
        g.fillRect(0,63,20,20);
        g.fillRect(21,63,20,20);
        g.fillRect(42,63,20,20);
        g.fillRect(63,63,20,20);
        g.fillRect(84,63,20,20);
        g.fillRect(105,63,20,20);
        g.fillRect(126,63,20,20);
        g.fillRect(147,63,20,20);
        g.fillRect(168,63,20,20);
        g.fillRect(189,63,20,20);
        g.fillRect(190,63,20,20);
        //
        g.fillRect(0,84,20,20);
        g.fillRect(21,84,20,20);
        g.fillRect(42,84,20,20);
        g.fillRect(63,84,20,20);
        g.fillRect(84,84,20,20);
        g.fillRect(105,84,20,20);
        g.fillRect(126,84,20,20);
        g.fillRect(147,84,20,20);
        g.fillRect(168,84,20,20);
        g.fillRect(189,84,20,20);
        g.fillRect(190,84,20,20);
        //
        g.fillRect(0,105,20,20);
        g.fillRect(21,105,20,20);
        g.fillRect(42,105,20,20);
        g.fillRect(63,105,20,20);
        g.fillRect(84,105,20,20);
        g.fillRect(105,105,20,20);
        g.fillRect(126,105,20,20);
        g.fillRect(147,105,20,20);
        g.fillRect(168,105,20,20);
        g.fillRect(189,105,20,20);
        g.fillRect(190,105,20,20);
        //
        g.fillRect(0,126,20,20);
        g.fillRect(21,126,20,20);
        g.fillRect(42,126,20,20);
        g.fillRect(63,126,20,20);
        g.fillRect(84,126,20,20);
        g.fillRect(105,126,20,20);
        g.fillRect(126,126,20,20);
        g.fillRect(147,126,20,20);
        g.fillRect(168,126,20,20);
        g.fillRect(189,126,20,20);
        g.fillRect(190,126,20,20);
        //
        g.fillRect(0,147,20,20);
        g.fillRect(21,147,20,20);
        g.fillRect(42,147,20,20);
        g.fillRect(63,147,20,20);
        g.fillRect(84,147,20,20);
        g.fillRect(105,147,20,20);
        g.fillRect(126,147,20,20);
        g.fillRect(147,147,20,20);
        g.fillRect(168,147,20,20);
        g.fillRect(189,147,20,20);
        g.fillRect(190,147,20,20);
        //
        g.fillRect(0,168,20,20);
        g.fillRect(21,168,20,20);
        g.fillRect(42,168,20,20);
        g.fillRect(63,168,20,20);
        g.fillRect(84,168,20,20);
        g.fillRect(105,168,20,20);
        g.fillRect(126,168,20,20);
        g.fillRect(147,168,20,20);
        g.fillRect(168,168,20,20);
        g.fillRect(189,168,20,20);
        g.fillRect(190,168,20,20);
        //
        g.fillRect(0,189,20,20);
        g.fillRect(21,189,20,20);
        g.fillRect(42,189,20,20);
        g.fillRect(63,189,20,20);
        g.fillRect(84,189,20,20);
        g.fillRect(105,189,20,20);
        g.fillRect(126,189,20,20);
        g.fillRect(147,189,20,20);
        g.fillRect(168,189,20,20);
        g.fillRect(189,189,20,20);
        g.fillRect(190,189,20,20);
    }

    public toggle(){
        super();
        JFrame frame=new JFrame();
        frame.setTitle("toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.setSize(226, 247);
        frame.setVisible(true);

        MouseHandler hand=new MouseHandler();
        addMouseListener(hand);

    }

    public class MouseHandler implements MouseListener{
        public void MousePressed (MouseEvent e){

            if(e.getX()<20&&e.getX()>0&&e.getY()<20&&e.getY()>0){


            }
        }
    }

    public static void main (String[]args){
        new toggle();
    }


}

I'm writing a program that has a grid of squares that when you click on one, it changes color from black to red. I'm writing the if statement for the first square, and It's not letting me do anything with graphics. How do I 1) change the color to red, 2) draw a new square in the same place, and 3) why dosen't it like the MouseHandler class declaration?

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class toggle extends JPanel{
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.fillRect(0,0,20,20);
        g.fillRect(21,0,20,20);
        g.fillRect(42,0,20,20);
        g.fillRect(63,0,20,20);
        g.fillRect(84,0,20,20);
        g.fillRect(105,0,20,20);
        g.fillRect(126,0,20,20);
        g.fillRect(147,0,20,20);
        g.fillRect(168,0,20,20);
        g.fillRect(189,0,20,20);
        g.fillRect(190,0,20,20);
        //
        g.fillRect(0,21,20,20);
        g.fillRect(21,21,20,20);
        g.fillRect(42,21,20,20);
        g.fillRect(63,21,20,20);
        g.fillRect(84,21,20,20);
        g.fillRect(105,21,20,20);
        g.fillRect(126,21,20,20);
        g.fillRect(147,21,20,20);
        g.fillRect(168,21,20,20);
        g.fillRect(189,21,20,20);
        g.fillRect(190,21,20,20);
        //
        g.fillRect(0,42,20,20);
        g.fillRect(21,42,20,20);
        g.fillRect(42,42,20,20);
        g.fillRect(63,42,20,20);
        g.fillRect(84,42,20,20);
        g.fillRect(105,42,20,20);
        g.fillRect(126,42,20,20);
        g.fillRect(147,42,20,20);
        g.fillRect(168,42,20,20);
        g.fillRect(189,42,20,20);
        g.fillRect(190,42,20,20);
        //
        g.fillRect(0,63,20,20);
        g.fillRect(21,63,20,20);
        g.fillRect(42,63,20,20);
        g.fillRect(63,63,20,20);
        g.fillRect(84,63,20,20);
        g.fillRect(105,63,20,20);
        g.fillRect(126,63,20,20);
        g.fillRect(147,63,20,20);
        g.fillRect(168,63,20,20);
        g.fillRect(189,63,20,20);
        g.fillRect(190,63,20,20);
        //
        g.fillRect(0,84,20,20);
        g.fillRect(21,84,20,20);
        g.fillRect(42,84,20,20);
        g.fillRect(63,84,20,20);
        g.fillRect(84,84,20,20);
        g.fillRect(105,84,20,20);
        g.fillRect(126,84,20,20);
        g.fillRect(147,84,20,20);
        g.fillRect(168,84,20,20);
        g.fillRect(189,84,20,20);
        g.fillRect(190,84,20,20);
        //
        g.fillRect(0,105,20,20);
        g.fillRect(21,105,20,20);
        g.fillRect(42,105,20,20);
        g.fillRect(63,105,20,20);
        g.fillRect(84,105,20,20);
        g.fillRect(105,105,20,20);
        g.fillRect(126,105,20,20);
        g.fillRect(147,105,20,20);
        g.fillRect(168,105,20,20);
        g.fillRect(189,105,20,20);
        g.fillRect(190,105,20,20);
        //
        g.fillRect(0,126,20,20);
        g.fillRect(21,126,20,20);
        g.fillRect(42,126,20,20);
        g.fillRect(63,126,20,20);
        g.fillRect(84,126,20,20);
        g.fillRect(105,126,20,20);
        g.fillRect(126,126,20,20);
        g.fillRect(147,126,20,20);
        g.fillRect(168,126,20,20);
        g.fillRect(189,126,20,20);
        g.fillRect(190,126,20,20);
        //
        g.fillRect(0,147,20,20);
        g.fillRect(21,147,20,20);
        g.fillRect(42,147,20,20);
        g.fillRect(63,147,20,20);
        g.fillRect(84,147,20,20);
        g.fillRect(105,147,20,20);
        g.fillRect(126,147,20,20);
        g.fillRect(147,147,20,20);
        g.fillRect(168,147,20,20);
        g.fillRect(189,147,20,20);
        g.fillRect(190,147,20,20);
        //
        g.fillRect(0,168,20,20);
        g.fillRect(21,168,20,20);
        g.fillRect(42,168,20,20);
        g.fillRect(63,168,20,20);
        g.fillRect(84,168,20,20);
        g.fillRect(105,168,20,20);
        g.fillRect(126,168,20,20);
        g.fillRect(147,168,20,20);
        g.fillRect(168,168,20,20);
        g.fillRect(189,168,20,20);
        g.fillRect(190,168,20,20);
        //
        g.fillRect(0,189,20,20);
        g.fillRect(21,189,20,20);
        g.fillRect(42,189,20,20);
        g.fillRect(63,189,20,20);
        g.fillRect(84,189,20,20);
        g.fillRect(105,189,20,20);
        g.fillRect(126,189,20,20);
        g.fillRect(147,189,20,20);
        g.fillRect(168,189,20,20);
        g.fillRect(189,189,20,20);
        g.fillRect(190,189,20,20);
    }

    public toggle(){
        super();
        JFrame frame=new JFrame();
        frame.setTitle("toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.setSize(226, 247);
        frame.setVisible(true);

        MouseHandler hand=new MouseHandler();
        addMouseListener(hand);

    }

    public class MouseHandler implements MouseListener{
        public void MousePressed (MouseEvent e){

            if(e.getX()<20&&e.getX()>0&&e.getY()<20&&e.getY()>0){


            }
        }
    }

    public static void main (String[]args){
        new toggle();
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆° 2024-12-08 17:35:37

另请考虑使用 JLabel的替代实现 >鼠标适配器

Also consider this alternative implementation using JLabel and a MouseAdapter.

伤痕我心 2024-12-08 17:35:37

当然你不需要全部 g.fillRect(0,0,20,20);您可以使用循环来处理此问题,因为每行中的第一个参数都有一个出现模式。

Surely you don't need all of them g.fillRect(0,0,20,20); You could use a loop to handle this since there is an occurring pattern for the first param in each line.

赤濁 2024-12-08 17:35:37

1 和 2) 您需要在 boolean[][] 字段中保留每个方块的状态,例如,这将告诉您网格中的每个方块是否被单击。在 paintComponent 方法中,检查每个方块的状态并调用 g.setColor,传递 Color.BLACKColor.RED 作为相应的参数。

3) 您需要实现 MouseListener 接口的所有方法:

public class MouseHandler implements MouseListener {
    public void mousePressed( MouseEvent e ) {
        // your logic here
    }

    public void mouseReleased( MouseEvent e ) {
        // leave method empty, since you don't need to listen to this event
    }

    // Same as above for mouseClicked, mouseEntered, mouseExited
}

或者扩展 MouseAdapter 类并覆盖 mousePressed 如果您不关心其他 MouseListener 事件:

public class MouseHandler extends MouseAdapter {
    public void mousePressed( MouseEvent e ) {
        // your logic here
    }

    // No need to override the other methods, since
    // MouseAdapter already has empty implementations for them
}

此外,您正在实现的方法的正确名称是 mousePressed (注意小写的 m)。

1 and 2) You need to keep state for each square, in a boolean[][] field, for instance, which would tell if each square in your grid is clicked or not. In your paintComponent method, check for the state of each square and call g.setColor, passing Color.BLACK or Color.RED as argument accordingly.

3) You need to implement all methods for the MouseListener interface:

public class MouseHandler implements MouseListener {
    public void mousePressed( MouseEvent e ) {
        // your logic here
    }

    public void mouseReleased( MouseEvent e ) {
        // leave method empty, since you don't need to listen to this event
    }

    // Same as above for mouseClicked, mouseEntered, mouseExited
}

or extend the MouseAdapter class and override mousePressed if you don't care for the other MouseListener events:

public class MouseHandler extends MouseAdapter {
    public void mousePressed( MouseEvent e ) {
        // your logic here
    }

    // No need to override the other methods, since
    // MouseAdapter already has empty implementations for them
}

Also, the correct name for the method you're implementing is mousePressed (note the lowercase m).

背叛残局 2024-12-08 17:35:37

3) 为什么它不像 MouseHandler 类声明?

当您实现接口时,您必须提供所有方法的实现或声明实现抽象。在这种情况下,您还没有实现任何一个方法 - Java 区分大小写,并且 MousePressed 与您想要实现的方法 mousePressed 不同。

此外,您会发现有一些接口的方法比您感兴趣的多得多,并且您最终将提供空方法以满足编译器的要求。在这种情况下,您可能会查看是否有可以扩展的“适配器”类。在这种情况下,您将找到 MouseAdapter很有用。

3) why dosen't it like the MouseHandler class declaration?

When you implement an interface you must provide an implementation for all the methods or declare the implementation abstract. In this case, you haven't implemented a single method - Java is case-sensitive and MousePressed is not the same as mousePressed, the method you do want to implement.

Also, you'll find that there are a few interfaces that have a lot more methods than you're interested in and you'll end up providing empty methods in order to satisfy the compiler. In such a case, you might look to see if there's an 'adapter' class you can extend. In this case, you'll find the MouseAdapter useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文