Change button color at runtime

4

I asked for help on how to paint a button with 3 different colors: can put 3 colors on a button?

But I'm trying to change its color at runtime and I can not. Same instantiating a new one and replacing it.

The button class is this:

 class MultiColorButton extends JButton {
    Graphics g ;
    public MultiColorButton() {
        setContentAreaFilled(false);

    }
    @Override
    protected void paintComponent(Graphics g) {
        this.g = g;
          int fractionWitdh = 0;     
            fractionWitdh = getWidth();
             g.setColor(Color.GRAY);
             g.fillRect(0, 0, fractionWitdh, getHeight());
        super.paintComponent(g);

    }

    void newcolor()
    {

         int fractionWitdh = getWidth()/3;

        g.setColor(new Color(63, 72, 204));
        g.fillRect(0, 0, fractionWitdh, getHeight());

        g.setColor(new Color(181, 230, 29));
        g.fillRect(fractionWitdh, 0, fractionWitdh, getHeight());

        g.setColor(new Color(237, 28, 36));
        g.fillRect(fractionWitdh * 2, 0, fractionWitdh, getHeight());
        super.repaint();
        super.paintComponent(g);

    }
}

Method newcolor() would be to paint the button with new colors.

Instantiate the button and place it in a vector:

  Botoes[l][c] = new MultiColorButton();

Add it as follows:

 getContentPane().add(Botoes[l][c]);

Then I try to change your Color as follows, but without success.

           Botoes[i][c].newcolor();
           Botoes[i][c].repaint();

I do not know what's missing, as this is my first contact with paint in java.

    
asked by anonymous 08.10.2017 / 20:31

1 answer

5
  

Edit : I've optimized the class so that it is not limited to just 3 colors, but the amount you think is needed.

Just create a method in the MultiColorButton class that receives the color array (or the 3 individual colors), assign them to an array of colors, and force the repaint() of the button shortly after assigning the colors in the array:

class MultiColorButton extends JButton {

    private static final long serialVersionUID = 1L;
    Color[] colors;

    public MultiColorButton() {
        this(new Color[] { new Color(63, 72, 204), new Color(181, 230, 29), new Color(237, 28, 36) });
    }

    public MultiColorButton(Color[] colors) {
        setContentAreaFilled(false);
        setFocusPainted(false);
        this.setColors(colors);
    }

    public void setColors(Color[] colors) {
        this.colors = colors;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {

        int colorsCount = colors.length;

        if (colorsCount > 0) {

            int fractionWitdh = getWidth() / colorsCount;

            for (int i = 0; i < colorsCount; i++) {
                g.setColor(colors[i]);
                g.fillRect(fractionWitdh * i, 0, fractionWitdh, getHeight());
            }
        }

        super.paintComponent(g);
    }
}

To illustrate how to use it, I made this example that generates random colors for the button, each time it is clicked:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.multi.MultiRootPaneUI;
import javax.swing.JButton;
import java.awt.Dimension;

public class JButtonMultipleColorsTest extends JFrame {

    private JPanel contentPane;
    private JPanel panel;
    private MultiColorButton multiColorButton;
    private GradientButton gradientButton;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JButtonMultipleColorsTest frame = new JButtonMultipleColorsTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public JButtonMultipleColorsTest() {
        initComponents();
        pack();
        setLocationRelativeTo(null);
    }

    private void initComponents() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 200));
        this.contentPane = new JPanel();
        this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(this.contentPane);

        this.panel = new JPanel();
        this.contentPane.add(this.panel, BorderLayout.CENTER);

        this.multiColorButton = new MultiColorButton();
        this.multiColorButton.addActionListener(e -> {

            Random rand =  new Random();

            Color[] colors =  new Color[3];

            for(int i = 0; i < colors.length; i++)
                colors[i] = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));

            multiColorButton.setColors(colors);
        });
        this.multiColorButton.setText("multicolor btn");
        this.panel.add(this.multiColorButton);
    }
    class MultiColorButton extends JButton implements iColorsButton {

        private static final long serialVersionUID = 1L;
        Color[] colors;

        public MultiColorButton() {
            this(new Color[] { new Color(63, 72, 204), new Color(181, 230, 29), new Color(237, 28, 36) });
        }

        public MultiColorButton(Color[] colors) {
            setContentAreaFilled(false);
            setFocusPainted(false);
            this.setColors(colors);
        }

        public void setColors(Color[] colors) {
            this.colors = colors;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {

            if (colors != null &&  colors.length > 0) {

                int colorsCount = colors.length;
                int fractionWitdh = getWidth() / colorsCount;

                for (int i = 0; i < colorsCount; i++) {
                    g.setColor(colors[i]);
                    g.fillRect(fractionWitdh * i, 0, fractionWitdh, getHeight());
                }
            }   
            super.paintComponent(g);
        }
    }
}

See working:

    
08.10.2017 / 20:58