Drawing Lines in a Circle Java
Drawing Lines and Shapes
Screenshot shows a Java app that illustrates the different things you'll be learning to describe during this hour: lines, rectangles, ellipses, and polygons.
Screenshot Displaying a sign using Java2D graphics.
With the exception of lines, all of the shapes you lot can draw can exist filled or unfilled. A filled shape is fatigued with the current colour completely filling the space taken upward by the shape. Unfilled shapes just draw a border with the current color. Earlier you create an app to draw this sign, we'll describe each of the cartoon methods. The comp2D object will be used as the Graphics2D object throughout this section.
By the Style
The sign shown in Screenshot is the international symbol that means "Employ of Compressed Air Prohibited," another one of the freedoms being stripped from us today similar smoking in restaurants, downloading MP3s, and ripping the tags off piece of furniture.
Drawing Lines
A 2D drawing operation in Java requires two steps:
- An object is created that represents the shape that is being drawn.
- A method of a Graphics2D object is called to actually draw that shape.
The objects that define shapes all are function of the java.awt.geom package of classes. There are ii classes that can be used to create lines: Line2D.Bladder and Line2D.Double. These classes differ but in the style they are created: One is created using floating-point numbers to specify the get-go (10,y) betoken and ending (x,y) point of the line, and the other uses double values. The following statement creates a line from the point (40,200) to the point (70,130):
Line2D.Float ln = new Line2D.Float(40F, 200F, 70F, 130F);
Each of the arguments to the Line2D.Float constructor is a number followed by an F, which indicates that the number is a floating-point value. This is necessary when creating a Line2D.Float object.
Past the Manner
Line2D.Float may look unusual because information technology includes a . character as part of the name. Normally, you would expect this to hateful that Line2D.Float refers to a variable called Float in the Line2D class. Float actually refers to an inner class that is defined inside the Line2D class. The capital that begins the proper name Float is an indicator that it's something other than a variable, considering course and object variable names are either all lowercase or all capital. Line2D.Float is treated similar whatever other class. The only divergence is the . in the name.
After y'all create a drawing object of any kind, whether information technology's a line, ellipse, rectangle, or some other shape, it is fatigued by calling a method of the Graphics2D class. The draw() method draws the shape equally an outline and the fill() method draws information technology every bit a filled shape. The following argument will draw the ln object created in the previous example:
comp2D.depict(ln);
Drawing Rectangles
Rectangles tin exist filled or unfilled, and they can have rounded corners or square ones. They can be created using the Rectangle2D.Float course and specifying the following four arguments:
- The x coordinate at the upper left of the rectangle
- The y coordinate at upper left
- The width of the rectangle
- The height
The post-obit statement draws an unfilled rectangle with foursquare corners:
Rectangle2D.Float rr = new Rectangle2D.Float(245F, 65F, 20F, 10F);
This statement creates a rectangle with its upper-left corner at the (x,y) coordinate of (245,65). The width of the rectangle is twenty and the height is 10. These dimensions are expressed in pixels, the same unit of measure used for coordinates. To draw this rectangle equally an outline, the post-obit statement could exist used:
comp2D.draw(rr);
If y'all want to make the rectangle filled in, apply the fill() method instead of draw(), as in this statement:
comp.fill(rr);
You can create rectangles with rounded corners instead of square ones by using the RoundRectangle2D.Float grade. The constructor to this grade starts with the aforementioned four arguments as the Rectangle2D.Bladder class, and adds the following two arguments:
- A number of pixels in the x direction away from the corner of the rectangle
- A number of pixels in the y direction away from the corner
These distances are used to make up one's mind where the rounding of the rectangle's corner should begin. The following statement creates a rounded rectangle:
RoundRectangle2D.Float ro = new RoundRectangle.Float( 10F, 10F, 100F, 80F, 15F, 15F);
This rectangle has its upper-left corner at the (10,10) coordinate. The concluding ii arguments to drawRoundRect() specify that the corner should begin rounding 15 pixels away from the corner at (10,10) too as the other iii corners. As with other rectangle methods, the 3rd and fourth arguments specify how wide and tall the rectangle should be. In this case, information technology should be 100 pixels wide and 80 pixels tall. Subsequently the rounded rectangle is created, it is drawn using the describe() and fill up() methods of a Graphics2D object.
Drawing Ellipses and Circles
Ellipses and circles can both be created with the same class: Ellipse2D.Float. This class takes four arguments: the (x,y) coordinates of the ellipse and its width and height. The (ten,y) coordinates exercise not indicate a bespeak at the center of the ellipse or circle, as you might expect. Instead, the (ten,y) coordinates, width, and superlative depict an invisible rectangle that the ellipse fits into. The (x,y) coordinate is the upper-left corner of this rectangle. If it has the same width and height, the ellipse is a circle. The following argument creates a circle at (245,45) with a tiptop and width of 5 each:
Ellipse2D.Float cir = new Ellipse2D.Float( 245F, 45F, 5F, 5F);
Drawing this object requires a telephone call to draw(cir) or fill(cir).
Cartoon Arcs
Another circular shape you can draw in Coffee is an arc, a partial ellipse or circle. Arcs are created using the Arc2D.Bladder course, which has a constructor method with many of the same arguments. Y'all draw the arc by specifying an ellipse, the portion of the ellipse that should be visible (in degrees), and the place the arc should begin on the ellipse. To create an arc, specify the following integer arguments to the constructor:
- The ten coordinate of the invisible rectangle that the ellipse fits into
- The y coordinate of the rectangle
- The width of the rectangle
- The height of the rectangle
- The point on the ellipse where the arc should begin (in degrees from 0 to 359)
- The size of the arc, besides in degrees
- The type of arc it is
The arc's starting point and size range from 0 to 359 degrees in a counterclockwise direction, beginning with 0 degrees at the 3 o'clock position, equally shown in Screenshot.
Screenshot How arcs are defined in degrees.
The type of arc is specified by using one of three class variables from the Arc2D.Float class: PIE if the arc should be fatigued as a slice of a pie graph, Closed if the endpoints of the arc should exist continued with a straight line, and Open if the endpoints should non be connected. The following argument draws an open arc at (100,50) that is 120 degrees long, begins at the 30-caste marker, and has a height and width of 75 each:
Arc2D.Bladder smile = new Arc2D.Bladder(100F, 50F, 75F, 75F, 30F, 120F, Arc2D.Float.OPEN);
You will work more with arcs later in this hour'south workshop.
Drawing Polygons
Polygons are the most complicated shape to create considering they have a varying number of points. The first pace in creating a polygon is to create a GeneralPath object that will hold information technology. This object starts off without any points in the polygon being defined:
GeneralPath polly = new GeneralPath();
Once this object is created, its moveTo() method is used to ascertain the showtime bespeak in the polygon. For a closed polygon, such as an octagon, this can be whatever of the points. You lot can also ascertain open polygons, and, in this case, your get-go point should exist 1 of the stop points of the polygon. The moveTo() method takes 2 arguments: an x and a y coordinate. This (x,y) coordinate defines the signal on the polygon. The following statement is an example:
polly.moveTo(100F, 20F);
Later you have established the initial bespeak in this fashion, each successive point is added with the lineTo() method. This method also takes an ten coordinate and y coordinate as arguments, every bit in the following statement:
polly.lineTo(80F, 45F);
Y'all tin telephone call lineTo() as often every bit needed to create the sides of the polygon. When you lot have added the terminal bespeak, y'all can close off the polygon by calling the closePath() method with no arguments:
polly.closePath();
If you don't call closePath(), the polygon volition be open. When you take finished creating the shape, it tin be drawn with the draw() and fill() methods like any other drawn object.
Drawing Lines of Different Widths
If you tried out all of the drawing operations you lot have learned about up to this betoken, yous would come across that they describe lines that are one pixel wide. You lot tin can change the size of the line past using the BasicStroke class, office of the java.awt package. Objects of this course correspond the width of the "drawing pen," and yous can create one by calling the BasicStroke( int ) constructor, as in the following case:
BasicStroke pen = new BasicStroke(12);
The integer specified in the constructor represents the width of the pen in pixels. Using BasicStroke objects is similar to working with fonts and colors. To draw with a pen you have created, phone call the setStroke() method of the Graphics2D class with the pen equally an argument. The following instance creates a 3-pixel broad stroke and assigns it to a Graphics2D object called comp2D:
BasicStroke brush = new BasicStroke(3); comp2D.setStroke(castor);
Everything you draw will apply the pen until you select a dissimilar i.
Creating the Sign
To put all of these shapes together, load your word processor and create a new file called Sign.java. Enter Listing 23.1 into the file and salvage it when you're done.
List 23.1. The Total Text of Sign.java
1: import java.awt.*; 2: import javax.swing.*; three: import coffee.awt.geom.*; 4: 5: public class Sign extends JFrame { 6: public Sign() { 7: super("Utilise of Compressed Air Prohibited"); 8: setSize(410, 435); 9: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 10: SignPanel sp = new SignPanel(); xi: add together(sp); 12: setVisible(true); 13: } 14: 15: public static void main(String[] arguments) { 16: Sign sign = new Sign(); 17: } xviii: } xix: 20: class SignPanel extends JPanel { 21: public void paintComponent(Graphics comp) { 22: super.paintComponent(comp); 23: Graphics2D comp2D = (Graphics2D) comp; 24: comp2D.setColor(Color.white); 25: comp2D.fillRect(0, 0, 400, 400); 26: 27: // draw sign 28: comp2D.setColor(Color.cerise); 29: Ellipse2D.Bladder sign1 = new Ellipse2D.Float(0F, 0F, 400F, 400F); xxx: comp2D.fill(sign1); 31: comp2D.setColor(Color.white); 32: Ellipse2D.Bladder sign2 = new Ellipse2D.Float(55F, 55F, 290F, 290F); 33: comp2D.fill up(sign2); 34: 35: // describe human 36: comp2D.setColor(Color.black); 37: Ellipse2D.Float head = new Ellipse2D.Bladder(160F, 96F, 32F, 32F); 38: comp2D.fill(head); 39: GeneralPath torso = new GeneralPath(); forty: body.moveTo(159F, 162F); 41: torso.lineTo(119F, 303F); 42: body.lineTo(139F, 303F); 43: body.lineTo(168F, 231F); 44: body.lineTo(190F, 303F); 45: body.lineTo(213F, 303F); 46: body.lineTo(189F, 205F); 47: body.lineTo(203F, 162F); 48: body.lineTo(247F, 151F); 49: body.lineTo(236F, 115F); 50: body.lineTo(222F, 118F); 51: body.lineTo(231F, 140F); 52: body.lineTo(141F, 140F); 53: torso.lineTo(101F, 190F); 54: trunk.lineTo(112F, 197F); 55: body.lineTo(144F, 162F); 56: body.closePath(); 57: comp2D.fill(body); 58: 59: // describe can 60: comp2D.setColor(Color.blackness); 61: comp2D.fillRect(251, 191, 33, 118); 62: comp2D.fillRect(262, 160, 13, 31); 63: GeneralPath nozzle = new GeneralPath(); 64: nozzle.moveTo(252F, 176F); 65: nozzle.lineTo(262F, 160F); 66: nozzle.lineTo(262F, 176F); 67: nozzle.closePath(); 68: comp2D.fill(nozzle); 69: 70: // draw lines 71: comp2D.setColor(Color.blackness); 72: BasicStroke pen = new BasicStroke(2F); 73: comp2D.setStroke(pen); 74: Line2D.Float ln1 = new Line2D.Bladder(203F, 121F, 255F, 102F); 75: comp2D.describe(ln1); 76: Line2D.Float ln2 = new Line2D.Float(255F, 102F, 272F, 116F); 77: comp2D.draw(ln2); 78: Line2D.Float ln3 = new Line2D.Bladder(272F, 134F, 252F, 158F); 79: comp2D.draw(ln3); 80: 81: // finish sign 82: comp2D.setColor(Color.red); 83: GeneralPath sign3 = new GeneralPath(); 84: sign3.moveTo(110F, 78F); 85: sign3.lineTo(321F, 289F); 86: sign3.lineTo(290F, 317F); 87: sign3.lineTo(81F, 107F); 88: sign3.closePath(); 89: comp2D.fill(sign3); ninety: } 91: }
Afterwards compiling the programme successfully, run information technology at the command line. The end result was displayed in Screenshot.
Source: http://underpop.free.fr/j/java/teach-yourself-programming-with-java-in-24-hours/ch23lev1sec2.html
Post a Comment for "Drawing Lines in a Circle Java"