Originariamente inviato da gogetassj4dp
E' possibile creare una colorbar in java? Per colobar se nn l'avete capito, intendo quella barra che riporta tutti i colori dal più scuro al più chiaro o qualcosa del genere che è presente in modo simile in word.
Prova questo programmino e dimmi se è più o meno quello che volevi:
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProvaFrame extends JFrame
{
private JPanel panel;
private JButton[] buttons;
public ProvaFrame ()
{
super ("Prova colori");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (300, 300);
panel = new JPanel ();
panel.setLayout (new GridLayout (1, 6));
buttons = new JButton[6];
buttons[0] = new JButton (new ColoredBlockIcon (255, 0, 0));
buttons[1] = new JButton (new ColoredBlockIcon (255, 255, 0));
buttons[2] = new JButton (new ColoredBlockIcon (0, 255, 0));
buttons[3] = new JButton (new ColoredBlockIcon (0, 255, 255));
buttons[4] = new JButton (new ColoredBlockIcon (0, 0, 255));
buttons[5] = new JButton (new ColoredBlockIcon (255, 0, 255));
for (int i=0; i < buttons.length; i++)
panel.add (buttons[i]);
add (panel, BorderLayout.NORTH);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable()
{
public void run ()
{
ProvaFrame f = new ProvaFrame ();
f.setVisible (true);
}
});
}
}
class ColoredBlockIcon implements Icon
{
private static Color borderColor = new Color (128, 128, 128);
private Color fillColor;
public ColoredBlockIcon (int r, int g, int b)
{
this.fillColor = new Color (r, g, b);
}
public ColoredBlockIcon (Color fillColor)
{
this.fillColor = fillColor;
}
public int getIconHeight ()
{
return 12;
}
public int getIconWidth ()
{
return 12;
}
public void paintIcon (Component c, Graphics g, int x, int y)
{
g.setColor (borderColor);
g.drawRect (x, y, 11, 11);
g.setColor (fillColor);
g.fillRect (x+1, y+1, 10, 10);
}
}