0% found this document useful (0 votes)
29 views6 pages

Dynamic Background Color in Java

This Java program dynamically changes the background color of an applet using different methods like checkboxes, buttons, text fields, scrollbars and a choice list. It uses events like mouse clicks, item changes and adjustments to detect color changes and updates the background color accordingly. Methods are used to set the background color on color selection and synchronize label colors with the applet background.

Uploaded by

Raj Umakantham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Dynamic Background Color in Java

This Java program dynamically changes the background color of an applet using different methods like checkboxes, buttons, text fields, scrollbars and a choice list. It uses events like mouse clicks, item changes and adjustments to detect color changes and updates the background color accordingly. Methods are used to set the background color on color selection and synchronize label colors with the applet background.

Uploaded by

Raj Umakantham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

CHANGING BACKGROUND COLOR DYNAMICALLY

PROGRAM
import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code="dynamicColor" width=300 height=350>


</applet>
*/

public class dynamicColor extends Applet implements


MouseListener,ItemListener,ActionListener,AdjustmentListener
{
Label demo,colorLabel,colorChoose,colorRadio,colorButton,colorScroll,coLabelChoice;
TextField colorText;
Checkbox redCheck,greenCheck,blueCheck;
CheckboxGroup cbg;
Checkbox redRadio,greenRadio,blueRadio;
Button redBut,greenBut,blueBut;
Scrollbar redScroll,greenScroll,blueScroll;
Choice colorChoice;

public void init()


{
demo=new Label("CHANGING BACKGROUND COLOR DYNAMICALLY");
coLabelChoice=new Label("Select Your Choice from Choice list ");
colorLabel=new Label("Enter Color : ");
colorText=new TextField(20);
colorChoose=new Label("Choose any color : ");
redCheck=new Checkbox("Red");
greenCheck=new Checkbox("Green");
blueCheck=new Checkbox("Blue");
colorRadio=new Label("Choose any color : ");
colorButton=new Label("Click any Button : ");
redBut=new Button("Red");
greenBut=new Button("Green");
blueBut=new Button("Blue");
colorScroll=new Label("Adjust ScrollBar to Change COLOR ");
redScroll=new Scrollbar([Link],0,5,0,255);
greenScroll=new Scrollbar([Link],0,5,0,255);
blueScroll=new Scrollbar([Link],0,5,0,255);
colorChoice=new Choice();
[Link]("Red");
[Link]("Green");
[Link]("Blue");
cbg=new CheckboxGroup();
redRadio=new Checkbox("Red",cbg,false);
greenRadio=new Checkbox("Green",cbg,false);
blueRadio=new Checkbox("Blue",cbg,false);
setLayout(new FlowLayout([Link]));
add(demo);
add(colorLabel);
add(colorText);
add(colorChoose);
add(redCheck);
add(greenCheck);
add(blueCheck);
add(colorRadio);
add(redRadio);
add(greenRadio);
add(blueRadio);
add(colorButton);
add(redBut);
add(greenBut);
add(blueBut);
add(colorScroll);
add(redScroll);
add(greenScroll);
add(blueScroll);
add(coLabelChoice);
add(colorChoice);

[Link](this);
[Link](this);
[Link](this);

[Link](this);
[Link](this);
[Link](this);
[Link](this);

[Link](this);
[Link](this);
[Link](this);
[Link](this);

[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
if([Link]()==redBut)
setBackground([Link]);
else if([Link]()==greenBut)
setBackground([Link]);
else if([Link]()==blueBut)
setBackground([Link]);
else if([Link]()==colorText)
{
if([Link]().equals("red"))
setBackground([Link]);
else if([Link]().equals("green"))
setBackground([Link]);
else if([Link]().equals("blue"))
setBackground([Link]);
}
ChangeLabelColor();
}

public void itemStateChanged(ItemEvent ie)


{
Color c;
if([Link]()==true && [Link]()==true
&&[Link]()==true)
setBackground(new Color(255,255,255));
else if([Link]()==true && [Link]()==true
&&[Link]()==false)
setBackground(new Color(255,255,0));
else if([Link]()==true && [Link]()==false
&&[Link]()==true)
setBackground(new Color(255,0,255));
else if([Link]()==true && [Link]()==false
&&[Link]()==false)
setBackground(new Color(255,0,0));
else if([Link]()==false && [Link]()==true
&&[Link]()==true)
setBackground(new Color(0,255,255));
else if([Link]()==false && [Link]()==true
&&[Link]()==false)
setBackground(new Color(0,255,0));
else if([Link]()==false && [Link]()==false
&&[Link]()==true)
setBackground(new Color(0,0,255));
else if([Link]()==false && [Link]()==false
&&[Link]()==false)
setBackground(new Color(0,0,0));

ChangeLabelColor();
}

public void adjustmentValueChanged(AdjustmentEvent ae)


{
Color c=new
Color([Link](),[Link](),[Link]());
setBackground(c);
ChangeLabelColor();
}

public void ChangeLabelColor()


{
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
[Link](getBackground());
}

public void mouseClicked(MouseEvent me)


{
if([Link]()==redRadio)
setBackground([Link]);
else if([Link]()==greenRadio)
setBackground([Link]);
else if([Link]()==blueRadio)
setBackground([Link]);
else if([Link]()=="Red")
setBackground([Link]);
else if([Link]()=="Green")
setBackground([Link]);
else if([Link]()=="Blue")
setBackground([Link]);
ChangeLabelColor();
}
public void mouseEntered(MouseEvent me)
{}

public void mouseExited(MouseEvent me)


{}

public void mousePressed(MouseEvent me)


{}

public void mouseReleased(MouseEvent me)


{}
}
OUTPUT

You might also like