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?

The awt GUI classes were superceeded in 1998 by Swing. Yes, 1998 - more that 15 years ago. You are seriously wasting your time learning or using it.
Update your code to use Swing (JApplet etc).

Thanks for the information but I am supposed to write it in awt GUI only. The reason being this is my college assignment and maybe they still 'believe' in teaching us these outdated technology. If you could, please help me figure out the problem.

That's really sad. I would worry that if the course materials have not been updated this century then they will also miss the massive language updates of version 1.5 etc.
Still, you have no choice if you want a passing grade!

I don't know why you have that message. Is there a stack trace or a "cause" Exception on the console? (java.lang.reflect.invocationtargetexception is a wrapper for some other exception = the "cause").

Ohk so I don't know how but the problem got solved. But I am not able to get the job done. I have added a button and a listener to it but it doesn't yet draws a circle.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Button;
import java.awt.TextField;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

public class Circle extends Applet {
    TextField text1;
    Button btn;

    public void init() {
        text1   = new TextField(8);
        add(text1);
        btn     = new Button("Submit");
        add(btn);
    }

    public void paint(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;
        g2.drawString("Enter the radius of circle : ", 15, 25);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                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;
    }
}

This is piece of code that I need to get working and replace the ActionListener used above with this. But action(Event, Object) is also deprecated. Can you tell me any alternate function that could be used instead of this?

public boolean action(Event event, Object object) {
    repaint();
    return true;
}

Sorry, I haven't used awt for 15 years, and I have forgotten whatever I did know.
I guess you will just have to search through the API doc.

But there are some general errors to fix...
the ONLY thing you should ever do in paint(Graphics g) is paint. You have no control over when or how often it will be called. So, for example, if you add an action listener in there, an new action listener will be added every time awt decides a repaint is needed.
The right thing to do is to add a listener to the button as part of your init. In the listener get the radius and store that in an instance variable, then call repaint().
In your paint just use the current value of radius to paint the text and circle.

Sorry, I haven't used awt for 15 years, and I have forgotten whatever I did know.
I guess you will just have to search through the API doc.

But there are some general errors to fix...
the ONLY thing you should ever do in paint(Graphics g) is paint. You have no control over when or how often it will be called. So, for example, if you add an action listener in there, an new action listener will be added every time awt decides a repaint is needed.
The right thing to do is to add a listener to the button as part of your init. In the listener get the radius and store that in an instance variable, then call repaint().
In your paint just use the current value of radius to paint the text and circle.

Yep. Got it working. Thanks.

// Java Document
import java.applet.Applet;
import java.awt.Color;
import java.awt.TextField;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

public class Circle extends Applet implements ActionListener {
    TextField text1;

    public void init() {
        text1   = new TextField(8);
        add(text1);
        text1.addActionListener(this);
    }

    public void paint(Graphics g) {
        Graphics2D g2   = (Graphics2D) g;
        g2.drawString("Enter the radius of circle : ", 15, 25);
        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);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}

Please mark this "solved" if it's working now.
Thanks
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.