Skip to content

Instantly share code, notes, and snippets.

@codeguru42
Last active December 11, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeguru42/4534928 to your computer and use it in GitHub Desktop.
Save codeguru42/4534928 to your computer and use it in GitHub Desktop.
Color Selector test
public class MainWindow {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable()) {
JFrame f = new JFrame("Color Selector Test");
f.add(new MainPanel());
f.pack();
f.setVisible(true);
}
}
}
private class MainPanel extends JPanel {
public MainPanel() {
final JFrame colorFrame = new JFrame("Use color");
final ColorPanel colorPanel = new ColorPanel();
colorFrame.add(colorPanel());
JButton colorChooserButton = new JButton("Choose Color");
colorChooserButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
Color c = JColorChooser.showDialog(MainPanel.this, "Choose a Color", Color.BLACK);
colorPanel.setColor(c);
colorFrame.pack();
colorFrame.setVisible(true);
} catch (HeadlessException ex) {
ex.printStackTrace();
}
}
});
}
}
private class ColorPanel extends JPanel {
public ColorPanel() {
this.setPreferredSize(150, 150);
}
public setColor(Color c) {
this.c = c;
}
public void paintComponent(Graphics g) {
super.paint(g);
g.setColor(c);
g.drawRect(10, 10, 100, 100);
}
private Color c = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment