Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Awt How to Create Circle In Java

Related Tutorials


 
 

Share on Google+Share on Google+

How to Create Circle In Java

Advertisement
This is a simple program of java awt. In this section, you will learn how to create Circle Diagram. The java circle is the most fundamental abstractions in a Java 2D in the supported java.awt.shape package. It describes a shape. But the Java 2D definitio

How to Create Circle In Java

     

Introduction:

This is a simple program of java awt. In this section, you will learn how to create Circle Diagram. The java circle is the most fundamental abstractions in a Java 2D in the supported  java.awt.shape package. It describes a shape. But the Java 2D definition of a shape does not require the shape to enclose an area. The Java2D API  also provides several classes that defines common Geometric Objects. 

Program Description:

In this program, you will also show that how to create square drawing. Inside the program define a class name CircleDraw for the circle component.  There is created two types of diagrams "Circle" and "square" for this program. Here, this program uses Ecllpise2D that is implemented to specify eclipse float and double. double and float components are represented for creating the square and circle in this program. 

setPaint(): This class is used for filling the color.

Here is the code of this program:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class CircleDraw extends Frame {
  Shape circle = new Ellipse2D.Float(100.0f100.0f100.0f100.0f);
  Shape square = new Rectangle2D.Double(100100,100100);
  public void paint(Graphics g) {
  Graphics2D ga = (Graphics2D)g;
  ga.draw(circle);
  ga.setPaint(Color.green);
  ga.fill(circle);
  ga.setPaint(Color.red);
  ga.draw(square);
  }

  public static void main(String args[]) {
  Frame frame = new CircleDraw();
  frame.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  frame.setSize(300250);
  frame.setVisible(true);
  }
}

Output this program:

Download this program.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Related Tutorials

Discuss: How to Create Circle In Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:8
bill
April 21, 2011
Draw Circle using user input

I can do rectangles,circles ect.. putting the numbers in before running, but how can you take user input, say from using joptionpane for the size , have place already coded in, pass input to the drawing method. Ex. What shape do you want to draw? Enter 1-square 2-circle 3-rectngle.(Square)then Enter side length of square: 30. then it draws a 30x30 on a window. then asks again what shape
umesh
September 27, 2011
computer graphics

Q 1. (Drawing an in-circle and an ex-circle) (a) (Pencil and paper) Derive the equations for computing the center of an ex-circle by walking along two edges and perpendicular to the edges. Explain how the solutions to the equations will be used to construct the bounding square for the circle. (b) (Pencil and paper) Derive the equations for computing the center of an incircle by walking along two of the angle bisectors. Explain how the solutions to the equations will be used to construct the bounding square for the circle. (c) Implement the solutions using java programs. The programs should execute as: java h3q11 Ax Ay Bx By Cx Cy and java h3q12 Ax Ay Bx By Cx Cy where all the command line parameters are float values that specify the coordinates of the vertices. The mapping from real to screen is just an inversion of the Y-axis. Choose a suitably large frame size (600 should be OK). Choose a side such that the ex-circle is fully visible and extend the other two sides to show the tangency. The program may require the mathematical solution to be tweaked so that the circles looks tangential. (d) Report (due on tuesday October 4). What adaptations did you have to make to the mathe- matical solution to make the circles looks tangential?
Ajay
October 23, 2011
JAVA

please send me the source code for the program"to draw line,circle,rectangle & ellipse"
Gerard
December 2, 2011
Help me guys

what is the code for making 3 Oval?
pin2
January 10, 2012
incomplt code

from next tym dont dare to post incomplete codes in the site...u think we are fools siiting out here to download ur code..........could have used more slang, but sparing u..god bless u....
Ankit jain
February 27, 2012
bluej

if main is user define function then a program can't run without intialing main()?why........ Since it is a user function, we can give any name to function but in case of main() it's not so.
LOL
November 13, 2012
I LIED

This just a troll trolling. Consider yourself warned. Be lucky you were not Rick Rolled.
jel
August 15, 2011
java

"What is the possible code when getting the Area of a Circle?"
DMCA.com