Tuesday, 30 October 2012

Change Background Color of an Applet

Dynamically Change Background Color of an applet using java scroll bar Control

/* Applet program to control the background color using scroll bars */
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

/*
    <APPLET    CODE="BackColor"
        HEIGHT=600
        WIDTH=600
    >
    </APPLET>
*/

public class BackColor extends Applet implements AdjustmentListener
{
    Scrollbar js_red,js_green,js_blue;
    Label lbl_red,lbl_green,lbl_blue;
    Color c;
    public void init()
    {
       
        js_red=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,256);
        js_green=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,256);
        js_blue=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,256);
        lbl_red=new Label("Red  "+" 0");
        lbl_green=new Label("Green "+"0");
        lbl_blue=new Label("Blue "+"0");               
        js_red.addAdjustmentListener(this);
        js_green.addAdjustmentListener(this);
        js_blue.addAdjustmentListener(this);
       
       
        js_red.setBounds(20,20,150,20);
        js_green.setBounds(20,50,150,20);
        js_blue.setBounds(20,80,150,20);
        lbl_red.setBounds(190,20,75,20);
        lbl_green.setBounds(190,50,75,20);
        lbl_blue.setBounds(190,80,75,20);
       
        Panel jp=new Panel();
        jp.setLayout(null);
        jp.add(js_red);
        jp.add(lbl_red);
        jp.add(js_green);
        jp.add(lbl_green);
        jp.add(js_blue);
        jp.add(lbl_blue);
        jp.setBounds(20,20,250,120);
       

        this.add(jp);

    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        c=new Color(js_red.getValue(),js_green.getValue(),js_blue.getValue());
        lbl_red.setText("red"+js_red.getValue());
        lbl_green.setText("green"+js_green.getValue());
        lbl_blue.setText("blue"+js_blue.getValue());
        setBackground(c);   
    }
   
}

Output 

Change Background Color of an Applet dynamically
Change Background color using scroll bar







 


No comments:

Post a Comment