package elte.java2_utikalauz5.java2d; import java.awt.*; import java.awt.image.*; import java.awt.event.*; /** peldaprogram virtualis kepernyok hasznalatara. @link.forrásfájl {@docRoot}/../data/java2d/src VirtualisKepernyo.java @link.letöltés {@docRoot}/../data/java2d VirtualisKepernyo.jar @since Java 2 Útikalauz programozóknak */ class VirtualisKepernyo extends Canvas { /** Verziószám */ private final static long serialVersionUID = 15L; BufferedImage kep; Image betoltottKep; public void kepBetoltes () { setBackground (Color.white); betoltottKep = getToolkit ().getImage (getClass().getClassLoader().getResource("kep.gif")); try { MediaTracker tracker = new MediaTracker (this); tracker.addImage (betoltottKep, 0); tracker.waitForID (0); } catch (Exception e) { } } public void paint (Graphics g) { Dimension meretek = getSize (); Graphics2D g2 = virtualisKepernyotLetrehoz (meretek.width, meretek.height); virtualisKepernyoreRajzol (g2, meretek.width, meretek.height); g2.dispose (); g.drawImage (kep, 0, 0, this); } public Graphics2D virtualisKepernyotLetrehoz (int w, int h) { Graphics2D g2 = null; if (kep == null || kep.getWidth () != w || kep.getHeight () != h) kep = (BufferedImage) createImage (w, h); g2 = kep.createGraphics (); g2.setBackground (getBackground ()); g2.setRenderingHint (RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.clearRect (0, 0, w, h); return g2; } public void virtualisKepernyoreRajzol (Graphics2D g2, int w, int h) { g2.drawImage (betoltottKep, 0, 0, this); } public static void main (String[] args) { Frame f = new Frame (); VirtualisKepernyo v = new VirtualisKepernyo (); v.kepBetoltes (); f.add (v); f.addWindowListener ( new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit (0); } } ); f.setSize (100, 100); f.setVisible (true); } }