3

当鼠标左键单击时,水平相邻的颜色应该交换,右键单击时,垂直相邻的颜色应该交换。当我单击任一按钮时,什么都没有发生。

有问题的代码:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

import sun.java2d.loops.DrawRect;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Board extends JPanel implements MouseListener
{
//instance variables
private int width;
private int height;
private Block topLeft;
private Block topRight;
private Block botLeft;
private Block botRight;

public Board()  //constructor
{
    width = 200;
    height = 200;
    topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
    topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
    botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
    botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
    setBackground(Color.WHITE);
    setVisible(true);
    //start trapping for mouse clicks
    addMouseListener(this);
}

//initialization constructor
 public Board(int w, int h)  //constructor
 {
    width = w;
    height = h;
    topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
    topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
    botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
    botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
    setBackground(Color.WHITE);
    setVisible(true);
    //start trapping for mouse clicks
    addMouseListener(this);
}



public void update(Graphics window)
{
    paint(window);
}

public void paintComponent(Graphics window)
 {
super.paintComponent(window);
topRight.draw(window);
topLeft.draw(window);
botRight.draw(window);
botLeft.draw(window);


}

 public void swapTopRowColors()
{
Color temp = topLeft.getColor();
topLeft.setColor(topRight.getColor());
topRight.setColor(temp);
repaint();
}

public void swapBottomRowColors()
{
   Color temp = botLeft.getColor();
   botLeft.setColor(botRight.getColor());
   botRight.setColor(temp);
   repaint();
}

public void swapLeftColumnColors()
{
   Color temp = botLeft.getColor();
   botLeft.setColor(topLeft.getColor());
   topLeft.setColor(temp);
   repaint();
}

public void swapRightColumnColors()
{
   Color temp = botRight.getColor();
   botRight.setColor(topRight.getColor());
   topRight.setColor(temp);
   repaint();
}

public void mouseClicked(MouseEvent e)
{
    int mouseX=e.getX();
    int mouseY=e.getY();
    int mouseButton = e.getButton();

    if(mouseButton==MouseEvent.BUTTON1)     //left mouse button pressed
    {
        if(mouseX>=topLeft.getX() && mouseX<=topLeft.getWidth() && mouseY>=topLeft.getY() && mouseY<=topLeft.getY())
        {
            this.swapTopRowColors();
        }

        else if(mouseX>=topRight.getX() && mouseX<=topRight.getWidth() && mouseY>=topRight.getY() && mouseY<=topRight.getY())
        {
            this.swapTopRowColors();
        }

        else if(mouseX>=botLeft.getX() && mouseX<=botLeft.getWidth() && mouseY>=botLeft.getY() && mouseY<=botLeft.getY())
        {
            this.swapBottomRowColors();
        }

        else if(mouseX>=botRight.getX() && mouseX<=botRight.getWidth() && mouseY>=botRight.getY() && mouseY<=botRight.getY())
        {
            this.swapBottomRowColors();
        }

    }
    //right mouse button pressed
    if(mouseX>=topLeft.getX() && mouseX<=topLeft.getWidth() && mouseY>=topLeft.getY() && mouseY<=topLeft.getY())
    {
        this.swapLeftColumnColors();
    }

    else if(mouseX>=topRight.getX() && mouseX<=topRight.getWidth() && mouseY>=topRight.getY() && mouseY<=topRight.getY())
    {
        this.swapRightColumnColors();
    }

    else if(mouseX>=botLeft.getX() && mouseX<=botLeft.getWidth() && mouseY>=botLeft.getY() && mouseY<=botLeft.getY())
    {
        this.swapLeftColumnColors();
    }

    else if(mouseX>=botRight.getX() && mouseX<=botRight.getWidth() && mouseY>=botRight.getY() &&    mouseY<=botRight.getY())
    {
        this.swapRightColumnColors();
    }









}

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

//toString
}

以及启动它的代码:

import javax.swing.JFrame;

public class BlockGame extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;

public BlockGame()
{
    super("Board");
    setSize(WIDTH,HEIGHT);

    getContentPane().add(new Board(500,500));

    setVisible(true);
}

public static void main( String args[] )
{
    BlockGame run = new BlockGame();

}
}
4

3 回答 3

5

你有两个问题...

mouseX >= topLeft.getX() && mouseX <= topLeft.getWidth()

这是检查鼠标位置是否大于或等于块 x 位置(这很好)并且小于或等于它的宽度....?因此,如果我有一个 100 宽度为 10 的框,并且我单击了 105,那么此检查将失败。

105 >= 100 && 105 < 10 // .... ???

然后是这个...

mouseY >= topLeft.getY() && mouseY <= topLeft.getY()

花点时间检查最后一个条件...您必须完全单击块的顶部边缘才能使此条件为真。

我会做两件事之一。

要么我会编写一个对任何块执行此计算的方法......

public boolean contains(Point p, Block block) {
    return p.x >= block.getX() && p.x <= block.getX() + block.getWidth() && 
            p.y >= block.getY() && p.y <= block.getY() + block.getHeight();
}

这样,如果代码中有错误,它只会在一个地方......

或者(最好),我会扩展Blockfrom Rectangle,这样我就可以简单地使用该contains方法......

public class Block extends Rectangle {

    private Color color;

    public Block(int x, int y, int width, int height, Color color) {
        super(x, y, width, height);
        this.color = color;
    }

    public void draw(Graphics2D g) {
        g.setColor(color);
        g.fill(this);
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

在你的鼠标点击事件处理程序中......

if (topLeft.contains(e.getPoint()) { ... }
于 2012-11-09T03:26:23.233 回答
0
  • 我认为你在这里混淆了屏幕和窗口坐标: getX() 返回相对于源组件的鼠标坐标,在这种特殊情况下这可能不是你想要的......
  • 打印一行以检查程序是否实际到达 mouseClicked() 函数。可能想将它绑定到 mousePressed(),然后查看它是否注册。
  • 你有 4 个 if 语句(if、else if、else if、else if),但是当这 4 个失败时没有最终语句,允许程序默默地通过所有这些选项。您显然不希望有任何其他选择,但这可能就是发生的情况 - 在那里打印一行带有一些调试输出(X,Y,源组件),您可能会学到很多东西。:)
于 2012-11-09T01:51:07.053 回答
0

最后的代码是这样的:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

//import sun.java2d.loops.DrawRect;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Board extends JPanel implements MouseListener
{
//instance variables
private int width;
private int height;
private Block topLeft;
private Block topRight;
private Block botLeft;
private Block botRight;

public Board()  //constructor
{
    width = 200;
    height = 200;
    topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
    topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
    botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
    botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
    setBackground(Color.WHITE);
    setVisible(true);
    //start trapping for mouse clicks
    addMouseListener(this);
}

  //initialization constructor
public Board(int w, int h)  //constructor
{
    width = w;
    height = h;
    topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
    topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
    botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
    botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
    setBackground(Color.WHITE);
    setVisible(true);
    //start trapping for mouse clicks
    addMouseListener(this);
}



public void update(Graphics window)
{
    paint(window);
}

 public void paintComponent(Graphics window)
 {
super.paintComponent(window);
topRight.draw(window);
topLeft.draw(window);
botRight.draw(window);
botLeft.draw(window);


}

  public void swapTopRowColors()
 {
Color temp = topLeft.getColor();
Color temp2 = topRight.getColor();
topRight.setColor(temp);
topLeft.setColor(temp2);

repaint();
}

public void swapBottomRowColors()
{
   Color temp = botLeft.getColor();
   Color temp2 = botRight.getColor();
   botLeft.setColor(temp2);
   botRight.setColor(temp);
   repaint();
 }

public void swapLeftColumnColors()
{
   Color temp = botLeft.getColor();
   Color temp2 = topLeft.getColor();
   botLeft.setColor(temp2);
   topLeft.setColor(temp);
   repaint();
}

public void swapRightColumnColors()
 {
   Color temp = botRight.getColor();
   Color temp2 = topRight.getColor();
   botRight.setColor(temp2);
   topRight.setColor(temp);
   repaint();
 }

public void mouseClicked(MouseEvent e)
{
    int mouseX=e.getX();
    int mouseY=e.getY();
    int mouseButton = e.getButton();
    //System.out.println("User clicked at " + e.getX() + "," + e.getY());
    if(mouseButton == MouseEvent.BUTTON1)       //left mouse button pressed
    {
        if(((mouseX>= 0 && mouseX <= topLeft.getWidth()-1) && (mouseY>= 0 && mouseY <= (topLeft.getHeight()-1))) || ((mouseX>= topRight.getX() && mouseX <= (topRight.getX()+topRight.getWidth())-1) && (mouseY>= 0 && mouseY <= (topRight.getY()+topRight.getHeight()-1))))
        {
            this.swapTopRowColors();
        }
        else
        {
            this.swapBottomRowColors();
        }





    }
    //right mouse button pressed
    else
    {
        if(((mouseX>= 0 && mouseX <= topLeft.getWidth()-1) && (mouseY>=0 && mouseY <=  (topLeft.getHeight()-1))) || ((mouseX>= botLeft.getX() && mouseX <= (botLeft.getX()+botLeft.getWidth())-1) && (mouseY>= 0 && mouseY <= (botLeft.getY()+(botLeft.getHeight()-1)))))
    {
        this.swapLeftColumnColors();
    }
        else
        {
            this.swapRightColumnColors();
        }




    }





    System.out.println(botLeft.getHeight() + ", " + botLeft.getY());




}

public void mouseEntered(MouseEvent e) {

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

 //toString
}

它工作得很好。有一个颜色问题来自我没有发现但已修复的复制/粘贴错误。

于 2012-11-09T03:57:59.120 回答