# Change color on button event



## HiImSkulte (21. Dez 2016)

*Hello guys!*
I have a question, I want to change the color of a button on a JFrame when the player clicks on it (I already defined the location in the mousePressed() and mouseReleased() Method, here the code.
*Here my render() Method:*


```
public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }
       
        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = 0xff00ff;
        }
       
        screen.clear(0);
        screen.render(0);

        Graphics g = bs.getDrawGraphics();
            g.create();
            g.drawImage(image, 0, 0, width, height, null);
               
            // MAIN BUTTON
                g.setColor(new Color(cwhite));
                g.fillRect(300, 200, rectWidth, rectHeight);
                g.setColor(new Color(cblack));
                g.setFont(new Font("Arial", 0, 14));
                g.drawString("Click Me!", 320, 250);
               
            // UPGRADE BUTTON
                g.setColor(new Color(cwhite));
                g.fillRect(610, 50, 150, 50);
                g.setColor(new Color(cblack));
                g.setFont(new Font("Arial", 0, 14));
                g.setColor(new Color(0));
                g.drawString("Upgrade your Yee!", 625, 80);
               
            // DOUBLECOIN BUTTON
                g.setColor(new Color(cwhite));
                g.fillRect(50, 100, 125, 50);
                g.setColor(new Color(cblack));
                g.setFont(new Font("Arial", 0, 14));
                g.drawString("Double your Coins!", 50, 125);
                g.drawString(String.valueOf(rate), 50, 150);
               
            // COINS
                g.setFont(new Font("Arial", 0, 25));
                g.setColor(new Color(cwhite));
                g.drawString(String.valueOf(counter), 50, 50);
               
            // LINE
                g.drawLine(575, 0, 575, 600);
               
               
           
           
           
        g.dispose();
        bs.show();
    }
```

*And here my mouse Events:
*

```
@Override
    public void mousePressed(MouseEvent e) {
        if ((mousex > 610 && mousex <= 760) && (mousey >= 50 && mousey <= 100)) {
           
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if ((mousex > 610 && mousex <= 760) && (mousey >= 50 && mousey <= 100)) {
           
        }
    }
```

I don't know how to change the color of the "Upgrade" Button now.
Thanks for help! Bye!


----------



## Flown (21. Dez 2016)

Simplest way would be setting a flag, like:

```
@Override
public void mousePressed(MouseEvent e) {
  if ((mousex > 610 && mousex <= 760) && (mousey >= 50 && mousey <= 100)) {
    upgradePressed = true;
    repaint();
  }
}

@Override
public void mouseReleased(MouseEvent e) {
  if ((mousex > 610 && mousex <= 760) && (mousey >= 50 && mousey <= 100)) {         
    upgradePressed = false;
    repaint();
  }
}
```
You also need to change the render method to assign a different color:

```
// Drawing the upgrade button
if(upgradePressed) {
  g.setColor(Color.GREEN);
} else {
  g.setColor(Color.BLUE);
}
g.drawRect(...);
```


----------



## HiImSkulte (22. Dez 2016)

Thanks! I first tried it with making a new color variable for each button but this is way easier!
*
- Closed -*


----------

