I have to write this applet for my CIT 130 class. I understand that you can not give solutions nor do I want you to do my homework for me I just need some pointers. The questions ask to "Write an applet that draws a house with it's door and windows open... Now when the user clicks on the door or windows they should close." I have been able to do this much:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* To draw a house and when the user clicks on the door or windows they close.
*
* @author Chris Darlak
* @version 12/01/2005
*/
public class HouseApplet extends JApplet
{
// instance variables - replace the example below with your own
private int x;

/**
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init()
{
// Set the background color to white.
getContentPane().setBackground(Color.white);

// Add a mouse listener to this applet.
addMouseListener(new MyMouseListener());
}
/**
* Paint method for applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
// Call the superclass paint method.
super.paint(g);

// Draw the house.
g.setColor(Color.black);
g.drawRect(100, 100, 200, 100);

// Draw the left window open.
g.fillRect(120, 130, 40, 40);

// Draw the right window open.
g.fillRect(240, 130, 40, 40);

// Draw the door open.
g.fillRect(180, 130, 40, 70);

// Draw the entire roof.
g.drawLine(80, 100, 320, 100);
g.drawLine(80, 100, 200, 40);
g.drawLine(200, 40, 320, 100);

// Draw the left window closed.
g.setColor(Color.white);
g.fillRect(120, 130, 40, 40);
g.setColor(Color.black);
g.drawRect(120, 130, 40, 40);
g.drawLine(140, 130, 140, 170);
g.drawLine(120, 150, 160, 150);

// Draw the right window closed.
g.setColor(Color.white);
g.fillRect(240, 130, 40, 40);
g.setColor(Color.black);
g.drawRect(240, 130, 40, 40);
g.drawLine(260, 130, 260, 170);
g.drawLine(240, 150, 280, 150);

// Draw the door closed.
g.setColor(Color.white);
g.fillRect(180, 130, 40, 70);
g.setColor(Color.black);
g.drawRect(180, 130, 40, 70);
g.fillOval(210, 165, 07, 07);
}
private class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
// Repaint the window.
repaint();
}
}
}
everythings lined up in my program just pasting it messed the indentation up. My question is above on the mouse adapter. I really do not understand how to make it replace the open windows and door with the closes ones when each is clicked. I currently have it painting the open version then painting right over with the closes and the mouse click is just set to repaint the whole house but I really have no clue on how to go about implementing a solution. The graphics are done I just need to program the mouse click to replace the windows and door plus you have to click in the area that the windows/door are in to change the picture so it has to have some kind of area restrictions. Also it would be able to keep opening and closing the door/windows every click. Should I use a loop or an array maybe both? Any help would be much appreciated. Thank You.

I copied this from another thread that was already marked as solved, but no one posted any answers to the question. I am in the same situation and have no idea how to get the windows to turn black when clicked, and then white when clicked again. Anything sending me in the right direction would be awesome. Thanks.

how to get the windows to turn black when clicked, and then white when clicked again.

Sounds like you need a flag to tell the paint() method what color to paint the windows.
One setting for black and one for white.
Change the setting when you get a click and call repaint() which will call the paint() method which can look at the new setting of the flag and paint with the correct colors.

ok, and what im trying to do so that the windows will only turn black or white if the users actually clicks on them is using the getX() and getY() functions, problem is, i dont know how to find the proper coordinates that the windows or door are at to plug into the if statement. if the x,y coordinates of the window are set at 130 and 120, does that mean that is the center of the window?

Read the API doc for where the x, y values are for a rectangle being drawn.
Normally they are the upper left corner.

To see where the mouse click is print out the x,y for where you click the mouse and compare that to where the cursor was relative to the shape on the screen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.