Java Reference
In-Depth Information
uses
cap = BasicStroke.CAP_ROUND
. The class provides more features, such as
dashed lines, which we do not discuss here.
BasicStroke(float width)
constructs a solid
BasicStroke
with the specified
line width and with default values for the cap and join styles.
BasicStroke(float width, int cap, int join)
constructs a solid
Basic-
Stroke
with the specified attributes.
The following code snippet shows how to use class
Graphics2D
and
BasicStroke
to draw lines 5 and 6 pixels wide and have rounded and square ends respectively:
//
define strokes in the preamble
BasicStroke stroke5 =
new
BasicStroke(5.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL);
BasicStroke stroke6 =
new
BasicStroke(6.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
//
using the strokes in paintComponent
public void
paintComponent(Graphics g){
super
.paintComponent(gr);
//
cast to Graphics2D
Graphics2D g2d = (Graphics2D) g;
//
draw a line with width 5
g2d.setStroke(stroke5);
g2d.drawLine(10,10,30,40);
//
draw a line with width 6
g2d.setStroke(stroke6);
g2d.drawLine(10,40,30,10);
}
13.2
■
Finding the screen parameters
When displaying a frame, one often wants to fill only a certain area of the screen.
Those parts of the frame that are 'outside' the screen should be avoided because
the frame is large and the screen resolution is low. The following lines show how
to find the number pixels per column and row, and the screen resolution, i.e.
the number of pixels per inch. We do not explain the class
Toolkit
used for this
purpose. Just think of it as a class that allows access to information on the system.
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenDim = tk.getScreenSize();
int
screenHeight
= screenDim.height;
Search WWH ::
Custom Search