Reputation: 229
When the mouse is left-clicked, the colors horizontally adjacent to each other are supposed to swap and when right-clicked, the colors vertically adjacent are supposed to swap. Nothing is happening when I click either button.
code in question:
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
}
and the code that starts it:
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();
}
}
Upvotes: 3
Views: 281
Reputation: 229
final code ended up like this:
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
}
and it works very well. Had one color issue that came from a copy/paste error I didn't catch but is fixed.
Upvotes: 0
Reputation: 347184
You have two issues...
mouseX >= topLeft.getX() && mouseX <= topLeft.getWidth()
This is checking to see if the mouse position is greater or equal to the blocks x position (this is good) and less then or equal to it's width....??? So if I had a box that was at 100 with a width of 10 and I clicked at 105, then this check would fail.
105 >= 100 && 105 < 10 // .... ???
And then there's this...
mouseY >= topLeft.getY() && mouseY <= topLeft.getY()
Take a moment to check the last condition...You'd have to click EXACTLY on the top edge of the block for this condition to be true.
I would do one of two things.
Either I would write a method that performs this calculation for any block...
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();
}
That way, if there is ever a bug in the code, it's only in one place...
OR (preferrably), I would extended the Block
from Rectangle
, this way I could simply use the contains
method instead...
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;
}
}
And in your mouse clicked event handler...
if (topLeft.contains(e.getPoint()) { ... }
Upvotes: 5
Reputation: 1314
Upvotes: 0