I’m working on a homework assignment to draw a house in a java applet. The house has three rectangles within a large main rectangle representing a door and two windows. I need the windows to change color when clicked on, I’ve reached the point where I have drawn the house and have the doors and windows drawn as well but I am not able to change the color of them based on clicking the mouse in them. I’m having some trouble determining why this is the case.
To summarize, the house is drawn; door and window rectangles are drawn and filled in black. When clicking on any of the window or door rectangles nothing occurs, no errors and no change in color.
Code follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class DrawHouse extends JApplet implements MouseListener
{
private int mX; //variable to hold x position of the mouse cursor when clicked
private int mY; //variable to hold y position of the mouse cursor when clicked
private int rect1x;
private int rect1y;
private int rect1w;
private int rect1h;
private int rect2x;
private int rect2y;
private int rect2w;
private int rect2h;
private int rect3x;
private int rect3y;
private int rect3w;
private int rect3h;
boolean rect1Clicked;
boolean rect2Clicked;
boolean rect3Clicked;
public void init()
{
super.init();
}
public void paint(Graphics g)
{
super.paint(g);
Polygon pg = new Polygon();
pg.addPoint(280, 200);
pg.addPoint(470, 100);
pg.addPoint(670, 200);
g.drawPolygon(pg);
g.setColor(Color.BLACK);
g.drawRect(300, 200, 350, 300);
g.fillRect(350, 300, 50, 100);
g.fillRect(550, 300, 50, 100);
g.fillRect(440, 300, 75, 200);
addMouseListener(this);
if(rect1Clicked || rect2Clicked || rect3Clicked)
{
g.setColor(Color.GRAY);
g.clearRect(rect1x, rect1y, rect1w, rect1h);
g.fillRect(rect1x, rect1y, rect1w, rect1h);
g.setColor(Color.GRAY);
g.clearRect(rect2x, rect2y, rect2w, rect2h);
g.fillRect(rect2x, rect2y, rect2w, rect2h);
g.setColor(Color.GRAY);
g.clearRect(rect3x, rect3y, rect3w, rect3h);
g.fillRect(rect3x, rect3y, rect3w, rect3h);
}
}
@Override
public void mouseClicked(MouseEvent e)
{
rect1x = 350;
rect1y = 300;
rect1w = 350;
rect1h = 300;
rect2x = 550;
rect2y = 300;
rect2w = 50;
rect2h = 100;
rect3x = 440;
rect3y = 300;
rect3w = 75;
rect3h = 200;
mX = e.getX();
mY = e.getY();
if(mX > rect1x && mX < rect1x + rect1w && mY > rect1y && mY < rect1y + rect1h)
{
rect1Clicked = true;
}
else
{
rect1Clicked = false;
}
if(mX > rect2x && mX < rect2x + rect2w && mY > rect2y && mY < rect2y+rect2h)
{
rect2Clicked = true;
}
else
{
rect2Clicked = false;
}
if(mX > rect3x && mX < rect3x + rect3w && mY > rect3y && mY < rect3y + rect3h)
{
rect3Clicked = true;
}
else
{
rect3Clicked = false;
}
}
}
Thanks in advance for any suggestions.
You’re really making life very difficult for yourself. The Java Graphics API has a number of classes specifically suited for solving this problem.
As a general rule of thumb. NEVER override any of the
paintmethods of top level containers. Use an appropriate component, such asJPanelorJComponent.Where possible, override the
paintComponentmethod instead.As HoverCraft has pointed out, DON’T modify the UI from within the
paintmethods, this includes adding listeners. Thepaintmethod will get called lots of times, meaning each time it’s called, you will register yet ANOTHER listener…You will want to start by having a read through the 2D Graphics trail and the Performing Custom Painting trail
While the example below uses a
JFrame, the basic principles apply.