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.