0% found this document useful (0 votes)
68 views4 pages

Write A Java Applet To Display A Textual Content On The Browser

This document contains 3 Java applets: 1) A simple text applet that displays the string "Hello : This is textual content" on the browser. 2) An applet that draws various shapes (circle, oval, rectangle, square) on the browser. 3) An applet that demonstrates event handling by allowing the user to enter a temperature, select a conversion (Kelvin or Fahrenheit), and displays the converted temperature.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views4 pages

Write A Java Applet To Display A Textual Content On The Browser

This document contains 3 Java applets: 1) A simple text applet that displays the string "Hello : This is textual content" on the browser. 2) An applet that draws various shapes (circle, oval, rectangle, square) on the browser. 3) An applet that demonstrates event handling by allowing the user to enter a temperature, select a conversion (Kelvin or Fahrenheit), and displays the converted temperature.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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