banner



How To Draw Dotted Line In Java Swing

Book Home Java Enterprise in a Nutshell Search this book

4.v. Stroking Lines

One of the new graphic attributes defined by Java 2D is the coffee.awt.Stroke; information technology is set with the setStroke() method of a Graphics2D object. The Stroke attribute is used by Java 2d whenever it draws a line. Conceptually, the Stroke describes the pen or brush that is used to draw the line: information technology controls all line-drawing attributes, such every bit line width and dash design. Java 2nd defines a single implementation of the Stroke interface, java.awt.BasicStroke, that is suitable for almost all line cartoon needs.

4.5.1. BasicStroke

A BasicStroke object encapsulates several dissimilar line drawing attributes: the line width, the dash pattern, the cease cap mode for the line, and the join manner for the line. You specify values for these attributes when y'all call the BasicStroke() constructor. BasicStroke objects are immutable, so that they can be safely cached and shared. This ways, however, that they don't have prepare() methods that let yous to alter the attribute values.

The line-width attribute specifies (obviously) the width of the line. This line width is measured in units of user space. If you are using the default coordinate system, then user space equals device space, and line widths are measured in pixels. For backward compatibility, the default line width is 1.0. Suppose you desire to describe the outline of a circumvolve of radius 100, using a line that is ten units wide. You can lawmaking it like this:

Graphics2D g;                                       // Initialized elsewhere Shape circumvolve = new Ellipse2D.Bladder(100.0f, 100.0f,  // Upper-left corner                                    300.0f, 300.0f); // Width and pinnacle grand.setStroke(new BasicStroke(10.0f));                // Set line width g.draw(circle);                                     // Now depict information technology

The end-cap attribute specifies how the ends of lines are fatigued, or, more than specifically, what type of end caps are placed at the end of lines. There is no analogous line aspect in AWT prior to Java 2d, as cease caps are necessary only for lines that are more than one-pixel wide. If you are non familiar with end caps, expect at Figure 4-2, as they are best explained visually. This figure shows what lines look like when fatigued with each of the iii possible end cap styles.

figure

Figure 4-two. BasicStroke end-cap styles

The BasicStroke.CAP_BUTT constant specifies that the line should have no stop cap. The CAP_SQUARE constant specifies a rectangular end cap that projects beyond the terminate point of the line past a distance equal to half of the line width; this is the default value for the end-cap attribute. CAP_ROUND specifies a semicircular end cap, with a radius equal to one-half of the line width.

The bring together-mode attribute is like to the end-cap attribute, except that it applies to the vertex where 2 lines join, rather than to the terminate of a line. Like the end-cap attribute, the join-fashion attribute is necessary only with wide lines and is all-time understood visually. Figure 4-3 illustrates this BasicStroke attribute. Note that the join way attribute is used only when drawing a shape that includes multiple line segments, not when 2 intersecting lines are drawn as divide shapes.

figure

Effigy 4-iii. BasicStroke join styles

The default join style is a mitered join, represented by the BasicStroke.JOIN_MITER abiding. This value specifies that lines are joined by extending their outer edges until they meet. The JOIN_BEVEL abiding specifies that lines are joined by drawing a straight line between the exterior corners of the two lines, while JOIN_ROUND specifies that the vertex formed by the two lines should be rounded, with a radius of half the line width. To utilize cap manner and join style, y'all can use code similar this:

g.setStroke(new BasicStroke(v.0f,                     // Line width                             BasicStroke.CAP_ROUND,    // Terminate-cap style                             BasicStroke.JOIN_ROUND)); // Vertex join way

When you use the JOIN_MITER style to bring together 2 lines that take a small angle between them, the miter tin can become quite long. To avert this situation, BasicStroke includes some other aspect known as the miter limit. If the miter would be longer than this value times one-half of the line width, information technology is truncated. The default miter limit is ten.0.

The dash design of a line is actually controlled by two attributes: the nuance assortment and the nuance phase. The dash array is a bladder[] that specifies the number of units to exist fatigued followed by the number of units to be skipped. For example, to draw a dashed line in which both the dashes and spaces are 25 units long, you use an assortment like:

new float[] { 25.0f, 25.0f }

To draw a dot-nuance blueprint consisting of 21 on, nine off, iii on, and ix off, yous employ this array:

new float[] { 21.0f, 9.0f, 3.0f, 9.0f }

Figure 4-4 illustrates these dashed-line examples. The end-cap style you lot specify is applied to each dash that is fatigued.

figure

Effigy 4-four. BasicStroke dash patterns

If, for some reason, yous want to draw a dashed line only do not want your line to begin at the showtime of the dash pattern, you can specify the nuance-phase attribute. The value of this aspect specifies how far into the dash pattern the line should begin. Note, however, that this value is not an integer index into the dash blueprint array. Instead, it is a floating-point value that specifies a linear distance.

To draw a dashed line, yous must use the almost complicated BasicStroke() constructor and specify values for all attributes. For example:

Stroke due south = new BasicStroke(4.0f,                      // Width                            BasicStroke.CAP_SQUARE,    // End cap                            BasicStroke.JOIN_MITER,    // Bring together style                            10.0f,                     // Miter limit                            new float[] {16.0f,20.0f}, // Nuance design                            0.0f);                     // Dash phase

4.5.two. How a Stroke Works

The BasicStroke form is sufficient for most drawing needs, and so it is unlikely that you will always need to implement the Stroke interface yourself. Still, the Stroke interface defines but a single createStrokedShape() method, and information technology is instructive to understand what this method does.

In Java 2D, filling an area is a more fundamental operation than cartoon (or stroking) the outline of a shape. The Stroke object is the link betwixt the two operations; it makes it possible to implement the describe() method using the fill() method.

Recall the code that nosotros only used to draw the outline of a circumvolve. The describe() method has to draw the outline of the circle using only the make full() method. If it but calls make full() on the circle, it ends up creating a solid deejay, not the outline of a circumvolve. So instead, describe() outset passes the circumvolve to the createStrokedShape() method of the BasicStroke object we've specified. createStrokedShape() returns a new shape: a circumvolve of radius 105, minus a concentric circle of radius 95. The interior of this shape is the area between the two circles, a region that always has a width of 10 units. At present draw() can call fill() on this stroked shape to draw the ten-unit of measurement-wide outline of the original circle. (We'll discuss the fill() performance and the graphics attributes that information technology uses in the next section.)



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.

Source: https://docstore.mik.ua/orelly/java-ent/jfc/ch04_05.htm

Posted by: parentdights.blogspot.com

0 Response to "How To Draw Dotted Line In Java Swing"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel