I'm working on this code in which I need to draw a house. The house has a door(rectangle) that randomly changes color with a mouse click. Everything else besides the door doesn't change color at all.
How to make the color of the door change on mouse click?
package finalExam;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Final extends JFrame{
public Final(){
setTitle("An Interactive House");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
int [] xCoords = { 40, 250, 460 };
int [] yCoords = { 200, 50, 200 };
super.paint(g);
g.drawRect(80, 200, 330, 260);
g.drawPolygon(xCoords , yCoords , 3);
g.setColor(Color.black);
g.fillRect(190, 330, 100, 130);
addMouseListener(new MouseEventListener());
g.setColor(Color.white);
g.fillOval(280, 400, 5, 5);
}
private class MouseEventListener implements MouseListener{
public void mouseClicked(MouseEvent arg0){
repaint();
}
public void mouseEntered(MouseEvent arg0){
}
public void mouseExited(MouseEvent arg0){
}
public void mousePressed(MouseEvent arg0){
}
public void mouseReleased(MouseEvent arg0){
}
}
public static void main(String[] args){
new Final();
}
}