Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

16. Write a Java applet to display a textual content on the browser.

import java.applet.Applet; import


java.awt.Graphics;

public class TextApplet extends Applet


{ public void paint(Graphics
g)
{
g.drawString("Hello : This is textual content", 90,50);
}
}

17. Write a Java applet to display various shapes like circle, oval, rectangle, square
on the Browser.
import java.applet.Applet; import
java.awt.Graphics;

public class ShapesDraw extends Applet


{ public void paint(Graphics
g)
{
// Circle 150*150
g.drawOval(30, 30, 150, 150);
// Rectangle 200*150
g.drawRect(210, 30, 200, 150);
// Oval 200*150
g.drawOval(30, 210, 150, 100);
// Square 100*100
g.drawRect(210, 210, 100, 100);
}
}
18. Write a Java applet to demonstrate event handing on the browser.
import java.awt.*; import
java.awt.event.*; import
java.applet.*;
public class AppletEventHandle extends Applet implements
ActionListener {
// Define Basic Components To Display
Label label1,label2,output;
TextField tempInput;
Checkbox ckKelvin,ckFarenh;
CheckboxGroup ckgrp;

// Initialize Frame
public void init()
{
label1 = new Label("Enter Temprature In Celcius : ");
tempInput = new TextField(15);

label2 = new Label("Choose Conversion Unit ");


ckgrp = new CheckboxGroup();
ckKelvin = new Checkbox("kelvin",ckgrp,false);
ckFarenh = new Checkbox("farenheight",ckgrp,false);
Button submitBtn=new Button("Convert");

// Ouput Will Be Displayed on this label


output = new Label("Output Will Be Displayed Here");
add(label1);
add(tempInput);
add(label2);
add(ckFarenh);
add(ckKelvin);
add(submitBtn);
add(output);

// When Submit button clicked


submitBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// Check if user has selected checkbox or not
if(ckgrp.getSelectedCheckbox()!=null)
{
// get data from input box and convert to double
double tempInp = Double.parseDouble(tempInput.getText());

// Display Result
if(ckgrp.getSelectedCheckbox().getLabel() == "kelvin")
{
output.setText("Temprature in Kelvin =
"+Double.toString(tempInp+273));
}
else {
output.setText("Temprature in Farenheight =
"+Double.toString((tempInp*1.8)+32));
}
}
}
}

You might also like