miércoles, 12 de octubre de 2011

Imagen dentro de un texto en [java]



Hoy les traigo una forma de colocar una imagen dentro del textoutilizandoelmétodopaint(Graphics g), es bastante sencillo con un efecto muy vistoso.
fragmento delcódigo


Graphics2D g2 = (Graphics2D)g;
int w = getSize().width;
int h = getSize().height;
// Se dibuja la imagen
Image im = (new ImageIcon("1600x1200.jpg")).getImage();
// Se prepara el texto
FontRenderContext frc = g2.getFontRenderContext();
Font f = new Font("Arial Black",Font.BOLD,180);
TextLayout tl = new TextLayout("JavaFace",f,frc);
float sw = (float)tl.getBounds().getWidth();
AffineTransform transform = new AffineTransform();
transform.setToTranslation(w/2-sw/2,h*5/8);
Shape shape = tl.getOutline(transform);
// Se dibuja la imagen en el interior del contorno
g2.setClip(shape);
g2.drawImage(im,0,0,this);
// Se dibuja sólo el contorno
g2.setColor(Color.YELLOW);
g2.draw(shape);



Si lo imaginas,lo programas


No hay comentarios:

Publicar un comentario