banner



How To Draw A Circle Java

Depict a Circle in Java

  1. Draw a Circle Using the drawOval() Function in Coffee
  2. Draw a Circle Using the drawRoundRect() Function in Java
  3. Draw a Circle Using Shape and draw() in Java

In this article, nosotros will introduce how we can draw a circumvolve in Java using libraries coffee.awt and javax.swing that are used to create GUI components. We will employ several functions of the Graphics2d class that provides more control over the graphical components that we want to draw on the screen.

Depict a Circle Using the drawOval() Function in Coffee

In the first instance, we use the method drawOval() of the Graphics2D class that could create oval shapes, but to create a perfect circumvolve. To draw the circumvolve, we first override the function pigment(Graphics g) that has paints the component using the Graphics class.

We bandage Graphics to Graphics2D type to use its methods and properties. Now we call the drawOval() function and laissez passer four arguments. The first 2 arguments are the ten and y coordinates of the circle, while the last two arguments specify the width and the superlative of the circle to be fatigued.

At concluding, we extend JFrame from the javax.swing package to create a window frame with the specified size and backdrop.

              import javax.swing.*; import java.awt.*;  public class DrawCircle extends JFrame {      public DrawCircle(){         setTitle("Drawing a Circle");         setSize(400, 400);         setVisible(true);         setDefaultCloseOperation(EXIT_ON_CLOSE);      }      @Override     public void paint(Graphics thou) {         Graphics2D g2d = (Graphics2D) g;         g2d.drawOval(150, 150, 100, 100);      }      public static void principal(String[] args) {         new DrawCircle();      } }                          

Output:

Java draw a circle using drawoval

Draw a Circumvolve Using the drawRoundRect() Office in Coffee

Another method of the Graphics2D course chosen drawRoundRect() can also be used to draw a circle. As its proper noun suggests, it paints a rounded rectangle. Just like the get-go example, nosotros override the pigment(Graphics yard) part and then telephone call the drawRoundRect() method of the Graphics2D class.

drawRoundRect() accepts six arguments; the commencement two are x and y coordinates, the adjacent two tell the width and the height, and the terminal ii arguments are the width and height of the arc.

              import javax.swing.*; import java.awt.*;  public class DrawCircle extends JFrame {      public DrawCircle(){         setTitle("Cartoon a Circle");         setSize(250, 250);         setVisible(true);         setDefaultCloseOperation(EXIT_ON_CLOSE);      }      @Override     public void paint(Graphics chiliad) {         Graphics2D g2d = (Graphics2D) g;         g2d.drawRoundRect(40, 50, 90, 90, 200, 200);     }      public static void primary(String[] args) {         new DrawCircle();      } }                          

Output:

Java draw a circle using drawRoundRect

Depict a Circle Using Shape and draw() in Java

describe() is another function of the graphics2D class that takes a Shape as an argument. Shape is an interface that provides the properties of a geometrical shape. We want a circumvolve here, then we volition use Ellipse2D.Double() that defines the height and width of the ellipse every bit well as the 10 and y coordinates of the framing rectangle.

In the beneath example, nosotros pass the arguments to Ellipse2D.double(), and a created Shape object to the draw() method. This method will finally describe a circle in the window that we created using JFrame.

              import javax.swing.*; import java.awt.*; import java.awt.geom.Ellipse2D;  public course DrawCircle extends JFrame {      public DrawCircle() {         setTitle("Cartoon a Circumvolve");         setSize(250, 250);         setVisible(truthful);         setDefaultCloseOperation(EXIT_ON_CLOSE);      }      @Override     public void pigment(Graphics chiliad) {         Graphics2D g2d = (Graphics2D) g;         Shape circleShape = new Ellipse2D.Double(100, 100, 100, 100);          g2d.draw(circleShape);     }      public static void chief(String[] args) {          new DrawCircle();      } }                          

Output:

Java draw a circle using shape and draw

Write for us

DelftStack articles are written by software geeks like you lot. If y'all also would like to contribute to DelftStack by writing paid manufactures, you tin check the write for us page.

Related Article - Java GUI

  • Create Pop Window in Coffee
  • Draw a Line in Java
  • Ezoic

    Source: https://www.delftstack.com/howto/java/draw-a-circle-in-java/

    Posted by: snyderthand1938.blogspot.com

    0 Response to "How To Draw A Circle Java"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel