Change button color at runtime

Asked

Viewed 361 times

4

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

But I’m trying to change his color at runtime and I can’t. Even instantiating a new and replacing.

The class of the button 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);

    }
}

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

I install the button and place it in a vector:

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

I 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 don’t know what’s missing, because this is my first contact with Paint in java.

1 answer

5


Edit.: I optimized the class so that it is not limited to only 3 colors, but the amount you find necessary.


Just create a method in the class MultiColorButton which receives the color array (or the 3 individual colors), assigns them to a color array and forces the repaint() button right after assigning 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 the form of use, 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:

inserir a descrição da imagem aqui

  • Now I’ve seen Where I went wrong !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.