Changing color of a shape multiple times with the same Jbutton

I am trying to figure out how to change the color of a shape from one color to another, to another, then back to the original color. Let's say from Red to yellow to green and back to red. I've gotten it so I can change the color the first click, but then can't get it to change again to a different color. It will only change back to the original if the shape is no longer "selected." How to I get a JButton to perform different events each new time its clicked? Here is what I have for the listeners:

(EDITED) Here is what I have so far (sorry it's a little long, but you asked for something compilable):

public class TestFace extends JFrame {

//Instance Variables
private static Face mugShot = new Face();
private JScrollBar scrollBar = new JScrollBar (JScrollBar.HORIZONTAL);
private MessagePanel messagePanel = new MessagePanel ("Face Class");

//Source    
public TestFace () {

    JButton colorChange = new JButton ("Change Color");

    //add buttons to panel
    JPanel panel = new JPanel ();
    panel.add(messagePanel, BorderLayout.CENTER);
    panel.add(colorChange);
    add(scrollBar, BorderLayout.SOUTH);


    add(panel);

    //Register Listeners
    ColorListenerClass listener = new ColorListenerClass();

    colorChange.addActionListener(listener);

    scrollBar.addAdjustmentListener(new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            double value = scrollBar.getValue(); //returns current value of scrollbar 0-100
            double maximumValue = scrollBar.getMaximum();
            double newX = (value * messagePanel.getWidth() / maximumValue);
            messagePanel.setXCoordinate((int)newX);

        }//end adjustment Value Changed

    });//end adjustment listener 


    //register mouse click activity
    addMouseMotionListener(new MouseMotionAdapter() {
        @Override public void mouseDragged (MouseEvent event) {
            mugShot.selectShapeUnder(event.getX(), event.getY());
            repaint();
        }
    });

    addMouseListener(new MouseAdapter() {
        @Override public void mousePressed (MouseEvent event) {
            mugShot.selectShapeUnder(event.getX(), event.getY());
            repaint();
        }
    });

}//end cons


/*************EnlargeListener Class**********************************/
class ColorListenerClass implements ActionListener{


    @Override// necessary to respond to the event
    public void actionPerformed (ActionEvent e) {
        if (mugShot.getSelected() == mugShot.getFace()){
            //System.out.println("Face is selected");
            mugShot.setColorFace(Color.blue);
            repaint();

        }//end if
        else if (mugShot.getSelected() == mugShot.getMouth()){
            System.out.println("Mouth is selected");
            mugShot.setColorMouth(Color.yellow);
            repaint();
        }//end if
        else if  (mugShot.getSelected() == mugShot.getEyeLeft() || mugShot.getSelected() == mugShot.getEyeRight()){
            System.out.println("Eyes are selected");
            mugShot.setColorEyes(Color.ORANGE);
            repaint();
        }//end if
        if (mugShot.getSelected() == null) { 
            JOptionPane.showMessageDialog(null, "Nothing is Selected");
            repaint();
        }


    }//end actionPerformed

}//end OKListenerClass
/********************************************************************/


//////////////MAIN////////////////////////////////////////    
public static final void main (String[] args) {

    JFrame frame = new JFrame();
    frame.setTitle("Mugshot");
    frame.add(mugShot);
    frame.setSize(500,400);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); 

    //draw toolbar panel
    JFrame frame1 = new TestFace ();
    frame1.setTitle("Toolbar");
    frame1.setSize(200,150);
    frame1.setLocation(200,100);
    frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame1.setVisible(true);  


}//end main

}//end face class

and the actual shape class:

    public class Face extends JPanel{


///Instance Variables   
private final Shape face = new Ellipse2D.Float(100, 20, 300, 300);//face component
    private final Shape eyeLeft = new Ellipse2D.Float(152,85, 80, 55);//left eye
    private final Shape eyeRight = new Ellipse2D.Float(265,85, 80, 55);//right eye
    private final Shape pupilRight = new Ellipse2D.Float(285, 85, 40, 55);//right pupil
    private final Shape pupilLeft = new Ellipse2D.Float(172, 85, 40, 55);//left pupil
    private final Shape mouth = new Rectangle2D.Float(180, 230, 140, 20);//mouth
private Shape selected = null;

Color colorMouth = Color.red;
Color colorFace = Color.green;
Color colorEyes = Color.white;



public Color getColorMouth() {
    return colorMouth;
}

public void setColorMouth(Color colorMouth) {
    this.colorMouth = colorMouth;
}

public Color getColorEyes() {
    return colorEyes;
}

public void setColorEyes(Color colorEyes) {
    this.colorEyes = colorEyes;
}

public Color getColorFace() {
    return colorFace;
}

public void setColorFace(Color colorFace) {
    this.colorFace = colorFace;
}

public Face () {


    //register mouse click activity
    addMouseMotionListener(new MouseMotionAdapter() {
        @Override public void mouseDragged (MouseEvent event) {
            selectShapeUnder(event.getX(), event.getY());
            repaint();
        }
    });
    addMouseListener(new MouseAdapter() {
        @Override public void mousePressed (MouseEvent event) {
            selectShapeUnder(event.getX(), event.getY());
            repaint();
        }
    });

}

 protected void paintComponent (Graphics g) {
     super.paintComponent(g);  
     Graphics2D graphics = (Graphics2D)g;

     graphics.setColor(colorFace);
     graphics.fill(face);

     graphics.setColor(colorMouth);
     graphics.fill(mouth);

     graphics.setColor(colorEyes);
     graphics.fill(eyeLeft);
     graphics.fill(eyeRight);

     graphics.setColor(Color.BLACK);
     graphics.fill(pupilLeft);
     graphics.fill(pupilRight);
     g.drawLine(220, 185, 270, 185);
     g.drawLine(220, 185, 260, 130);



}//end pC

    public Shape getSelected() {
        return selected;
    }//end getSelected

    public void setSelected(Shape selected) {
        this.selected = selected;
    }//end setSelected

    public Shape getFace() {
        return face;
    }//end getFace

    public Shape getEyeLeft() {
        return eyeLeft;
    }//end getEyeLeft

    public Shape getEyeRight() {
        return eyeRight;
    }//end getRightEye

    public Shape getMouth() {
        return mouth;
    }//end getMouth


    public void selectShapeUnder (int x, int y) {
        Shape oldSelected = selected;

        if (eyeLeft.contains(x, y)){
            selected = eyeLeft; 
        }//end if 
        else if (eyeRight.contains(x, y)){
            selected = eyeRight;    
        }//end else if
        else if (mouth.contains(x, y)){
            selected = mouth; 
        }//end else if
        else if (face.contains(x, y)) {
            selected = face;
        }//end else if
        else
            selected = null;
        if (selected != oldSelected)
            repaint();
    }//end selectShapeUnder


}//end Face class

Answers


A fancy way would be to keep an ArrayList of all the colors that your want to use for each face part. For example:

List<Color> faceColors = new ArrayList<Color>();

Then your component that paints the face could have a method:

public void addFaceColor(Color color)
{
    faceColors.add(color);
}

So in your program you can add as many different colors as you want.

Then the painting code for the face could be something like:

graphics.setColor(faceColors.isEmpty() ? getForeground() : faceColors.get(0));
graphics.fill(face);

To change the face color you can create a method like the following that would be invoked when you click your button:

public void nextFaceColor()
{
    faceColors.add( faceColors.remove(0) );
    repaint();
}

Need Your Help

Recording android events for playback

java android automation

I would like to write an application that records a sequence of events in android and save them in order to do stuff like playback. For example, i can record a sequence of events when i open the se...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.