我正在设计一款与 Flow free 非常相似的游戏。我在代码中遇到了一个问题,我希望能够单击网格中的一个圆圈,能够测量其颜色,然后检查颜色是否为白色。如果颜色是白色,我将打印错误,目前它是一个占位符。但是,即使当我单击非白色图 block 时,我仍然看到打印的错误消息。我做错了什么?
我的代码:
import javax.swing.JFrame; //JFrame Class
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.util.ArrayList;
import javax.swing.JPanel;
import java.awt.geom.Ellipse2D;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DriverV2 extends JPanel {
static ArrayList<Shape> circles;
static ArrayList<Color> colors;
public DriverV2() {
circles = new ArrayList<Shape>();
colors = new ArrayList<Color>();
colors.add( Color.CYAN);//1
colors.add( Color.BLUE);//2
colors.add( Color.WHITE);//3
colors.add( Color.WHITE);//4
colors.add( Color.GREEN);//5
colors.add( Color.WHITE);//6
colors.add( Color.CYAN);//7
colors.add( Color.WHITE);//8
colors.add( Color.WHITE);//9
colors.add( Color.RED);//10
colors.add( Color.WHITE);//11
colors.add( Color.YELLOW);//12
colors.add( Color.WHITE);//13
colors.add( Color.GREEN);//14
colors.add( Color.WHITE);//15
colors.add( Color.WHITE);//16
colors.add( Color.BLUE);//17
colors.add( Color.WHITE);//18
colors.add( Color.RED);//19
colors.add( Color.WHITE);//20
colors.add( Color.WHITE);//21
colors.add( Color.WHITE);//22
colors.add( Color.WHITE);//23
colors.add( Color.WHITE);//24
colors.add( Color.YELLOW);//25
int rows = 5;
for (int y=0;y< rows;y++)
{
for (int x=0;x<rows;x++)
{
circles.add( new Ellipse2D.Double((x + 1) * 150, (y + 1) *150, 100, 100) );
}
}
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
Color foreground = g2d.getColor();
int h = 0;
for (Shape shape : circles)
{
g2d.setColor( colors.get(h));
g2d.draw( shape );
g2d.fill(shape);
h++;
}
}
public static boolean checkLinearity(int index1, int index2){
if((index1%5) == (index2 %5)) { //vertical line check
return true;
}
if(Math.abs(index1-index2) < 5) { //horizantal line check
return true;
}
else {
return false;
}
}
static int counter = 0;
static int index1 = 0;
static int index2 = 0;
public static void main(String[] args)
{
JFrame f = new JFrame("Flow"); //new JFrame
DriverV2 t = new DriverV2();
f.setSize(900,900);//sets size of frame
// f.setLocation(100,50);//sets location of frame
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets default close operation
// f.setContentPane(new Panel()); //sets content pane
f.setVisible(true);//makes panel visible
f.add(t);
f.addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
for (Shape shape: circles) {
if (shape.contains(x,y)) {
counter++;
if(counter % 2 == 1) {
index1 = circles.indexOf(shape)+1;
if(Color.WHITE.equals(colors.get(index1))) {
counter--;
System.out.println("error");
}
}
else{
index2 = circles.indexOf(shape) +1;
if(checkLinearity(index1, index2) == true) {
//I want to be able to draw a rectange here
}
}
}
}
}
});
}
}
最佳答案
你的问题似乎是为什么许多人(包括我)提倡总是添加大括号的另一个例子(即使在 1 语句 block 上 - 他们可能不会保持这种方式):
看看这个片段:
if (shape.contains(x,y))
counter++;
if(counter % 2 == 1) {
...
它不会按照您的想法进行操作,即,如果您点击形状计数器
,则会增加,但会对每个形状执行其余检查。这是因为对于编译器来说,代码如下所示:
if (shape.contains(x,y))
counter++;
if(counter % 2 == 1) {
...
因此,当您击中一个圆圈时,counter
将为 1,因此对于之后检查的每个圆圈,counter % 2 == 1
将为 true。
因此,在缩进的代码周围添加大括号。此外,一旦检测到命中,您可能想要中断循环,因为您可能也没有兴趣检查所有其他圈子。
您可能还想引入一个类Circle
,它可能如下所示:
class Circle {
Shape shape;
Color color;
... //additional properties, e.g. position, and methods
public boolean wasHit(int x, int y) {
return shape.contains(x,y);
}
public boolean isValid() {
return Color.WHITE.equals( color );
}
}
然后你可以像这样迭代:
for( Circle circle : circles ) {
if( circle.wasHit(x, y) {
if( circle.isValid() ) {
//do what you'd need here, e.g. change the shape to a rectangle
} else {
//error
}
//break the loop since other circles can't be hit or we're not interested in those
break;
}
}
关于java - 颜色引用不好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569974/