This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
swing:color [2018/03/27 20:35] gthanos [Παράδειγμα χρήση της κλάσης Color] |
swing:color [2021/04/13 05:37] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Η κλάση Color ====== | ||
| - | |||
| - | Η κλάση [[https:// | ||
| - | <code java> | ||
| - | public void setBackground(Color bg) | ||
| - | </ | ||
| - | |||
| - | Για παράδειγμα, | ||
| - | <code java> | ||
| - | this.getContnetPane().setBackground(Color.BLUE); | ||
| - | </ | ||
| - | |||
| - | Η κλάση Color παρέχει τα παρακάτω χρώματα ως στατικές μεταβλητές τύπου Color. | ||
| - | <code java> | ||
| - | Color.BLACK | ||
| - | Color.BLUE | ||
| - | Color.CYAN | ||
| - | Color.DARK_GRAY | ||
| - | Color.GRAY | ||
| - | Color.GREEN | ||
| - | Color.LIGHT_GRAY | ||
| - | Color.MAGENTA | ||
| - | Color.ORANGE | ||
| - | Color.PINK | ||
| - | Color.RED | ||
| - | Color.WHITE | ||
| - | Color.YELLOW | ||
| - | </ | ||
| - | |||
| - | Αν θέλετε να ορίσετε τα δικά σας αντικείμενα τύπου Color μπορείτε να το κάνετε με ένα από τους | ||
| - | <code java> | ||
| - | Color(ColorSpace cspace, float[] components, float alpha) | ||
| - | Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha. | ||
| - | ------------------------------------------------------------------------- | ||
| - | Color(float r, float g, float b) | ||
| - | Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0). | ||
| - | ------------------------------------------------------------------------- | ||
| - | Color(float r, float g, float b, float a) | ||
| - | Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0). | ||
| - | ------------------------------------------------------------------------- | ||
| - | Color(int rgb) | ||
| - | Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. | ||
| - | ------------------------------------------------------------------------- | ||
| - | Color(int rgba, boolean hasalpha) | ||
| - | Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. | ||
| - | ------------------------------------------------------------------------- | ||
| - | Color(int r, int g, int b) | ||
| - | Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255). | ||
| - | ------------------------------------------------------------------------- | ||
| - | Color(int r, int g, int b, int a) | ||
| - | Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255). | ||
| - | </ | ||
| - | |||
| - | ====== Παράδειγμα χρήση της κλάσης Color ====== | ||
| - | |||
| - | Παρακάτω δίνουμε ένα παράδειγμα όπου χρησιμοποιούμε ότι μάθαμε μέχρι τώρα αναφορικά με το Swing framework. Δημιουργούμε ένα παράθυρο και ορίζουμε ως Manager για το content pane του παραθύρου τον [[swing: | ||
| - | |||
| - | <code java ColorDemo.java> | ||
| - | import javax.swing.*; | ||
| - | import java.awt.*; | ||
| - | import java.awt.event.*; | ||
| - | |||
| - | public class ColorDemo extends JFrame implements ActionListener { | ||
| - | public static final int WIDTH = 300; | ||
| - | public static final int HEIGHT = 200; | ||
| - | | ||
| - | private JPanel redPanel; | ||
| - | private JPanel whitePanel; | ||
| - | private JPanel bluePanel; | ||
| - | | ||
| - | public static void main(String args[]) { | ||
| - | ColorDemo gui = new ColorDemo(); | ||
| - | gui.setVisible(true); | ||
| - | } | ||
| - | | ||
| - | public ColorDemo() { | ||
| - | super(" | ||
| - | setSize(WIDTH, | ||
| - | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | setLayout(new BorderLayout()); | ||
| - | | ||
| - | JPanel biggerPanel = new JPanel(); | ||
| - | biggerPanel.setLayout(new GridLayout(1, | ||
| - | | ||
| - | redPanel = new JPanel(); | ||
| - | redPanel.setBackground(Color.LIGHT_GRAY); | ||
| - | biggerPanel.add(redPanel); | ||
| - | | ||
| - | whitePanel = new JPanel(); | ||
| - | whitePanel.setBackground(Color.LIGHT_GRAY); | ||
| - | biggerPanel.add(whitePanel); | ||
| - | | ||
| - | bluePanel = new JPanel(); | ||
| - | bluePanel.setBackground(Color.LIGHT_GRAY); | ||
| - | biggerPanel.add(bluePanel); | ||
| - | | ||
| - | add(biggerPanel, | ||
| - | | ||
| - | JPanel buttonPanel = new JPanel(); | ||
| - | buttonPanel.setLayout(new FlowLayout()); | ||
| - | buttonPanel.setBackground(Color.LIGHT_GRAY); | ||
| - | | ||
| - | JButton redButton = new JButton(" | ||
| - | redButton.setBackground(Color.RED); | ||
| - | redButton.addActionListener(this); | ||
| - | buttonPanel.add(redButton); | ||
| - | | ||
| - | JButton whiteButton = new JButton(" | ||
| - | whiteButton.setBackground(Color.WHITE); | ||
| - | whiteButton.addActionListener(this); | ||
| - | buttonPanel.add(whiteButton); | ||
| - | | ||
| - | JButton blueButton = new JButton(" | ||
| - | blueButton.setBackground(Color.BLUE); | ||
| - | blueButton.addActionListener(this); | ||
| - | buttonPanel.add(blueButton); | ||
| - | | ||
| - | add(buttonPanel, | ||
| - | } | ||
| - | | ||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | String buttonString = e.getActionCommand(); | ||
| - | if(buttonString.equals(" | ||
| - | redPanel.setBackground(Color.RED); | ||
| - | whitePanel.setBackground(Color.LIGHT_GRAY); | ||
| - | bluePanel.setBackground(Color.LIGHT_GRAY); | ||
| - | } | ||
| - | else if(buttonString.equals(" | ||
| - | redPanel.setBackground(Color.LIGHT_GRAY); | ||
| - | whitePanel.setBackground(Color.WHITE); | ||
| - | bluePanel.setBackground(Color.LIGHT_GRAY); | ||
| - | } | ||
| - | else if(buttonString.equals(" | ||
| - | redPanel.setBackground(Color.LIGHT_GRAY); | ||
| - | whitePanel.setBackground(Color.LIGHT_GRAY); | ||
| - | bluePanel.setBackground(Color.BLUE); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | | Προηγούμενο: | ||