Results 1 to 2 of 2
- 04-05-2009, 05:00 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 34
- Rep Power
- 0
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:
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.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() { getContentPane().setBackground(Color.WHITE); } 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); repaint(); } else; } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } }
When I compile it in Terminal, It gives me this:
"g" comes from the paint method of the earlier class.House.java:43: cannot find symbol
symbol : variable g
location: class House.MyMouseListener
g.fillRect(50, 155, 50, 70);
^
1 errorLast edited by gotenks05; 04-05-2009 at 05:03 PM.
-
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:
Then in your mouselistener, if the door has been clicked, set the fillDoor boolean to true and call repaint on the JPanel.Java Code:// this is inside of the paint or // paintComponent method if (fillDoor) { g.fillRect(....); }
Similar Threads
-
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
change the delay between double click in mouse listener
By itaipee in forum AWT / SwingReplies: 6Last Post: 03-17-2009, 02:23 AM -
mouse click do not work after repaint
By nobody in forum Java 2DReplies: 8Last Post: 12-07-2008, 04:43 PM -
mouse click alert
By amir in forum AWT / SwingReplies: 1Last Post: 08-05-2008, 10:42 PM -
Problem in mouse click n repaint
By Preethi in forum New To JavaReplies: 4Last Post: 07-04-2008, 11:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks