无法将图形应用于 if 语句
我正在编写一个程序,它有一个正方形网格,当您单击其中一个网格时,它的颜色会从黑色变为红色。我正在为第一个方块编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另请考虑使用
JLabel
和的替代实现 >鼠标适配器
。Also consider this alternative implementation using
JLabel
and aMouseAdapter
.当然你不需要全部 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.
1 和 2) 您需要在
boolean[][]
字段中保留每个方块的状态,例如,这将告诉您网格中的每个方块是否被单击。在paintComponent
方法中,检查每个方块的状态并调用g.setColor
,传递Color.BLACK
或Color.RED
作为相应的参数。3) 您需要实现
MouseListener
接口的所有方法:或者扩展
MouseAdapter
类并覆盖mousePressed
如果您不关心其他MouseListener
事件:此外,您正在实现的方法的正确名称是
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 yourpaintComponent
method, check for the state of each square and callg.setColor
, passingColor.BLACK
orColor.RED
as argument accordingly.3) You need to implement all methods for the
MouseListener
interface:or extend the
MouseAdapter
class and overridemousePressed
if you don't care for the otherMouseListener
events:Also, the correct name for the method you're implementing is
mousePressed
(note the lowercase m).当您实现接口时,您必须提供所有方法的实现或声明实现
抽象
。在这种情况下,您还没有实现任何一个方法 - Java 区分大小写,并且MousePressed
与您想要实现的方法mousePressed
不同。此外,您会发现有一些接口的方法比您感兴趣的多得多,并且您最终将提供空方法以满足编译器的要求。在这种情况下,您可能会查看是否有可以扩展的“适配器”类。在这种情况下,您将找到
MouseAdapter
很有用。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 andMousePressed
is not the same asmousePressed
, 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.