/* * @(#)J2DCanvas.java 1.20 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.*; import java.awt.print.*; import java.awt.image.BufferedImage; import com.sun.java.swing.JApplet; import java.net.URL; /** * All demos extend this J2DCanvas Abstract Canvas class. From * this class demos must implement the drawDemo method. This * class handles animated demos, the demo must set the runnable * boolean to true. */ public abstract class J2DCanvas extends Canvas implements Runnable, Printable { public Object AntiAlias = RenderingHints.VALUE_ANTIALIAS_ON; public Object Rendering = RenderingHints.VALUE_RENDER_SPEED; public int Screen = 2; // default to off-screen public Object texture; public AlphaComposite composite; public String perfStr; // PerformanceMonitor public BufferedImage offImg; public boolean isRunning; // ImageObserver demos public boolean runnable; public Thread thread; protected int w, h; protected boolean newBufferedImage; protected boolean clearCanvas = true; protected int sleepAmount; protected Font font = new Font("Dialog", Font.PLAIN, 12); private long orig, start, frame; private String className; private Toolkit toolkit; public J2DCanvas() { className = this.toString(); className = className.substring(className.lastIndexOf(".")+1); className = className.substring(0,className.indexOf("[")) + " "; orig = System.currentTimeMillis(); start = orig; toolkit = getToolkit(); } protected Image getImage(String fileName) { URL url = J2DCanvas.class.getResource(fileName); Image img = toolkit.getImage(url); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) { e.printStackTrace(); } return img; } public void setup() { if (Java2Demo.controls.screenCombo.getSelectedIndex() == 0) { if (runnable || isRunning) { Screen = 2; } else { Screen = 1; } } else { Screen = Java2Demo.controls.screenCombo.getSelectedIndex(); } AntiAlias = Java2Demo.controls.aliasCB.isSelected() ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF; Rendering = Java2Demo.controls.renderCB.isSelected() ? RenderingHints.VALUE_RENDER_QUALITY : RenderingHints.VALUE_RENDER_SPEED; if (Java2Demo.controls.textureCB.isSelected()) { if (texture instanceof GradientPaint) { texture = new GradientPaint(0, 0, Color.white, getSize().width*2, 0, Color.green); } else { texture = Java2Demo.controls.texturechooser.texture; } } else { texture = null; } composite = Java2Demo.controls.compositeCB.isSelected() ? AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f) : null; // special case if user wants to see animation with on screen g. if (Screen == 1 && runnable) { newBufferedImage = true; } } public BufferedImage createDemoBufferedImage(int w, int h, int imageType) { BufferedImage bi = null; if (imageType == 0) { bi = (BufferedImage) createImage(w, h); } else { bi = new BufferedImage(w, h, imageType); } this.w = bi.getWidth(); this.h = bi.getHeight(); return bi; } public Graphics2D createDemoGraphics2D(BufferedImage bi, Graphics g) { Graphics2D g2 = null; if (bi != null) { g2 = bi.createGraphics(); } else { g2 = (Graphics2D) g; } g2.setFont(font); g2.setBackground(getBackground()); // .. set attributes .. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias); g2.setRenderingHint(RenderingHints.KEY_RENDERING, Rendering); // .. clear canvas .. if (clearCanvas) { g2.clearRect(0, 0, w, h); } if (texture != null) { // set composite to opaque for texture fills g2.setComposite(AlphaComposite.SrcOver); if (texture instanceof TexturePaint) { g2.setPaint((TexturePaint) texture); } else { g2.setPaint((GradientPaint) texture); } g2.fillRect(0,0,w,h); } if (composite != null) { g2.setComposite(composite); } return g2; } // ... all demos that extend J2DCanvas must invoke this routine ... public abstract void drawDemo(Graphics2D g2); public void paint(Graphics g) { Dimension d = getSize(); w = d.width; h = d.height; if (!isShowing() || w <= 0 || h <= 0) { return; } if (Screen == 1) { offImg = null; } else if (offImg == null || offImg.getWidth() != w || offImg.getHeight() != h) { offImg = createDemoBufferedImage(w, h, Screen-2); newBufferedImage = true; } Graphics2D g2 = createDemoGraphics2D(offImg, g); drawDemo(g2); g2.dispose(); if (offImg != null && isShowing()) { g.drawImage(offImg, 0, 0, this); toolkit.sync(); } if (Java2Demo.performancemonitor != null && Java2Demo.performancemonitor.isShowing()) { LogPerformance(); } newBufferedImage = false; } public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; } g.translate((int) pf.getImageableX(), (int) pf.getImageableY()); BufferedImage bi = null; if (Screen > 1) { bi = createDemoBufferedImage(w, h, Screen-2); } Graphics2D g2 = createDemoGraphics2D(bi, g); drawDemo(g2); g2.dispose(); if (bi != null) { g.drawImage(offImg, 0, 0, this); } return Printable.PAGE_EXISTS; } public void start() { if (thread != null) { return; } thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName(className + "Demo"); thread.start(); } public synchronized void stop() { thread = null; notifyAll(); } public void run() { Thread me = Thread.currentThread(); while (thread == me && !isShowing() || getSize().width == 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { return; } } while (thread == me && isShowing()) { Graphics g = getGraphics(); paint(g); g.dispose(); try { Thread.sleep(sleepAmount); } catch (InterruptedException e) { return; } } thread = null; } private static final int REPORTFRAMES = 30; private void LogPerformance() { if ((frame % REPORTFRAMES) == 0) { long end = System.currentTimeMillis(); long rel = (end - start); long tot = (end - orig); if (frame == 0) { perfStr = className + " " + rel+" ms"; } else { String s1 = Float.toString((REPORTFRAMES/(rel/1000.0f))); s1 = (s1.length() < 4) ? s1.substring(0,s1.length()) : s1.substring(0,4); perfStr = className + " " + s1 + " fps"; } start = end; } ++frame; } // System.out graphics state information. public void verbose() { String str = className + " "; if (runnable || getClass().getInterfaces().length != 0) { if (thread != null || isRunning) { str = str.concat("Running "); } else { str = str.concat("Stopped "); } } str = str.concat((String) Java2Demo.controls.screenCombo.getItemAt(Screen)); str = AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON ? str.concat(" ANTIALIAS_ON ") : str.concat(" ANTIALIAS_OFF "); str = Rendering == RenderingHints.VALUE_RENDER_QUALITY ? str.concat("RENDER_QUALITY ") : str.concat("RENDER_SPEED "); if (texture != null) { str = str.concat("Texture "); } if (composite != null && composite.getAlpha() != 1.0) { str = str.concat("Composite=" + composite.getAlpha() + " "); } Runtime r = Runtime.getRuntime(); r.gc(); float freeMemory = (float) r.freeMemory(); float totalMemory = (float) r.totalMemory(); str = str.concat(((totalMemory - freeMemory)/1024) + "K used"); System.out.println(str); } }