当鼠标左键单击时,水平相邻的颜色应该交换,右键单击时,垂直相邻的颜色应该交换。当我单击任一按钮时,什么都没有发生。
有问题的代码:
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();
}
}