HOW CREATE TEXTURE PROGRAM IN JAVA
//learn how use texture in java program in java using paint and rectangle2d packages
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author rajesh kumar shukla
*/
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 Texturedprogram extends Frame {
public static void main( String[] argv ) {
Texturedprogram myExample = new Texturedprogram( "Using Text Textures" );
}
public Texturedprogram( 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( "WELCOME!", 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;
}
}
0 comments:
Post a Comment