import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.AdjustmentListener; import java.awt.event.AdjustmentEvent; /** * mei 2007: Oefening op JScrollBar en Color * De bedoeling is dat de gebruiker de 3 kleurwaarden (rood, groen en blauw) kan instellen * via 3 horizontale schuifbalken. Deze kleurinstelling wordt dan toegepast op het centrale JLabel kleurLabel. * Uitbreiding (enkel als de rest al werkt): * * 1) Als de gebruiker getalwaarden intikt in de TextFields en dan op drukt, * dan worden de schuifbalken aangepast (en dus ook de kleur) * 2) Onderzoek: hoe maak je wit255,255,255? grijs 123,123,123? geel255,255,0? paars255,0,255? bruin? */ public class KleurenFrame extends JFrame { private JScrollBar roodBar; private JScrollBar groenBar; private JScrollBar blauwBar; private JLabel roodLabel; private JLabel groenLabel; private JLabel blauwLabel; private JTextField roodField; private JTextField groenField; private JTextField blauwField; private JLabel kleurLabel; public KleurenFrame() { super("Demo JScrollBar"); maakComponenten(); maakLayout(); voegListenersToe(); toonFrame(); } private void maakComponenten() { // De scrollBars instellen: min = 0; max = 255; grootte van de knobbel = 10; startwaarde = 0 roodBar = new JScrollBar(Scrollbar.HORIZONTAL); roodBar.setValues(0, 10,0,265); roodBar.setBlockIncrement(10); groenBar = new JScrollBar(Scrollbar.HORIZONTAL); groenBar.setValues(0, 10,0,265); groenBar.setBlockIncrement(10); blauwBar = new JScrollBar(Scrollbar.HORIZONTAL); blauwBar.setValues(0, 10,0,265); blauwBar.setBlockIncrement(10); // De labels instellen: achtergrond grijs, voorgrond in de specifieke kleur roodLabel = new JLabel("rood : "); roodLabel.setOpaque(true); roodLabel.setForeground(Color.RED); roodLabel.setBackground(Color.GRAY); groenLabel = new JLabel("groen : "); groenLabel.setOpaque(true); groenLabel.setForeground(Color.GREEN); groenLabel.setBackground(Color.GRAY); blauwLabel = new JLabel("blauw : "); blauwLabel.setOpaque(true); blauwLabel.setForeground(Color.BLUE); blauwLabel.setBackground(Color.GRAY); // De TextFields instellen (ok) roodField = new JTextField("0"); groenField = new JTextField("0"); blauwField = new JTextField("0"); // kleurLabel instellen (ok) kleurLabel = new JLabel(); kleurLabel.setOpaque(true); kleurLabel.setBackground(Color.BLACK); } private void maakLayout() { JPanel schuifPanel = new JPanel(new GridLayout(3, 0)); schuifPanel.add(roodBar); schuifPanel.add(groenBar); schuifPanel.add(blauwBar); JPanel getallenPanel = new JPanel(new GridLayout(3, 0)); getallenPanel.add(roodLabel); getallenPanel.add(roodField); getallenPanel.add(groenLabel); getallenPanel.add(groenField); getallenPanel.add(blauwLabel); getallenPanel.add(blauwField); JPanel bovenPanel = new JPanel(new BorderLayout()); bovenPanel.add(schuifPanel, BorderLayout.CENTER); bovenPanel.add(getallenPanel, BorderLayout.EAST); this.getContentPane().add(bovenPanel, BorderLayout.NORTH); this.getContentPane().add(kleurLabel, BorderLayout.CENTER); } private void voegListenersToe() { roodBar.addAdjustmentListener(new SchuifListener()); roodField.addActionListener(new FieldListener()); groenBar.addAdjustmentListener(new SchuifListener()); groenField.addActionListener(new FieldListener()); blauwBar.addAdjustmentListener(new SchuifListener()); blauwField.addActionListener(new FieldListener()); // Geef als listener voor de schuifbalken "this" op // kleurLabel.setBackground(roodField.getText(), groenField.getText(), blauwField.getText()); } private void toonFrame() { setBounds(200, 200, 500, 300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class SchuifListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e){ int rood = roodBar.getValue(); int groen = groenBar.getValue(); int blauw = blauwBar.getValue(); kleurLabel.setBackground(new Color(rood, groen, blauw)); roodField.setText(String.valueOf(rood)); groenField.setText(String.valueOf(groen)); blauwField.setText(String.valueOf(blauw)); } } private class FieldListener implements ActionListener{ public void actionPerformed(ActionEvent e){ roodBar.setValue(Integer.parseInt(roodField.getText())); blauwBar.setValue(Integer.parseInt(blauwField.getText())); groenBar.setValue(Integer.parseInt(groenField.getText())); } } }