Results 1 to 2 of 2
  1. #1
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Rep Power

    Default change object color on mouse click

    I have been given an assignment where I need to make an applet that draws a house, and upon mouse click of either the door or two windows, that part of the drawing becomes completely black, while the rest stays unfilled. For example, if one of the windows were clicked only that window became completely black. I've got the house to appear fine by trial and error but I'm having troubles with the ActionListener performing the repaint. So far, I have a conditional statement that relies on coordinates when the mouse is clicked. How can I get this to work right?

    My code looks like this:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class House extends JApplet
    	private int currentx = 0;
    	private int currenty = 0;
    	public void init()
    		public void paint(Graphics g)
    			g.drawLine(156, 99, 0, 150);
    			g.drawLine(156, 99, 299, 150);
    			g.drawLine(0, 150, 299, 150);
    			g.drawRect(3, 150, 296, 110);
    			g.drawRect(50, 155, 50, 70);
    			g.drawLine(50, 185, 100, 185);
    			g.drawLine(75, 155, 75, 225);
    			g.drawRect(120, 155, 50, 100);
    			g.fillOval(158, 200, 5, 10);
    			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)
    				currentx = e.getX();
    				currenty = e.getY();
    				if (currentx >= 50 && currentx < 77 && currenty >= 155 && currenty <= 225)
    						g.fillRect(50, 155, 50, 70);
    			public void mouseReleased(MouseEvent e)
    			public void mouseEntered(MouseEvent e)
    			public void mouseExited(MouseEvent e)
    For now, the main problem is getting this compiled, but since syntax errors and runtime errors are not the same I want this to work the way it should at run time. Else is nulled, because I want to make sure it works with one, before going through the else-if coding.

    When I compile it in Terminal, It gives me this: cannot find symbol
    symbol : variable g
    location: class House.MyMouseListener
    g.fillRect(50, 155, 50, 70);
    1 error
    "g" comes from the paint method of the earlier class.
    Last edited by gotenks05; 04-05-2009 at 05:03 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Myself, I'd do my drawing in a JPanel or JComponent, override the paintComponent method, not paint, and then add this JPanel or JComponent to the JApplet's contentPane. Note that this is a general recommendation for good coding practice here but does not answer your main question.

    All the drawing should be done within the paintComponent (here your paint) method. You could have boolean variables such as one called "fillDoor" that the paintComponent (or paint) method uses in an if condition to see if the door should be filled or not. Something like so:
    Java Code:
    // this is inside of the paint or 
    // paintComponent method
    if (fillDoor)
    Then in your mouselistener, if the door has been clicked, set the fillDoor boolean to true and call repaint on the JPanel.

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  2. Replies: 6
    Last Post: 03-17-2009, 02:23 AM
  3. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 04:43 PM
  4. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 10:42 PM
  5. Problem in mouse click n repaint
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-04-2008, 11:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts