Color Chooser – multiple event handling
This tutorial is on applying/handling multiple events in Applet based Application. What we are going to develop is a utility which selects the value of Red, Green and Blue in the range of 0 – 255 by using Scrollbar components. After setting up those three values, user can set the generated Color as Background. And once the color is set, we gonna make it black in 10 seconds by a smooth change in Color intensity.
If you are not clear with the program aim then scroll down and see the output and read again!
Declaring and Defining Array of components:
import java.awt.*; import java.applet.*; public class ColorChooserApplet extends Applet { Button btn = new Button("Run"); Label lbl[] = new Label[3]; Scrollbar sc[] = new Scrollbar[3]; TextField tf[] = new TextField[3]; String[] slbl = {"Red","Green","Blue"}; public void init() { for(int i=0;i<3;i++) { lbl[i] = new Label(slbl[i]); sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275); tf[i] = new TextField("0"); add(lbl[i]); add(sc[i]); add(tf[i]); } add(btn); } }
Applying null layout:
Java based desktop applications support various Layouts. Default Layout of Applet application is FlowLayout in which components are attached one after another as per the direction set by use(default:center). Now in case we need exact positioning of components, We can remove the layout by calling a method setLayout(null) and add the components. After setting null layout if we try to add components, they will not be visible in output. To make it visible we have to call a method named setBounds(int x_cord, int y_cord, int component_width, int component_height) for each component object.
import java.awt.*; import java.applet.*; import java.awt.event.*; public class ColorChooserApplet extends Applet { Button btn = new Button("Run"); Label lbl[] = new Label[3]; Scrollbar sc[] = new Scrollbar[3]; TextField tf[] = new TextField[3]; String[] slbl = {"Red","Green","Blue"}; public void init() { setLayout(null); for(int i=0;i<3;i++) { lbl[i] = new Label(slbl[i]); sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275); tf[i] = new TextField("0"); add(lbl[i]); add(sc[i]); add(tf[i]); lbl[i].setBounds(20,(75*i)+50, 50,30); sc[i].setBounds(70,(75*i)+50, 350,30); tf[i].setBounds(450,(75*i)+50, 50, 30); } add(btn); btn.setBounds(200,400,100,50); } }
Code to sync Scrollbars and TextFields:
In our application, When user moves Scrollbar the current value of scroll bar is to be displayed/updated in text field and vice versa.
public void adjustmentValueChanged(AdjustmentEvent e) { for(int i=0;i<3;i++) { tf[i].setText(sc[i].getValue()+""); } } public void keyReleased(KeyEvent e) { System.out.println("Release:"+tf[0].getText()); for(int i=0;i<3;i++) { sc[i].setValue(Integer.parseInt(tf[i].getText())); } }
Full Program:
import java.awt.*; import java.applet.*; import java.awt.event.*; public class ColorChooserApplet extends Applet implements AdjustmentListener, KeyListener, ActionListener, Runnable { int r=0,g=0,b=0; Thread t; Button btn = new Button("Run"); Label lbl[] = new Label[3]; Scrollbar sc[] = new Scrollbar[3]; TextField tf[] = new TextField[3]; String[] slbl = {"Red","Green","Blue"}; public void init() { setLayout(null); for(int i=0;i<3;i++) { lbl[i] = new Label(slbl[i]); sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275); tf[i] = new TextField("0"); add(lbl[i]); add(sc[i]); add(tf[i]); lbl[i].setBounds(20,(75*i)+50, 50,30); sc[i].setBounds(70,(75*i)+50, 350,30); tf[i].setBounds(450,(75*i)+50, 50, 30); sc[i].addAdjustmentListener(this); tf[i].addKeyListener(this); } add(btn); btn.setBounds(200,400,100,50); btn.addActionListener(this); } public void adjustmentValueChanged(AdjustmentEvent e) { for(int i=0;i<3;i++) { tf[i].setText(sc[i].getValue()+""); } } public void keyPressed(KeyEvent e) { System.out.println("Pressed:"+tf[0].getText()); } public void keyReleased(KeyEvent e) { System.out.println("Release:"+tf[0].getText()); for(int i=0;i<3;i++) { sc[i].setValue(Integer.parseInt(tf[i].getText())); } } public void keyTyped(KeyEvent e) { System.out.println("Typed:"+tf[0].getText()); } public void actionPerformed(ActionEvent e) { r = sc[0].getValue(); g = sc[1].getValue(); b = sc[2].getValue(); Color clr = new Color(r,g,b); setBackground(clr); t = new Thread(this); t.start(); } public void run() { try { for(int i=0;i<10;i++) { t.sleep(1000); r = r - sc[0].getValue()/10; g = g - sc[1].getValue()/10; b = b - sc[2].getValue()/10; Color clr = new Color(r,g,b); setBackground(clr); } } catch(Exception e) { System.out.println(e); } } } /* <applet code="ColorChooserApplet" width="550" height="550"></applet> */
Recent Comments