Practical-8 Develop an program that contains three check boxes and 30 x 30 pixel canvas.The three checkboxes should be labeled “Red”, “Green”,”Blue”. The selection of the check boxes determine the color of the canvas. For example, if the user selects both “Red” and “Blue”, the canvas should be purple.

 

import java.awt.*;

import java.awt.event.*;

public class Prac8 implements ItemListener {

    Frame f;

    Checkbox c1,c2,c3;

    Canvas c;

    int red=0,blue=0,green=0;

    Prac8()

    {

        f=new Frame("Practical-8");

        c1=new Checkbox("Red");

        c2=new Checkbox("Green");

        c3=new Checkbox("Blue");

        

        c1.addItemListener(this);

        c2.addItemListener(this);

        c3.addItemListener(this);

        

        c=new Canvas();

        c.setSize(30,30);

        c.setBackground(Color.BLACK);

   

        f.setLayout(new FlowLayout());

        

        f.add(c1);

        f.add(c2);

        f.add(c3);

        f.add(c);

        

        f.setSize(300,300);

        f.setVisible(true);

    }

    

    public static void main(String[] args)

    {

        Prac8 f=new Prac8();

    }

    public void itemStateChanged(ItemEvent e)

    {

        if(c1.getState())

            red=255;

        else

            red=0;

        if(c2.getState())

            green=255;

        else

            green=0;

        if(c3.getState())

            blue=255;

        else

            blue=0;

        

        Color clr=new Color(red,green,blue);

        c.setBackground(clr);

    }

}

Output:


Comments

Popular posts from this blog

Write a program to check whether empty query string is entered in Asp.Net.

Mandavrayji Temple, Muli, Surendranagar, Gujarat

Write a program to change color of Label text control programmatically in Asp.Net.