Escribir encima de una Imagen


package texto; import javax.swing.*; import java.awt.*; import java.awt.font.*; import java.awt.geom.*; public class Main extends JFrame { public Main() { super("Un super Autazo"); } @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; int w = getSize().width; int h = getSize().height; // Se dibuja la imagen Image im = (new ImageIcon("src/Imagen/MiAuto.jpg")).getImage(); g2.drawImage(im,0,0,this); // Se prepara el texto FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Times",Font.BOLD,w/15); String s = "Les presento mi auto"; TextLayout tl = new TextLayout(s,f,frc); float sw = (float)tl.getBounds().getWidth(); AffineTransform transform = new AffineTransform(); transform.setToTranslation(w/2-sw/2,h*3/8); g2.setColor(Color.RED); // Se dibuja sólo el contorno Shape shape = tl.getOutline(transform); g2.draw(shape); // Se dibuja el texto completo tl.draw(g2,w/2-sw/2,h*5/8); } public static void main(String[] args) { Main v = new Main(); v.setSize(445,335); v.setDefaultCloseOperation(EXIT_ON_CLOSE); v.setVisible(true); v.setLocationRelativeTo(null); } }

Comentarios