home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / DemoSurface.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  11.0 KB  |  386 lines

  1. /*
  2.  * @(#)DemoSurface.java    1.31 98/09/22
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.image.*;
  18. import java.awt.print.*;
  19. import javax.swing.JPanel;
  20.  
  21.  
  22. /**
  23.  * All demos extend this DemoSurface Abstract class.  From
  24.  * this class demos must implement the drawDemo method.  This
  25.  * class handles animated demos, the demo must implement the
  26.  * AnimatingContext interface.
  27.  */
  28. public abstract class DemoSurface extends JPanel implements Runnable, Printable {
  29.  
  30.     public Object AntiAlias = RenderingHints.VALUE_ANTIALIAS_ON;
  31.     public Object Rendering = RenderingHints.VALUE_RENDER_SPEED;
  32.     public Paint texture;
  33.     public AlphaComposite composite;
  34.     public String perfStr;            // PerformanceMonitor
  35.     public BufferedImage bimg;
  36.     public int imageType;
  37.     public Thread thread;
  38.     public String name;         
  39.     public boolean observerRunning;
  40.     public boolean clearSurface = true;
  41.  
  42.     protected long sleepAmount;
  43.  
  44.     private long orig, start, frame;
  45.     private Toolkit toolkit;
  46.     private boolean perfMonitor, outputPerf;
  47.     private int biw, bih;
  48.     private AnimatingContext animating;
  49.  
  50.  
  51.     public DemoSurface() {
  52.         setDoubleBuffered(false);
  53.         setBackground(Color.white);
  54.         toolkit = getToolkit();
  55.         name = this.getClass().getName();
  56.         name = name.substring(name.indexOf(".", 7)+1);
  57.         setImageType(0);
  58.  
  59.         // To launch an individual demo with the performance str output  :
  60.         //    java -Djava2demo.perf= demos.Clipping.ClipAnim
  61.         try {
  62.             if (System.getProperty("java2demo.perf") != null) {
  63.                 perfMonitor = outputPerf = true;
  64.             }
  65.         } catch (Exception ex) { }
  66.     }
  67.  
  68.  
  69.     protected Image getImage(String fileName) {
  70.         if (DemoImages.cache == null) {
  71.             return DemoImages.createImage(fileName, this);
  72.         } else {
  73.             return (Image) DemoImages.cache.get(fileName);
  74.         }
  75.     }
  76.  
  77.  
  78.     protected boolean containsImage(String imgName) {
  79.         if (DemoImages.cache == null) {
  80.             return false;
  81.         } else {
  82.             return DemoImages.cache.containsKey(imgName);
  83.         }
  84.     }
  85.  
  86.  
  87.     protected Image getCroppedImage(Image img, int x, int y, int w, int h) {
  88.         return DemoImages.getCroppedImage(img, x, y, w, h, this);
  89.     }
  90.  
  91.  
  92.     public void setImageType(int imgType) {
  93.         if (imgType == 0) {
  94.             if (this instanceof AnimatingContext || observerRunning) {
  95.                 imageType = 2;
  96.             } else {
  97.                 imageType = 1;
  98.             }
  99.         } else {
  100.             imageType = imgType;
  101.         }
  102.         bimg = null;
  103.     }
  104.  
  105.  
  106.     public void setAntiAlias(boolean aa) {
  107.         AntiAlias = aa 
  108.             ? RenderingHints.VALUE_ANTIALIAS_ON
  109.             : RenderingHints.VALUE_ANTIALIAS_OFF;
  110.     }
  111.  
  112.  
  113.     public void setRendering(boolean rd) {
  114.         Rendering = rd
  115.             ? RenderingHints.VALUE_RENDER_QUALITY
  116.             : RenderingHints.VALUE_RENDER_SPEED;
  117.     }
  118.  
  119.  
  120.     public void setTexture(Object obj) {
  121.         if (obj instanceof GradientPaint) {
  122.             texture = new GradientPaint(0, 0, Color.white,
  123.                                         getSize().width*2, 0, Color.green);
  124.         } else {
  125.             texture = (Paint) obj;
  126.         }
  127.     }
  128.  
  129.  
  130.     public void setComposite(boolean cp) {
  131.         composite = cp 
  132.             ? AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f) 
  133.             : null;
  134.     }
  135.  
  136.  
  137.     public void setMonitor(boolean pm) {
  138.         perfMonitor = pm;
  139.     }
  140.  
  141.  
  142.     public BufferedImage createBufferedImage(int w, int h, int imgType) {
  143.         BufferedImage bi = null;
  144.         if (imgType == 0) {
  145.             bi = (BufferedImage) createImage(w, h);  
  146.         } else {
  147.             bi = new BufferedImage(w, h, imgType);
  148.         }
  149.         biw = w;
  150.         bih = h;
  151.         return bi;
  152.     }
  153.  
  154.  
  155.     public Graphics2D createGraphics2D(int width, 
  156.                                        int height, 
  157.                                        BufferedImage bi, 
  158.                                        Graphics g) {
  159.  
  160.         Graphics2D g2 = null;
  161.  
  162.         if (bi != null) {
  163.             g2 = bi.createGraphics();
  164.         } else {
  165.             g2 = (Graphics2D) g;
  166.         }
  167.  
  168.         g2.setBackground(getBackground());
  169.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  170.         g2.setRenderingHint(RenderingHints.KEY_RENDERING, Rendering);
  171.  
  172.         if (clearSurface) {
  173.             g2.clearRect(0, 0, width, height);
  174.         }
  175.  
  176.         if (texture != null) {
  177.             // set composite to opaque for texture fills
  178.             g2.setComposite(AlphaComposite.SrcOver);
  179.             g2.setPaint(texture);
  180.             g2.fillRect(0, 0, width, height);
  181.         }
  182.  
  183.         if (composite != null) {
  184.             g2.setComposite(composite);
  185.         }
  186.  
  187.         return g2;
  188.     }
  189.  
  190.     // ...demos that extend DemoSurface must implement this routine...
  191.     public abstract void drawDemo(int w, int h, Graphics2D g2);
  192.  
  193.  
  194.     public void paint(Graphics g) {
  195.  
  196.         Dimension d = getSize();
  197.  
  198.         if (imageType == 1) {
  199.             bimg = null;
  200.             startClock();
  201.         } else if (bimg == null || biw != d.width || bih != d.height) {
  202.             bimg = createBufferedImage(d.width, d.height, imageType-2);
  203.             if (this instanceof AnimatingContext) {
  204.                 ((AnimatingContext) this).reset(d.width, d.height);
  205.             }
  206.             startClock();
  207.         }
  208.  
  209.         if (thread != null) {
  210.             animating.step(d.width, d.height);
  211.         } 
  212.  
  213.         Graphics2D g2 = createGraphics2D(d.width, d.height, bimg, g);
  214.         drawDemo(d.width, d.height, g2);
  215.         g2.dispose();
  216.  
  217.         if (bimg != null)  {
  218.             g.drawImage(bimg, 0, 0, null);
  219.             toolkit.sync();
  220.         }
  221.  
  222.         if (perfMonitor) {
  223.             LogPerformance();
  224.         }
  225.     }
  226.  
  227.  
  228.     public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
  229.         if (pi >= 1) {
  230.             return Printable.NO_SUCH_PAGE;
  231.         }
  232.  
  233.         Graphics2D g2d = (Graphics2D) g;
  234.         g2d.translate(pf.getImageableX(), pf.getImageableY());
  235.         g2d.translate(pf.getImageableWidth() / 2,
  236.                       pf.getImageableHeight() / 2);
  237.         
  238.         Dimension d = getSize();
  239.  
  240.         double scale = Math.min(pf.getImageableWidth() / d.width,
  241.                                 pf.getImageableHeight() / d.height);
  242.         if (scale < 1.0) {
  243.             g2d.scale(scale, scale);
  244.         }
  245.  
  246.         g2d.translate(-d.width / 2.0, -d.height / 2.0);
  247.  
  248.         if (bimg == null) {
  249.             Graphics2D g2 = createGraphics2D(d.width, d.height, null, g2d);
  250.             drawDemo(d.width, d.height, g2);
  251.             g2.dispose();
  252.         } else {
  253.             g2d.drawImage(bimg, 0, 0, this);
  254.         }
  255.  
  256.         return Printable.PAGE_EXISTS;
  257.     }
  258.  
  259.  
  260.     public void start() {
  261.         if (thread != null || !(this instanceof AnimatingContext)) {
  262.             return;
  263.         }
  264.         animating = (AnimatingContext) this;
  265.         thread = new Thread(this);
  266.         thread.setPriority(Thread.MIN_PRIORITY);
  267.         thread.setName(name + "Demo");
  268.         thread.start();
  269.     }
  270.  
  271.  
  272.     public synchronized void stop() {
  273.         if (thread != null) {
  274.             thread.interrupt();
  275.         }
  276.         thread = null;
  277.         notifyAll();
  278.     }
  279.  
  280.  
  281.     public void run() {
  282.  
  283.         Thread me = Thread.currentThread();
  284.  
  285.         while (thread == me && !isShowing() || getSize().width == 0) {
  286.             try {
  287.                 thread.sleep(200);
  288.             } catch (InterruptedException e) { }
  289.         }
  290.  
  291.         while (thread == me) {
  292.             repaint();
  293.             try {
  294.                 thread.sleep(sleepAmount);
  295.             } catch (InterruptedException e) { }
  296.         }
  297.         thread = null;
  298.     }
  299.  
  300.  
  301.     private void startClock() {
  302.         orig = System.currentTimeMillis();
  303.         start = orig;
  304.     }
  305.  
  306.     private static final int REPORTFRAMES = 30;
  307.  
  308.     private void LogPerformance() {
  309.         if ((frame % REPORTFRAMES) == 0) {
  310.             long end = System.currentTimeMillis();
  311.             long rel = (end - start);
  312.             long tot = (end - orig);
  313.             if (frame == 0) {
  314.                 perfStr = name + " " + rel+" ms";
  315.                 if (imageType == 1) { 
  316.                     frame = -1;         // reset for on-screen
  317.                 }
  318.             } else {
  319.                 String s1 = Float.toString((REPORTFRAMES/(rel/1000.0f)));
  320.                 s1 = (s1.length() < 4) ? s1.substring(0,s1.length()) : s1.substring(0,4);
  321.                 perfStr = name + " " + s1 + " fps";
  322.             }
  323.             if (outputPerf) {
  324.                 System.out.println(perfStr);
  325.             }
  326.             start = end;
  327.         }
  328.         ++frame;
  329.     }
  330.  
  331.  
  332.  
  333.     // System.out graphics state information.
  334.     public void verbose() {
  335.         String str = name + " ";
  336.         if (thread != null) {
  337.             str = str.concat(" Running");
  338.         } else if (this instanceof AnimatingContext) {
  339.             str = str.concat(" Stopped");
  340.         }
  341.  
  342.         String s = "On Screen";
  343.         if (bimg != null) {
  344.             switch (bimg.getType()) {
  345.                 case bimg.TYPE_INT_RGB : s="INT_RGB"; break;
  346.                 case bimg.TYPE_INT_ARGB : s="INT_ARGB"; break;
  347.                 case bimg.TYPE_INT_ARGB_PRE : s="TYPE_INT_ARGB_PRE"; break;
  348.                 case bimg.TYPE_INT_BGR : s="TYPE_INT_BGR"; break;
  349.                 case bimg.TYPE_3BYTE_BGR : s="TYPE_3BYTE_BGR"; break;
  350.                 case bimg.TYPE_4BYTE_ABGR : s="TYPE_4BYTE_ABGR"; break;
  351.                 case bimg.TYPE_4BYTE_ABGR_PRE : s="TYPE_4BYTE_ABGR_PRE"; break;
  352.                 case bimg.TYPE_USHORT_565_RGB : s="TYPE_USHORT_565_RGB"; break;
  353.                 case bimg.TYPE_USHORT_555_RGB : s="USHORT_555_RGB"; break;
  354.                 case bimg.TYPE_BYTE_GRAY : s="BYTE_GRAY"; break;
  355.                 case bimg.TYPE_USHORT_GRAY : s="USHORT_GRAY"; break;
  356.                 case bimg.TYPE_BYTE_BINARY : s="BYTE_BINARY"; break;
  357.                 case bimg.TYPE_BYTE_INDEXED : s="BYTE_INDEXED"; break;
  358.             }
  359.         }
  360.         str = str.concat(" " + s);
  361.  
  362.         str = AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON
  363.             ? str.concat(" ANTIALIAS_ON ") 
  364.             : str.concat(" ANTIALIAS_OFF ");
  365.  
  366.         str = Rendering == RenderingHints.VALUE_RENDER_QUALITY
  367.             ? str.concat("RENDER_QUALITY ") 
  368.             : str.concat("RENDER_SPEED ");
  369.  
  370.         if (texture != null) {
  371.             str = str.concat("Texture ");
  372.         }
  373.  
  374.         if (composite != null) {
  375.             str = str.concat("Composite=" + composite.getAlpha() + " ");
  376.         }
  377.  
  378.         Runtime r = Runtime.getRuntime();
  379.         r.gc();
  380.         float freeMemory = (float) r.freeMemory();
  381.         float totalMemory = (float) r.totalMemory();
  382.         str = str.concat(((totalMemory - freeMemory)/1024) + "K used");
  383.         System.out.println(str);
  384.     }
  385. }
  386.