Java Reference
In-Depth Information
int mid - y = dy/2;
int mid - x = dx/2;
int rect - x = 3*dx/4;
int rect - y = 3*dy/4;
// Set current drawing color
g.setColor (Color.BLACK);
// Draw a rectangle centered at the mid-point
g.drawRect (mid - x-rect - x/2, mid - y-rect - y/2,
rect - x, rect - y);
// Set a new drawing color
g.setColor (Color.LIGHT - GRAY);
// Fill a rectangle centered at the mid-point. Put it
// within the previous rectangle so that border shows.
g.fillRect (mid - x-rect - x/2 + 10, mid - y-rect - y/2+10,
rect - x-20, rect - y-20);
// Set a new drawing color
g.setColor (Color.DARK - GRAY);
// Draw a circle around the mid-point
g.drawOval (mid - x-rect - x/6, mid - y-rect - y/6,
rect - x/3, rect - y/3);
// Fill an oval inside the circle
g.fillOval (mid - x-rect - x/6+10, mid - y-rect - y/6+10,
rect - x/3—20, rect - y/3—20);
} // paintComponent
} // class DrawingPanel
6.7.2 Drawing text
Often in a drawing you want to include text such as placing labels on the axes
of a graph or adding a title. Here we discuss the basic text drawing capabilities
of the Graphics class. Java2D provides much more extensive text drawing
capabilities, such as drawing Arabic text right to left, but that is beyond the scope
of this topic [5].
The first step in drawing text involves choosing the font. With Java 2D all
the fonts available on the platform can be used, but for nominal text you can
just specify one of the five standard fonts given by the following logical names:
Serif , SansSerif , Monospaced , Dialog , and DialogInput . These fonts
 
Search WWH ::




Custom Search