Write a program in Java to create an applet which take radius of a circle as input and find the area of a circle and draw the circle.
Java Code:
// Java Document
import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.TextField;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
class Circle extends Applet {
TextField text1;
public void init() {
text1 = new TextField(8);
add(text1);
text1.setText(null);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Enter the radius of circle : ", 10, 2);
int r;
r = Integer.parseInt(text1.getText());
double radius = Math.PI * r * r;
g2.setColor(Color.blue);
g2.drawString("The area of circle is: ", 100, 150);
g2.drawString(String.valueOf(radius), 120, 170);
Ellipse2D.Double ob = new Ellipse2D.Double(100, 100, 2*radius, 2*radius);
g2.draw(ob);
}
public boolean ActionListener() {
repaint();
return true;
}
//public boolean action(Event event, Object object) {
// repaint();
// return true;
//}
}
HTML Code:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<center>
<applet code="Circle.class" width="500" height="300"></applet>
</center>
</body>
</html>
Unfortunately I keep receiving the error java.lang.reflect.invocationtargetexception
when I try to run the html file in my browser. Any Help?