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:
Now I’ve seen Where I went wrong !
– saidmrn