0% found this document useful (0 votes)
18 views9 pages

Applet Hands On

The document provides Java applet code examples for creating a simple applet that displays a welcome message, a calculator application using AWT controls, and a color palette program that changes text and background colors based on user input. Each applet includes initialization, event handling, and painting methods to manage user interactions and display results. The examples demonstrate the use of various AWT components such as labels, text fields, buttons, and checkboxes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Applet Hands On

The document provides Java applet code examples for creating a simple applet that displays a welcome message, a calculator application using AWT controls, and a color palette program that changes text and background colors based on user input. Each applet includes initialization, event handling, and painting methods to manage user interactions and display results. The examples demonstrate the use of various AWT components such as labels, text fields, buttons, and checkboxes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

To compile and run applets:

c:\>javac GfgApplet.java
c:\>appletviewer GfgApplet.java

1)Applet program for displaying WELCOME.

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="sw.class" height=400 width=500>

</applet>

*/

public class sw extends Applet implements ActionListener

TextField t1,t2;

public void init()

Label l1=new Label("No of bits :",Label.RIGHT);

Label l2=new Label("Window size :",Label.RIGHT);

t1=new TextField(10);

t2=new TextField(10);

add(l1);

add(t1);

add(l2);

add(t2);

t1.addActionListener(this);

t2.addActionListener(this);

public void actionPerformed(ActionEvent ae)

5acXjzUk
repaint();

public void paint(Graphics g)throws NumberFormatException

int i,j=0,t,q,s,l=0,w,win,k,p;

String[] msg={"w","e","l","c","o","m","e"," ","t","o"," ","M","C","E"};

String[] wind={"","","","","","","","","",""};

t=Integer.parseInt(t1.getText());

win=Integer.parseInt(t2.getText());

q=(int)Math.pow(2,t);

int y=10;

double d;

g.setColor(Color.black);

g.drawLine(200,100,200,450);

g.drawLine(400,100,400,450);

g.drawString("WINDOW",10,80);

g.drawString("MSG_SENT",130,80);

g.drawString("ACKNOWLEDGEMENT",350,80);

do

for(i=0;i<win && j<msg.length;i++,j++)

wind[i]=msg[j];

k=i;

d=Math.random();

s=(int)(d*10)%k+1;

5acXjzUk
l=l+s;

for(i=0;i<k;i++)

g.drawString(wind[i],10+i*10,100+y);

for(i=0;i<s;i++)

g.drawString(wind[i],150+i*10,100+y);

g.drawLine(200,100+y,400,100+y);

g.drawLine(400,100+y,200,130+y);

w=l%q;

g.drawString(Integer.toString(w),405,100+y);

j=j-(k-s);

y=y+30;

}while(j<msg.length);

DEMONSTRATION AWT CONTROLS:


SIMPLE CALCULATOR APPLICATION
(DISPLAYING BACKGROUND COLOR USING AWT BUTTON AND
ACTION EVENT.)

import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class calci extends Applet implements ActionListener
{

/* DECLARATION OF AWT */
Label l1,l2,l3,l4;
TextField t1,t2,t3;
Button add,sub,mul,div;
TextArea a1;

5acXjzUk
public void init()
{

/* CREATION OF LABEL */
l1=new Label("NUMBER 1");
l2=new Label("NUMBER 2");
l3=new Label("RESULT ");
l4=new Label("ENTER YOUR NAME ");

/*CREATION OF TEXTFIELD WITH SIZE 10*/


t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);

/*CREATION OF BUTTON*/
add=new Button("ADD");
sub=new Button("SUB");
mul=new Button("MUL");
div=new Button("DIV");

/*CREATION OF TEXT AREA*/


a1=new TextArea(5,15);

/*ADDING AWT IN APPLETWINDOW*/


add(l1);
add(t1);
add(l2);
add(t2);

5acXjzUk
add(l3);
add(t3);
add(l4);
add(a1);
add(add);
add(sub);
add(mul);
add(div);

/*ADDING ACTION TO THE BUTTON */


add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);

public void actionPerformed(ActionEvent ae)


{
/*OPERATION FPR ADDITION*/
if(ae.getSource()==add)
{
setBackground(Color.red);
int sum=(Integer.parseInt(t1.getText()))+(Integer.parseInt(t2.getText()));
t3.setText(String.valueOf(sum));
}

/*OPERATION FOR SUBSTRACTION*/


if(ae.getSource()==sub)
{

5acXjzUk
setBackground(Color.blue);
int sum=(Integer.parseInt(t1.getText()))-(Integer.parseInt(t2.getText()));
t3.setText(String.valueOf(sum));
}

/*OPERATION FOR MULTIPLICATION*/


if(ae.getSource()==mul)
{
setBackground(Color.green);
int sum=(Integer.parseInt(t1.getText()))*(Integer.parseInt(t2.getText()));
t3.setText(String.valueOf(sum));
}

/*OPERATION FOR DIVISION*/


if(ae.getSource()==div)
{
setBackground(Color.yellow);
int sum=(Integer.parseInt(t1.getText()))/(Integer.parseInt(t2.getText()));
t3.setText(String.valueOf(sum));
}
repaint();
}

public void paint(Graphics g)


{
/*TO DRAW A SQUARE*/
g.drawRect(180,180,220,220);

5acXjzUk
/*TO PRINT NUMBER 1*/
g.drawString("NUMBER 1="+t1.getText(),200,200);

/*TO PRINT NUMBER 2*/


g.drawString("NUMBER 2="+t2.getText(),200,210);

/*TO PRINT RESULT*/


g.drawString("RESULT ="+t3.getText(),200,220);

/*TO PRINT NAME*/


g.drawString("Name is ="+a1.getText(),200,230);
}

/*<applet code="calci.class" height=200 width=200></applet>*/

COLOR PALETTE PROGRAM USING ITEM EVENT

PROGRAM:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="colorpal" width=450 height=210>
</applet>
*/
public class colorpal extends Applet implements ItemListener
{
TextArea text=new TextArea("Sample Text");
int fr=0,fb=0,fg=0,br=0,bb=0,bg=0;
Checkbox fred,fgreen,fblue,bred,bgreen,bblue;
Font f=new Font("Sanserif",Font.BOLD,18);
public void init()
{
fred=new Checkbox("Forecolor Red");
fgreen=new Checkbox("Forecolor Green");
fblue=new Checkbox("Forecolor Blue");
bred=new Checkbox("Backcolor Red");
bgreen=new Checkbox("Backcolor Green");
bblue=new Checkbox("Backcolor Blue");

5acXjzUk
add(fred);
add(fgreen);
add(fblue);
add(bred);
add(bgreen);
add(bblue);
add(text);
fred.addItemListener(this);
fgreen.addItemListener(this);
fblue.addItemListener(this);
bred.addItemListener(this);
bgreen.addItemListener(this);
bblue.addItemListener(this);
}

public void itemStateChanged(ItemEvent ie)


{
repaint();
}

public void paint(Graphics g)


{
if(fred.getState())
fr=255;
else
fr=0;
if(fgreen.getState())
fg=255;
else
fg=0;
if(fblue.getState())
fb=255;
else
fb=0;
if(bred.getState())
br=255;
else
br=0;
if(bgreen.getState())
bg=255;
else
bg=0;

5acXjzUk
if(bblue.getState())
bb=255;
else
bb=0;
Color c=new Color(fr,fg,fb);
Color c1=new Color(br,bg,bb);
text.setForeground(c);
text.setBackground(c1);
}
}

5acXjzUk

You might also like