954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Painting Applet

The following applet that I have displays a house with two windows and a door that are colored in black. I want to display these items with window panes in them to show that they are open when the user clicks on them. I know that I need to use a MouseListener that repaints to do so, but do not understand how to set the right coordinates to make that possible...

package hw13;

import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;

/**
* This class is an applet that demonstrates how
* rectangles can be drawn.
*/

public class Main extends JApplet
{

public void init()
{
// Set the background color to white.
getContentPane().setBackground(Color.WHITE);

}


public void paint(Graphics g)
{
super.paint(g);

g.setColor(Color.BLUE);
g.fillRect(5, 5 , 200, 150);
g.setColor(Color.BLACK);
g.fillRect(20,40,35,35);
g.setColor(Color.BLACK);
g.fillRect(140,40,35,35);
g.setColor(Color.BLACK);
g.fillRect(75,75,50,80);
}






}

cproud21
Light Poster
42 posts since Sep 2008
Reputation Points: 4
Solved Threads: 0
 

Here some links, that can solve your problem:
http://www.hostitwise.com/java/japplet.html
http://www.roseindia.net/java/example/java/applet/

and here is alternative decision (if you interested):
http://www.jython.org/applets/coordinates.html
:)

Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You