Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gotenks05

macrumors member
Original poster
Jan 1, 2009
78
0
I have been given an assignment where I need to make an Applet that draws a house and if you click on either the door, right window, or left window, that object becomes black. I've got the house to be drawn fine by trial and error and I got the mouse events to work, but it is not working correctly. When I run a test, it fills the whole applet instead of that shape with all black. How do I get this to work properly?

This is the code I typed up:

Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class House extends Applet
{
	public void init()
	{
		setBackground(Color.WHITE);
		addMouseListener(new MyMouseListener());
	}
		public void paint(Graphics g)
		{

			super.paint(g);
			// roof
			g.drawLine(156, 99, 0, 150);
			g.drawLine(156, 99, 299, 150);
			g.drawLine(0, 150, 299, 150);

			// base
			g.drawRect(3, 150, 296, 110);

			// window left
			g.drawRect(50, 155, 50, 70);
			g.drawLine(50, 185, 100, 185);
			g.drawLine(75, 155, 75, 225);

			// door
			g.drawRect(120, 155, 50, 100);
			g.fillOval(158, 200, 5, 10);

			// window right
			g.drawRect(200, 155, 50, 70);
			g.drawLine(200, 185, 250, 185); 
			g.drawLine(225, 155, 225, 225);

		}

		private class MyMouseListener implements MouseListener
		{
			public void mousePressed(MouseEvent e)
			{
			}
			
			public void mouseClicked(MouseEvent e)
			{
				int currentx = e.getX();
				int currenty = e.getY();

				boolean WindowLeft = (currentx >= 50 && currentx < 77 && currenty >= 155 && currenty <= 225);
				if (WindowLeft)
				{
					setBackground(Color.BLACK);
					repaint();
				}

				else;
			}

			public void mouseReleased(MouseEvent e)
			{
			}

			public void mouseEntered(MouseEvent e)
			{
			}

			public void mouseExited(MouseEvent e)
			{
			}
		}	
}

When I asked on another forum, somebody suggested using the paintComponent() method, but I could not compile it and did not know how to use that method. The code used to extend the JApplet class, but it was changed to the Applet class. Also, when I type

Code:
g.setBackground(Color.BLACK);

The compiler does not know that it has been used already, because it says variable "g" cannot be found. I also tried

Code:
House g.setBackground(Color.BLACk);

This at least recognized the "g" variable, but it wanted a semi-colon to be entered.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Well if you're setting the background color then the entire background will fill that to color. You need to figure out a way to get the shape that is clicked on, store it, and use it in the paint() method, and only fill that shape instead of everything. You could try the setColor() method on g inside the paint() method, only when you're going to draw the shape that was clicked.

The reason g isn't available in the mouseClicked() method is because it's not declared there. It's only declared locally to the paint() method, so it's only available inside the paint() method.
 

gotenks05

macrumors member
Original poster
Jan 1, 2009
78
0
I tried putting the paint() method there, but it kept complaining about illegal expression. Also, if I were to use setColor in that, it would color it as it was first drawn instead of after a mouse click and using the repaintt() method.
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
draw a filled rectangle (Graphics.fillRect(...)) at the correct location and redraw everything.

To speed it up you can keep a vector of Graphics with everything predrawn that you simply paint on the canvas - quick and simple :)

(edit: faults in reference)
 

darrenscerri

macrumors regular
Nov 15, 2007
185
0
Malta
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class House extends Applet
{
	boolean winleft, winright, roof, base, door;
	public void init()
	{
		winleft = winright = roof = base = door = false;
		setBackground(Color.WHITE);
		addMouseListener(new MyMouseListener());
	}
	
		public void paint(Graphics g)
		{

			super.paint(g);
			// roof
			
			if (roof) {
				g.fillPolygon(new int[] {156,0,299}, new int[] {99,150,150}, 3);
			}
			
			if (winleft) {
				g.fillRect(50, 155, 50, 70);
			}
			
			if (winright) {
				g.fillRect(200, 155, 50, 70);
			}
			
			if (door) {
				g.fillRect(120, 155, 50, 100);
			}
			
			// window left
			g.drawLine(156, 99, 0, 150);
			g.drawLine(156, 99, 299, 150);
			g.drawLine(0, 150, 299, 150);
			

			// base
			g.drawRect(3, 150, 296, 110);

			
			g.drawRect(50, 155, 50, 70);
			g.drawLine(50, 185, 100, 185);
			g.drawLine(75, 155, 75, 225);

			// door
			g.drawRect(120, 155, 50, 100);
			g.fillOval(158, 200, 5, 10);

			// window right
			g.drawRect(200, 155, 50, 70);
			g.drawLine(200, 185, 250, 185); 
			g.drawLine(225, 155, 225, 225);

		}

		private class MyMouseListener implements MouseListener
		{
			public void mousePressed(MouseEvent e)
			{
			}
			
			public void mouseClicked(MouseEvent e)
			{
				int currentx = e.getX();
				int currenty = e.getY();

				boolean WindowLeft = (currentx >= 50 && currentx < 77 && currenty >= 155 && currenty <= 225);
				if (WindowLeft)
				{
					winleft = true;
					repaint();
				}
				
				boolean Roof = (currentx >= 0 && currentx < 299 && currenty >= 99 && currenty <= 150);
				if (Roof) {
					roof = true;
					repaint();
				}
					
				boolean Door = (currentx >= 120 && currentx < 170 && currenty >= 155 && currenty <= 255);
				if (Door) {
					door = true;
					repaint();
				}
				
				boolean WindowRight = (currentx >= 200 && currentx < 270 && currenty >= 155 && currenty <= 210);
				if (WindowRight)
				{
					winright = true;
					repaint();
				}

				else;
			}

			public void mouseReleased(MouseEvent e)
			{
			}

			public void mouseEntered(MouseEvent e)
			{
			}

			public void mouseExited(MouseEvent e)
			{
			}
		}	
}

Hope you get the idea now! It's better if you take a more object oriented approach, after all that's what Java is all about. You should create the windows, doors etc as objects with methods such as drawAt(int, int), getCoordinates() etc. Good Luck!
 

gotenks05

macrumors member
Original poster
Jan 1, 2009
78
0
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class House extends Applet
{
	boolean winleft, winright, roof, base, door;
	public void init()
	{
		winleft = winright = roof = base = door = false;
		setBackground(Color.WHITE);
		addMouseListener(new MyMouseListener());
	}
	
		public void paint(Graphics g)
		{

			super.paint(g);
			// roof
			
			if (roof) {
				g.fillPolygon(new int[] {156,0,299}, new int[] {99,150,150}, 3);
			}
			
			if (winleft) {
				g.fillRect(50, 155, 50, 70);
			}
			
			if (winright) {
				g.fillRect(200, 155, 50, 70);
			}
			
			if (door) {
				g.fillRect(120, 155, 50, 100);
			}
			
			// window left
			g.drawLine(156, 99, 0, 150);
			g.drawLine(156, 99, 299, 150);
			g.drawLine(0, 150, 299, 150);
			

			// base
			g.drawRect(3, 150, 296, 110);

			
			g.drawRect(50, 155, 50, 70);
			g.drawLine(50, 185, 100, 185);
			g.drawLine(75, 155, 75, 225);

			// door
			g.drawRect(120, 155, 50, 100);
			g.fillOval(158, 200, 5, 10);

			// window right
			g.drawRect(200, 155, 50, 70);
			g.drawLine(200, 185, 250, 185); 
			g.drawLine(225, 155, 225, 225);

		}

		private class MyMouseListener implements MouseListener
		{
			public void mousePressed(MouseEvent e)
			{
			}
			
			public void mouseClicked(MouseEvent e)
			{
				int currentx = e.getX();
				int currenty = e.getY();

				boolean WindowLeft = (currentx >= 50 && currentx < 77 && currenty >= 155 && currenty <= 225);
				if (WindowLeft)
				{
					winleft = true;
					repaint();
				}
				
				boolean Roof = (currentx >= 0 && currentx < 299 && currenty >= 99 && currenty <= 150);
				if (Roof) {
					roof = true;
					repaint();
				}
					
				boolean Door = (currentx >= 120 && currentx < 170 && currenty >= 155 && currenty <= 255);
				if (Door) {
					door = true;
					repaint();
				}
				
				boolean WindowRight = (currentx >= 200 && currentx < 270 && currenty >= 155 && currenty <= 210);
				if (WindowRight)
				{
					winright = true;
					repaint();
				}

				else;
			}

			public void mouseReleased(MouseEvent e)
			{
			}

			public void mouseEntered(MouseEvent e)
			{
			}

			public void mouseExited(MouseEvent e)
			{
			}
		}	
}

Hope you get the idea now! It's better if you take a more object oriented approach, after all that's what Java is all about. You should create the windows, doors etc as objects with methods such as drawAt(int, int), getCoordinates() etc. Good Luck!

Thanks, this has helped me a lot. As for the rest of your post, I would have liked to do a seperate class, but I'm not too sure how similar and/or different it is using in a program using the Applet class, although it seemed to work well with the JApplet class, when converting a program to an applet. My programming instructor just decided to do Applet programming before GUI programming for some reason.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.