...::: Recent Updates :::...

Friday, September 23, 2011

Develop an application which is having one rectangle and three textboxes of Red, Green, and Blue (i.e. RGB). As the value of RGB Textbox changing the fill color of rectangle should change.

------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
JAVA APPLET PROGRAM
RBG  Program
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
Develop an application which is having one rectangle and three textboxes of Red, Green, and Blue (i.e. RGB).  As the value of RGB Textbox changing the fill color of rectangle should change.
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------ 
NAME : SAHIN NISAR RAJ
ENROLL : 105200693009
COLLEGE : LAXMI INSTITUTE OF COMPUTER APPLICATIONS
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------ 
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------ 
package session9;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;

public class S9Q5 extends Applet implements KeyListener,ActionListener
{
    int vred = 20;
    int vgreen = 30;
    int vblue = 40;
    TextField red;
    TextField green;
    TextField blue;
    Label lbred;
    Label lbgreen;
    Label lbblue;
    Button Change;


    @Override
    public void init()
    {
        setLayout(null);

        red = new TextField(20);
        green = new TextField(20);
        blue = new TextField(20);
        lbred = new Label("Red :");
        lbgreen = new Label("Green :");
        lbblue = new Label("Blue :");
        Change = new Button("Change");

        add(red);
        red.setBackground(Color.red);
        add(green);
        green.setBackground(Color.green);
        add(blue);
        blue.setBackground(Color.blue);

        add(lbred);
        lbred.setBackground(Color.gray);
        lbred.setForeground(Color.white);

        add(lbgreen);
        lbgreen.setBackground(Color.gray);
        lbgreen.setForeground(Color.white);

        add(lbblue);
        lbblue.setBackground(Color.gray);
        lbblue.setForeground(Color.white);

        add(Change);


        red.setBounds(230,30,100,20);
        green.setBounds(230,50,100,20);
        blue.setBounds(230,70,100,20);
        lbred.setBounds(180,30,100,20);
        lbgreen.setBounds(180,50,100,20);
        lbblue.setBounds(180,70,100,20);
        Change.setBounds(180,100, 150, 30);

        red.addKeyListener(this);
        green.addKeyListener(this);
        blue.addKeyListener(this);
        Change.addActionListener(this);

        setBackground(Color.GRAY);


    }

    @Override
    public void paint(Graphics g)
    {

        try
        {
            Color c = new Color(vred,vgreen,vblue);
            g.setColor(c);
            g.fillRect(20, 20,130,100);
            g.drawString("* Range Must be Between 0 - 255", 90, 180);

        } catch (Exception e)
        {
            JOptionPane.showMessageDialog(null,e.getMessage());
        }

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == Change)
        {
           
                this.vblue = Integer.parseInt(blue.getText().toString());
                this.vgreen = Integer.parseInt(green.getText().toString());
                this.vred = Integer.parseInt(red.getText().toString());
                repaint();
        }

    }
}

No comments:

Post a Comment