Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Java2D Code Examples

TexturedText

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
TexturedText package com.ack.gui.awt.geometry; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; public class TexturedText extends Frame { public static void main( String[] argv ) { TexturedText myExample = new TexturedText( "Using Text Textures" ); } public TexturedText( String title ) { super( title ); setSize( 450, 180 ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent we ) { dispose(); System.exit( 0 ); } } ); setVisible( true ); } public void paint( Graphics g ) { Graphics2D g2d = (Graphics2D) g; FontRenderContext frc = g2d.getFontRenderContext(); Font font = new Font( "Helvetica", Font.ITALIC | Font.BOLD, 72 ); TextLayout tl = new TextLayout( "Excellent!", font, frc ); Shape myShape = tl.getOutline( AffineTransform.getTranslateInstance( 50, 100 ) ); Paint myPaint = loadTextureResource( "1.gif" ); g2d.setPaint( myPaint ); g2d.fill( myShape ); } public TexturePaint loadTextureResource( String absfilename ) { MediaTracker tracker = new MediaTracker( this ); Image imtexture = Toolkit.getDefaultToolkit().getImage( absfilename ); tracker.addImage( imtexture, 0 ); try { tracker.waitForID( 0 ); int width = imtexture.getWidth( this ); int height = imtexture.getHeight( this ); System.out.println( "width" + width + " height =" + height ); BufferedImage buffImg = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB ); Graphics g = buffImg.getGraphics(); g.drawImage( imtexture, 0, 0, this ); return new TexturePaint( buffImg, new Rectangle2D.Double( 0, 0, width, height ) ); } catch( Exception e ) { System.out.println( "Exception on Image-Texture Loading" ); } return null; } }