Last active
December 11, 2015 02:59
-
-
Save codeguru42/4534928 to your computer and use it in GitHub Desktop.
Color Selector test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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