user3077551
user3077551

Reputation: 79

How can I change the variable of a color?

I am trying to current make a JFrame and when the user clicks a button the circle that is displayed in the JFrame changes color every second. But at the moment I am having trouble changing the color that is currently being displayed in the window, stored in a variable.

Color lastColor = Color.ORANGE;            
g.setColor(lastColor);

smallerButton.addActionListener(new ActionListener()
{
      public void actionPerformed(ActionEvent e)
      {
            String action = e.getActionCommand();
            if (action.equals("Flash")) {

                  //when clicked change color of the circle listed above.
                  //or change the variable of last color.
            }
      }});
     }
};

This isn't all of the code. All I am trying to do is when the user clicks the button the variable lastColor is then changed to let say GRAY. I am having trouble trying to do this as when I put the variable name in the action listener it cannot find the variable lastColor to change to the new variable. How can I change the variable lastColor in the action listener?

Upvotes: 0

Views: 207

Answers (2)

nIcE cOw
nIcE cOw

Reputation: 24616

You just need to create one instance variable as:

private Color backColor;

And on the click of a JButton, simply call a method, that will change the value of this variable, and call repaint (), for the view to change the colour of the circle drawn.

Here is a small example:

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

public class ColourExample {

    private JPanel drawingBoard;
    private JButton button;
    private JPanel customPanel;

    private static final int GAP = 5;

    private Color [] colours = {
        Color.red,
        Color.blue,
        Color.cyan,
        Color.magenta,
        Color.gray
    };
    private int counter;

    public ColourExample () {
        counter = 0;
    }

    private void displayGUI () {
        JFrame frame = new JFrame ( "" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = new JPanel ();
        contentPane.setLayout ( new BorderLayout ( GAP, GAP) );

        customPanel = new CustomPanel ();
        contentPane.add ( customPanel, BorderLayout.CENTER );

        button = new JButton ( "Change Colour" );
        button.addActionListener ( new ActionListener () {
            @Override
            public void actionPerformed ( ActionEvent ae ) {
                System.out.println ( "Counter: " + counter );
                ( ( CustomPanel ) customPanel ).setValues ( colours [ counter++ ] );
                counter %= colours.length;
            }
        } );
        contentPane.add ( button, BorderLayout.PAGE_END );

        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );
    }

    public static void main ( String [] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new ColourExample ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}

class CustomPanel extends JPanel {

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;
    private static final int RADIUS = 400;

    private Color backColour;

    public CustomPanel ( ) {
        setOpaque ( true );
        backColour = Color.green;       
    }

    public void setValues ( Color backColour ) {
        this.backColour = backColour;
        repaint ();
    }

    @Override
    public Dimension getPreferredSize () {
        return new Dimension ( WIDTH, HEIGHT );
    }

    @Override
    protected void paintComponent ( Graphics g ) {
        super.paintComponent ( g );
        g.setColor ( backColour );
        g.fillOval ( 0, 0, RADIUS, RADIUS );
    }
}

Upvotes: 0

Murat Karagöz
Murat Karagöz

Reputation: 37584

You have to declare lastColor as a member variable of your class. You are creating it locally and thus the clickListener can't see it.

EDIT:

public class foo(){
Color lastColor;

public foo(){
lastColor = Color.ORANGE();
}

public void paintFoo(){
 // do your paint stuff here and access lastColor
}

}

Upvotes: 1

Related Questions