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);
   }
   
   
   
   

   
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.