944,162 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3281
  • Java RSS
Dec 3rd, 2005
0

Applet Help

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjd_1986 is offline Offline
3 posts
since Dec 2005
Aug 2nd, 2010
1
Re: Applet Help
Did you ever figure out the solution to this? I am in the same boat as you and dont know where to go from here.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ttunstall07 is offline Offline
9 posts
since Feb 2010
Dec 8th, 2010
-1

House applet

Chris, I just copied and pasted your class to Textpad and tried to compile it and it doesn't. It keep saying identifier expected and illegal character(2 errors) I am in CIT-130 now and trying to write house applet but have no clue how to do it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Valeriya is offline Offline
2 posts
since Dec 2010
Dec 8th, 2010
-1

house applet

Did you ever figure out the solution to this? I am in the same boat as you and dont know where to go from here.
Did you ever write this applet? Please help me out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Valeriya is offline Offline
2 posts
since Dec 2010
Feb 28th, 2011
0
Re: Applet Help
Did you ever figure out the solution to this? I am in the same boat as you and dont know where to go from here.
Yep a very long time ago.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjd_1986 is offline Offline
3 posts
since Dec 2005
Feb 28th, 2011
0
Re: Applet Help
Click to Expand / Collapse  Quote originally posted by Valeriya ...
Chris, I just copied and pasted your class to Textpad and tried to compile it and it doesn't. It keep saying identifier expected and illegal character(2 errors) I am in CIT-130 now and trying to write house applet but have no clue how to do it.
Should compile if your class' named the same and all. Did that proj 6 years ago lol.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjd_1986 is offline Offline
3 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Stack-Symbol Checker
Next Thread in Java Forum Timeline: Serial Port comm with java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC