banner



How To Draw An Arc In Java

Home » Coffee »    Java Graphics Programming

   Graphics is ane of the nearly important features of Java. Java applets tin be written to draw lines, arcs, figures, images and text in different fonts and styles. Different colors can also be incorporated in display.

Contents

  • The Graphics Class

  • Lines and Rectangles

  • Circles and Ellipses

  • Drawing Arcs

  • Drawing Polygons

The Graphics Class

The graphics course defines a number of drawing functions, Each shape can be drawn border-only or filled. To draw shapes on the screen, we may telephone call one of the methods available in the graphics class. The most unremarkably used drawing methods included in the graphics grade are listed below. To draw a shape, we simply need to use the appropriate method with the required arguments.

Drawing Methods of the Graphics Class

Sr.No Method Description
1. clearRect() Erase a rectangular area of the canvas.
ii. copyAre() Copies a rectangular area of the canvas to some other area
iii. drawArc() Draws a hollow arc
4. drawLine() Draws a straight line
5 drawOval() Draws a hollow oval
half dozen drawPolygon() Draws a hollow polygon
7 drawRect() Draws a hollow rectangle
8. drawRoundRect() Draws a hollow rectangle with rounded corners
ix. drawString() Display a text cord
10. FillArc() Draws a filled arc
eleven. fillOval() Draws a filled Oval
12. fillPolygon() Draws a filled Polygon
xiii. fillRect() Draws a filled rectangle
14. fillRoundRect() Draws a filled rectangle with rounded corners
15. getColor() Retrieves the current drawing color
xvi. getFont() Retrieves the currently used font
17. getFontMetrics() Retrieves the information about the current font
eighteen. setColor() Sets the drawing color
19. setFont() Sets the font

Lines and Rectangles

Lines are fatigued by means of the drawLine() method.

Syntax
void drawLine(int startX, int startY, int endX, int endY)

drawLine() displays a line in the current drawing color that begins at (start X, outset Y) and ends at (endX, end Y).


Later on this you can comiple your java applet plan as shown below:

"Output of Lines.class"

Download Complete Plan Rectangle

The drawRect() and fillRect() methods display an outlined and filled rectangle, respectively.

Syntax
void drawRect(int top, int left, int width, int elevation)
void fillRect(int top, int left, int width, int summit)

The upper-left corner of the rectangle is at(top,left). The dimensions of the rectangle are specified by width and height.

Use drawRoundRect() or fillRoundRect() to draw a rounded rectangle. A rounded rectangle has rounded corners.

Syntax
void drawRoundRect(int top, int left, int width, int height int Xdiam, int YDiam)
void fillRoundRect(int top, int left, int width, int acme int Xdiam, int YDiam)

The upper-left corner of the rounded rectangle is at (superlative,left). The dimensions of the rectangle are specified past width and height. The diameter of the ribdubg are along the X centrality are specified by x Diam. The diameter of the rounding are along the Y axis is specified by Y Diam.

After this you can comiple your java applet program equally shown beneath:


c:\jdk1.4\bin\javac Rectanlge.java
c:\jdk1.4\bin\appletviewer Rectanlge.coffee
"Output of Rectangle.class"

Download Complete Program

Circles and Ellipses

The Graphics class does non contain any method for circles or ellipses. To draw an ellipse, use drawOval(). To fill an ellipse, employ fillOval().

Syntax
void drawOval(int tiptop, int left, int width, int acme)
void fillOval(int top, int left, int width, int pinnacle)

The ellipse is drawn within a bounding rectangle whose upper-left corner is specified by (superlative,left) and whose width and height are specified by width and height. To describe a circumvolve, specify a square equally the bounding rectangle i.e get elevation = width.

The following program draws serveral ellipses:

After this you lot can comiple your java applet program as shown beneath:


c:\jdk1.4\bin\javac Ellipses.java
c:\jdk1.4\bin\appletviewer Ellipses.coffee

"Output of Ellipses.grade"

Download Complete Programme

Cartoon Arcs

An arc is a part of oval. Arcs can be drawn with depict Arc() and fillArc() methods.

Syntax
void drawArc(int top, int left, int width, int height, int startAngle, int sweetAngle)
void fillArc(int superlative, int left, int width, int acme, int startAngle, int sweetAngle)

The arc is divisional by the rectangle whose upper-left corner is specified by (peak,left) and whose width and height are specified past width and elevation. The arc is drawn from startAngle through the angular altitude specified by sweepAngle. Angles are specified in caste. '0o' is on the horzontal, at the 30' clock's position. The arc is fatigued conterclockwise if sweepAngle is positive, and clockwise if sweetAngle is negative.

The post-obit applet draws several arcs:

After this yous tin can comiple your coffee applet program as shown below:


c:\jdk1.4\bin\javac Arcs.java
c:\jdk1.4\bin\appletviewer Arcs.coffee

"Output of Arcs.class"
Download Complete Program

Drawing Polygons

Polygons are shapes with many sides. It may be considered a set of lines continued together. The end of the first line is the beginning of the second line, the end of the second line is the start of the third line, and so on. Utilize drawPolygon() and fillPolygon() to draw arbitrarily shaped figures.

Syntax
void drawPolygon(int ,[] , int y[], int numPointer)
void fillPolygon(int ,[] , int y[], int numPointer)

The polygon's endpoints are specified by the coordinate pairs contained within the 10 and y arrays. The number of points divers past x and y is specified past numPoints.

Information technology is obvious that ten and y arrays should be of the aforementioned size and we must repeat the beginning betoken at the end of the array for closing the polygon.

The following appplet draws an hourglass shape:

After this you can comiple your java applet plan every bit shown below:


c:\jdk1.4\bin\javac Polygon.coffee
c:\jdk1.4\bin\appletviewer Polygon.coffee
"Output of Polygon.form"
Download Complete Plan

<< Previous Topic
Next Topic >>

Source: https://javaproglang.blogspot.com/2014/03/java-graphics-programming.html

Posted by: greenoury1954.blogspot.com

0 Response to "How To Draw An Arc In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel