The subject of the 2D graphics added in Java 2 could be the subject of an entire book, and in fact, it is. Java 2D Graphics by Jonathan Knudsen (O’Reilly) covers every imaginable aspect of this comprehensive new graphics package. Here I’ll just show one example, that of drawing text with a textured background.
The Graphics2D
class is a direct subclass of the
original Java Graphics
object. In fact, in Java 2,
your paint( )
method is always called with an
instance of Graphics2D
. So, it suffices to begin
your paint method by casting appropriately:
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g;
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
TexturePaint
, 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 Section 12.7) can be used.
Figure 12-5 shows the resulting screen (it looks
even better in color).
The program that produced this is shown in Example 12-4 .
Example 12-4. TexturedText.java
import java.awt.*; import java.awt.event.*; import java.awt.image.*; /** Text with a Texture */ public class TexturedText extends Component { /** The image we draw in the texture */ protected BufferedImage bim; /** The texture for painting. */ TexturePaint tp; /** The string to draw. */ String mesg = "Stripey"; /** The font */ Font myFont = new Font("Lucida Regular", Font.BOLD, 72); /** "main program" method - construct and show */ public static void main(String av[]) { // create a TexturedText object, tell it to show up final Frame f = new Frame("TexturedText"); TexturedText comp = new TexturedText( ); f.add(comp); f.addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { f.setVisible(false); f.dispose( ); System.exit(0); } }); f.pack( ); f.setLocation(200, 200); f.setVisible(true); } protected static Color[] colors = { Color.green, Color.red, Color.blue, Color.yellow, }; /** Construct the object */ public TexturedText( ) { super( ); setBackground(Color.white); int width = 8, height = 8; bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bim.createGraphics( ); for (int i=0; i<width; i++) { g2.setPaint(colors[(i/2)%colors.length]); g2.drawLine(0, i, i, 0); g2.drawLine(width-i, height, width, height-i); } Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight( )); tp = new TexturePaint(bim, r); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(tp); g2.setFont(myFont); g2.drawString(mesg, 20, 100); } public Dimension getMinimumSize( ) { return new Dimension(250, 100); } public Dimension getPreferredSize( ) { return new Dimension(320, 150); } }
3.14.131.47