Java Reference
In-Depth Information
You can then use any
Graphics2D
methods or any regular
Graphics
methods, getting to
them with the object reference
g2
. One of the additional methods in
Graphics2D
is
setPaint()
, which can take the place of
setColor()
to draw with a solid color. However, it
can also be called with several other types, and in this case we pass in an object called a
Tex-
turePaint
, which refers to a pattern. Our pattern is a simple set of diagonal lines, but any
pattern or even a bitmap from a file (see
Drawing an Image
) can be used.
Figure 12-4
shows
the resulting screen (it looks even better in color); the program itself is shown in
Figure 12-4. TexturedText in action
Example 12-3. TexturedText.java
public
public class
class
TexturedText
TexturedText
extends
extends
JComponent
{
private
private static
long
serialVersionUID
=
8898234939386827451L
;
/** The image we draw in the texture */
protected
static final
final
long
protected
BufferedImage bim
;
/** The texture for painting. */
TexturePaint tp
;
/** The string to draw. */
String mesg
=
"Stripey"
;
/** The font */
Font myFont
=
new
new
Font
(
"Lucida Regular"
,
Font
.
BOLD
,
72
);
/** "main program" method - construct and show */
public
public static
void
main
(
String
[]
av
) {
// create a TexturedText object, tell it to show up
final
static
void
final
Frame f
=
new
new
Frame
(
"TexturedText"
);