| | |
Applet Help
Thread Solved |
•
•
Join Date: Dec 2005
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Java Applet viewer blinks everytime the init method is used. (Java)
- Java Game Applet Too Small (Java)
- Problem with Yahoo chat applet. (Windows NT / 2000 / XP)
- help!! My applet can't run... Urgent!! (Java)
- where to get the java applet? (Web Browsers)
- applet crashed (Windows NT / 2000 / XP)
- Applet or Application? (Java)
Other Threads in the Java Forum
- Previous Thread: JFrame Resizing
- Next Thread: Errors with JPanel
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows working





