Java – Draw circle in applet

Drawing a perfect circle can be challenging, but with the Draw Circle applet, it’s easy and fun! This user-friendly tool allows you to create flawless circles in just a few simple steps.

Here’s an example code in Java that demonstrates how to draw a circle using an applet:

This code creates an Applet subclass called DrawCircleApplet, which overrides the paint method to draw a circle using the Graphics class. The circle is drawn in the center of the applet, with a radius of 50 pixels.

When you run this code as an applet, a window will appear with a circle drawn in the center of it.

Here’s an explanation of the code:

The import statement brings in the necessary classes for drawing graphics and creating an applet:

The DrawCircleApplet class extends Applet, which is a Java class used for creating applets. The paint method is overridden to draw the circle:

The paint method is automatically called whenever the applet needs to be redrawn, such as when the applet is first displayed or when it is resized. The method takes a Graphics object as a parameter, which is used to draw the circle. The first three lines of the method calculate the center point of the applet and the radius of the circle, just like in the previous example. The last line calls the drawOval method on the Graphics object to draw the circle, just like before.

When you run this code as an applet, you will need to embed it in an HTML page to see it in action. Here’s an example HTML page that embeds the DrawCircleApplet:

The code attribute specifies the name of the applet class, and the width and height attributes specify the size of the applet. When you open this HTML page in a web browser, you should see a window with a circle drawn in the center.

Overall, this code demonstrates how to use an applet to draw a simple shape using the Graphics class. It also shows how to embed an applet in an HTML page to display it in a web browser.

Please check out our another tutorial for printing string using applet.

Scroll to Top