Is it possible to put 3 colors on a button?

Asked

Viewed 176 times

8

I need to divide the button into 3 "equal pieces" and each piece has a different color. For example, a button with the colors Blue, Green and Red, each occupying 33.3% space. How to do this?

inserir a descrição da imagem aqui

  • Place an image with the colors in the background

1 answer

13


Edit.: I optimized the two classes so that they are not limited to only 3 colors, but the amount you find necessary.


Without using gradient, applying the color division exactly as you exemplified:

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) {

        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);
    }
}

Upshot:

inserir a descrição da imagem aqui


Although not mentioned in the question, I leave here an alternative with linear gradient, which makes the transition of colors more beautiful:

class GradientButton extends JButton {

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

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

    public GradientButton(Color[] colors) {
        setContentAreaFilled(false);
        setFocusPainted(false);
        this.colors = colors;
    }

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

    @Override
    protected void paintComponent(Graphics g) {

        final Graphics2D g2d = (Graphics2D) g;
        float[] fractions = new float[colors.length];

        for (int i = 1; i <= fractions.length; i++) {
            float fraction = 1.0f / fractions.length;
            fractions[i - 1] = i * fraction;
        }

        g2d.setPaint(new LinearGradientPaint(0, 0, getWidth(), getHeight(), fractions, colors));
        g2d.fillRect(0, 0, getWidth(), getHeight());

        super.paintComponent(g);
    }
}

Upshot:

inserir a descrição da imagem aqui

The use is like that of a common Jbutton.

Your choice is the best option ;)

  • 1

    Thanks Articuno, that’s exactly what I was looking for! I appreciate the attention !

  • Sorry to ask another question,but I can not in any way change the color of the buttons after they were instantiated,tried repaint and validate but nothing. What can I do to change them at runtime ?

  • @saidmrn you would need to create a method that gets the colors to be changed, and then give repaint.

  • I created a separate question to not spam this. https://answall.com/questions/244443/trocar-buttonbutton-time_execution If you know how to help me, !

Browser other questions tagged

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