In this Java AWT Tutorial, we will create three horizontal scrollbar to denote RGB color component values.Then using these AWT Scrollbars we will change the background color of the Java AWT Panel by handling Adjustment Event by implementing AdjustmentListener.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Checkbox; import java.awt.Color; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; import java.awt.Scrollbar; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 01: Get Adjustment Listener Support //To Handle AdjustmentEvent public class FrameWindow extends Frame implements WindowListener, AdjustmentListener { //Sample 02: Class Members Checkbox chkGrayScale; Panel MidPanel; Scrollbar RedBar; Scrollbar BlueBar; Scrollbar GreenBar; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(500, 250); setLocation(100,100); addWindowListener(this); //Sample 03: Create Two Panels Panel TopPanel = new Panel(); MidPanel = new Panel(); //Sample 04: Create Scroll Bars RedBar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 5, 0, 255); RedBar.setBackground(Color.RED); BlueBar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 5, 0, 255); BlueBar.setBackground(Color.BLUE); GreenBar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 5, 0, 255); GreenBar.setBackground(Color.GREEN); RedBar.setBlockIncrement(5); BlueBar.setBlockIncrement(5); GreenBar.setBlockIncrement(5); //Sample 04: Add Scrollbars to Top Panel TopPanel.setLayout(new GridLayout(3, 1)); TopPanel.add(RedBar); TopPanel.add(BlueBar); TopPanel.add(GreenBar); //Sample 05: Add Controls to Frame Window chkGrayScale = new Checkbox("Gray Scale"); add(BorderLayout.SOUTH, chkGrayScale); add(BorderLayout.NORTH, TopPanel); add(MidPanel); //Sample 06: Register to get adjustment event RedBar.addAdjustmentListener(this); BlueBar.addAdjustmentListener(this); GreenBar.addAdjustmentListener(this); } //Sample 07: Method to Set GrayScale Color Value //For Grayscale RGB Component will hold same value private void setGrayScale(int GrayScaleValue) { GreenBar.setValue(GrayScaleValue); RedBar.setValue(GrayScaleValue); BlueBar.setValue(GrayScaleValue); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } //Sample 08: Change Panel Background public void adjustmentValueChanged(AdjustmentEvent e) { //8.1: Initialize RGB Components int red, blue, green; red = green = blue = 0; //8.2: Set RGB Component Value //For GrayScale Adjust Scrollbar values if (e.getAdjustable() == RedBar) { red = RedBar.getValue(); if (chkGrayScale.getState() == true) { blue = green = red; setGrayScale(red); } } if (e.getAdjustable() == BlueBar) { blue = BlueBar.getValue(); if (chkGrayScale.getState() == true) { green = red = blue; setGrayScale(blue); } } if (e.getAdjustable() == GreenBar) { green = GreenBar.getValue(); if (chkGrayScale.getState() == true) { red = blue = green ; setGrayScale(green); } } //8.3: Create Color Component and Set it to MidPanel Color rgbColor = new Color(red, green, blue); MidPanel.setBackground(rgbColor); } } |
Categories: AWT-Tube