package elte.java2_utikalauz5.java2d; import java.awt.*; import java.awt.font.*; import java.awt.event.*; import java.text.*; /** peldaprogram kepek szovegekben valo elhelyezesere. @link.forrásfájl {@docRoot}/../data/java2d/src Kep.java @link.letöltés {@docRoot}/../data/java2d Kep.jar @since Java 2 Útikalauz programozóknak */ class Kep extends Canvas { /** Verziószám */ private final static long serialVersionUID = 15L; Image kep; public void kepBetoltes () { setBackground (Color.white); kep = getToolkit ().getImage (getClass().getClassLoader().getResource("kep.gif")); try { MediaTracker tracker = new MediaTracker (this); tracker.addImage (kep, 0); tracker.waitForID (0); } catch (Exception e) { } } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; Font betutipus = new Font ("Times New Roman", Font.BOLD, 12); AttributedString szoveg = new AttributedString ("Ebbe a szovegbe kell beagyazni a kepet."); ImageGraphicAttribute kepAttributum = new ImageGraphicAttribute (kep, GraphicAttribute.ROMAN_BASELINE); szoveg.addAttribute (TextAttribute.CHAR_REPLACEMENT, kepAttributum, 10, 11); AttributedCharacterIterator iterator = szoveg.getIterator (); FontRenderContext frc = g2.getFontRenderContext (); TextLayout megjelenito = new TextLayout (iterator, frc); g2.setColor (Color.red); megjelenito.draw (g2, 0.0f, 100.0f); } public static void main (String[] args) { Frame f = new Frame (); Kep k = new Kep (); k.kepBetoltes (); f.add (k); f.addWindowListener ( new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit (0); } } ); f.setSize (500, 300); f.setVisible (true); } }