Skip to content

Draw number of Circles in Applet

September 7, 2013


–> draw applet circles from a given input. position of the circles is at the upper left corner of the app.

–> increase size and change positions of the circle as we paint to the applet.

–> show how to embed applet in a browser.

–> show how init and paint methods works as extending JApplet class.


  1. // imports neccessary class packages
  2. import javax.swing.*;
  3. import java.awt.*;
  4. //extends the JApplet class to be able to override its methods
  5. public class Ex14 extends JApplet
  6. {
  7. //declare the initial position and size of the circle
  8. private int x = 30;
  9. private int y = 30;
  10. private int w = 30;
  11. private int h = 40;
  12. private int noOvals;
  13. //one of the method of JApplet Class. the init method initializes and runs first when the app is called
  14. public void init()
  15. {
  16. try
  17. {
  18. //for the dialog to pop out asking question you know the answer to
  19. String str = JOptionPane.showInputDialog(“enter no. of Ovals”);
  20. noOvals = Integer.parseInt(str);
  21. }
  22. //if your answer is not valid, this section run. called error handling
  23. catch(NumberFormatException e)
  24. {
  25. JOptionPane.showMessageDialog(null, “ERROR: ” + e.getMessage(), “error”,
  26. JOptionPane.ERROR_MESSAGE);
  27. }
  28. }
  29. //also another method of the JApplet class. deals with drawing and appearances.
  30. public void paint(Graphics g)
  31. {
  32. super.paint(g);
  33. //the loop changes the size and position of the circle
  34. while(noOvals > 0)
  35. {
  36. g.drawOval(x, y, w, h);
  37. x += 5;
  38. y += 5;
  39. w += 5;
  40. h += 5;
  41. //decreases the no. of ovals and stops the loop until the condition isn’t met anymore.
  42. noOvals–;
  43. }
  44. }
  45. }

Below is how you embed the applet to a browser. using the applet tag of html.


  1. <html>
  2. <head>
  3. <title>example 14</title>
  4. </head>
  5. <body>
  6. // the “Ex14.class” is the class file of the java file of the java code above. the size is then defined
  7. <applet code = “Ex14.class” width = “300” height = “300”>
  8. </applet>
  9. </body>
  10. </html>

Simple, Free Image and File Hosting at MediaFire

this dialog first pops up when you run the applet

Simple, Free Image and File Hosting at MediaFire


I should have used some <pre> tags to maintain proper indenting of the codes. probably next time. Just comment if you have something to add or corrections on my post.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.


Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.


A simple guide to programming...


Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans


Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media


News About Tech, Money and Innovation


The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

Waterlooville Designs

A Professional Graphic Designer In Waterlooville.

%d bloggers like this: