left-click revert - rectangle, mouseClickedEvent
<p>I've created a basic Java program that creates a rectangle on startup and every time it is clicked, the rectangle grows and changes to a different (random) color.  here's my code:</p>

<pre><code>package rectPAK;

import javax.swing.JFrame;

public class DisplayRect {

public static void main(String[] args) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(0,0,1000,1000);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
    }
}
</code></pre>

<p>and then myCanvas is this:</p>

<pre><code>package rectPAK;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JComponent;

public class MyCanvas extends JComponent{
int W = 100;
int H = 100;
int r;
int g;
int b;
int trans;
int maxRandNum = 255;
int xPoint;
int yPoint;


public MyCanvas() {
    this.addMouseListener(m);
    this.addMouseMotionListener(ml);
}

Random rand = new Random();

MouseListener m = new MouseListener() {

    @Override
    public void mouseClicked(MouseEvent e) {

        W = W + 20;
        H = H + 20;
        r = rand.nextInt(maxRandNum);
        g = rand.nextInt(maxRandNum);
        b = rand.nextInt(maxRandNum);
        trans = rand.nextInt(maxRandNum);

        repaint();

    }

    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent e) {


    }

    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

};

MouseMotionListener ml = new MouseMotionListener() {

    @Override
    public void mouseDragged(MouseEvent e) {

        Point p = e.getLocationOnScreen();
        xPoint = p.x;
        yPoint = p.y;

        repaint();

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

};

public void paint(Graphics gr) {
    gr.setColor(Color.BLACK);
    gr.drawRect(xPoint, yPoint, W, H);
    gr.setColor(new Color(r,g,b,trans));
    gr.fillRect(xPoint, yPoint, W, H);
}

}
</code></pre>

<p>now my question is this: how do i make it so that when i right-click on the rectangle, it reverts to the previous size and color? i Know it's a lot to ask, but i cant find anything about it...</p>

<p>Thanks a lot.</p>
