Surface.java :  » Swing » java2d » Java-2D-Examples » Java Open Source

Java Open Source
1. Apache Common
2. Aspect Oriented
3. Barcode
4. Byte Code
5. Cache
6. Chart
7. Collections Libraries
8. Content Management Systems CMS
9. Crawlers
10. CSV File
11. Database Connection Pools
12. Database Engines
13. Database JDBC Drivers
14. Database Persistence Frameworks
15. Development
16. Development Build Systems
17. Development Code Analyzers
18. Development Code Beautifiers Formatters
19. Development Code Coverage Tools
20. Development IDEs
21. Development Java Source Obfuscators
22. Development JavaDoc
23. Development Text Editor
24. Email Clients
25. Enterprise Service Bus
26. Game Graphic
27. HTML Parsers
28. Installers
29. Inversion of Control Container
30. Issue Tracking
31. J2EE EJB Servers
32. J2EE Frameworks
33. JMS
34. JMX Tools
35. Job Schedulers
36. Logging Tools
37. Network Clients
38. Network Clients Chat
39. Network Servers
40. Open Source System Bloggers
41. Open Source System Financial ERP CRM
42. Open Source System Forum
43. Open Source System Groupware
44. Parser Generators
45. PDF Libraries
46. Portals
47. Profilers
48. Project Management
49. Report
50. RSS RDF
51. Rule Engines
52. Science Library
53. Scripting Languages
54. Search Engines
55. Security
56. Source Control Tools
57. SQL Clients Interface
58. Swing
59. Template Engines
60. Testing Tools
61. UML Modeling
62. Web Development JSP Tag Libraries
63. Web Frameword Ajax
64. Web Frameworks
65. Web Mail Clients
66. Web Servers
67. Web Services Tools
68. Web Testing Tools
69. Wiki Engines
70. Windows Parm
71. Workflow Engines
72. XML
73. XML UI Toolkits
74. Zip Tar
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Open Source » Swing » java2d 
Surface.java :  » Swing » java2d » Java-2D-Examples » Java Open Source
/*
 * @(#)Surface.java  1.55 06/08/09
 
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/*
 * @(#)Surface.java  1.55 06/08/09
 */

package java2d;

import java.awt.*;
import java.awt.image.*;
import java.awt.print.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.RepaintManager;

import static java.awt.RenderingHints.*;


/**
 * Surface is the base class for the 2d rendering demos.  Demos must
 * implement the render() method. Subclasses for Surface are 
 * AnimatingSurface, ControlsSurface and AnimatingControlsSurface.
 */
public abstract class Surface extends JPanel implements Printable {


    public Object AntiAlias = VALUE_ANTIALIAS_ON;
    public Object Rendering = VALUE_RENDER_SPEED;
    public AlphaComposite composite;
    public Paint texture;
    public String perfStr;            // PerformanceMonitor
    public BufferedImage bimg;
    public int imageType;
    public String name;         
    public boolean clearSurface = true;
    // Demos using animated gif's that implement ImageObserver set dontThread.
    public boolean dontThread;       
    public AnimatingSurface animating;

    protected long sleepAmount = 50;

    private long orig, start, frame;
    private Toolkit toolkit;
    private boolean perfMonitor, outputPerf;
    private int biw, bih;
    private boolean clearOnce;
    private boolean toBeInitialized = true;


    public Surface() {
        setDoubleBuffered(this instanceof AnimatingSurface);
        toolkit = getToolkit();
        name = this.getClass().getSimpleName();
        setImageType(0);

        // To launch an individual demo with the performance str output  :
        //    java -Djava2demo.perf= -cp Java2Demo.jar demos.Clipping.ClipAnim
        try {
            if (System.getProperty("java2demo.perf"!= null) {
                perfMonitor = outputPerf = true;
            }
        catch (Exception ex) { }
        if (this instanceof AnimatingSurface) {
            animating = (AnimatingSurfacethis;
        }
    }


    protected Image getImage(String name) {
        return DemoImages.getImage(name, this);
    }


    protected Font getFont(String name) {
        return DemoFonts.getFont(name);
    }


    public int getImageType() {
        return imageType;
    }


    public void setImageType(int imgType) {
        if (imgType == 0) {
            imageType = 1;
        else {
            imageType = imgType;
        }
        bimg = null;
    }


    public void setAntiAlias(boolean aa) {
        AntiAlias = aa ? VALUE_ANTIALIAS_ON : VALUE_ANTIALIAS_OFF;
    }


    public void setRendering(boolean rd) {
        Rendering = rd ? VALUE_RENDER_QUALITY : VALUE_RENDER_SPEED;
    }


    public void setTexture(Object obj) {
        if (obj instanceof GradientPaint) {
            texture = new GradientPaint(00, Color.white,
                                        getSize().width*20, Color.green);
        else {
            texture = (Paintobj;
        }
    }


    public void setComposite(boolean cp) {
        composite = cp 
            ? AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f
            null;
    }


    public void setMonitor(boolean pm) {
        perfMonitor = pm;
    }


    public void setSleepAmount(long amount) {
        sleepAmount = amount;
    }


    public long getSleepAmount() {
        return sleepAmount;
    }


    public BufferedImage createBufferedImage(Graphics2D g2,
                                             int w,
                                             int h,
                                             int imgType) {
        BufferedImage bi = null;
        if (imgType == 0) {
            bi = (BufferedImageg2.getDeviceConfiguration().
                                    createCompatibleImage(w, h);  
        else if (imgType > && imgType < 14) {
            bi = new BufferedImage(w, h, imgType);
        else if (imgType == 14) {
            bi = createBinaryImage(w, h, 2);
        else if (imgType == 15) {
            bi = createBinaryImage(w, h, 4);
        else if (imgType == 16) {
            bi = createSGISurface(w, h, 32);
        else if (imgType == 17) {
            bi = createSGISurface(w, h, 16);
        }
        return bi;
    }


    // Lookup tables for BYTE_BINARY 1, 2 and 4 bits.
    static byte[] lut1Arr = new byte[] {0(byte)255 };
    static byte[] lut2Arr = new byte[] {0(byte)85(byte)170(byte)255};
    static byte[] lut4Arr = new byte[] {0(byte)17(byte)34(byte)51,
                                  (byte)68(byte)85,(byte102(byte)119,
                                  (byte)136(byte)153(byte)170(byte)187,
                                  (byte)204(byte)221(byte)238(byte)255};


    private BufferedImage createBinaryImage(int w, int h, int pixelBits) {
       int bytesPerRow = w * pixelBits / 8;
       if (w * pixelBits % != 0) {
           bytesPerRow++;
       }
       byte[] imageData = new byte[h * bytesPerRow];
       IndexColorModel cm = null;
       switch (pixelBits) {
       case 1:
           cm = new IndexColorModel(pixelBits, lut1Arr.length,
                                    lut1Arr, lut1Arr, lut1Arr);
           break;
       case 2:
           cm = new IndexColorModel(pixelBits, lut2Arr.length,
                                    lut2Arr, lut2Arr, lut2Arr);
           break;
       case 4:
           cm = new IndexColorModel(pixelBits, lut4Arr.length,
                                    lut4Arr, lut4Arr, lut4Arr);
           break;
       default:
           {new Exception("Invalid # of bit per pixel").printStackTrace();}
       }
       
       DataBuffer db = new DataBufferByte(imageData, imageData.length);
       WritableRaster r = Raster.createPackedRaster(db, w, h, pixelBits, null);
       return new BufferedImage(cm, r, false, null);
    }

    private BufferedImage createSGISurface(int w, int h, int pixelBits) {
       int rMask32 = 0xFF000000;
       int rMask16 = 0xF800;
       int gMask32 = 0x00FF0000;
       int gMask16 = 0x07C0;
       int bMask32 = 0x0000FF00;
       int bMask16 = 0x003E;

       DirectColorModel dcm = null;
       DataBuffer db = null;
       WritableRaster wr = null;
       switch (pixelBits) {
       case 16:
     short[] imageDataUShort = new short[w * h];
     dcm = new DirectColorModel(16, rMask16, gMask16, bMask16);
     db = new DataBufferUShort(imageDataUShort, imageDataUShort.length);
     wr = Raster.createPackedRaster(db, w, h, w, 
            new int[] {rMask16, gMask16, bMask16},
            null);
     break;
       case 32:
     int[] imageDataInt = new int[w * h];
     dcm = new DirectColorModel(32, rMask32, gMask32, bMask32);
     db = new DataBufferInt(imageDataInt, imageDataInt.length);
     wr = Raster.createPackedRaster(db, w, h, w, 
            new int[] {rMask32, gMask32, bMask32},
            null);
     break;
       default:
     {new Exception("Invalid # of bit per pixel").printStackTrace();}
       }
       
       return new BufferedImage(dcm, wr, false, null);
    }

    public Graphics2D createGraphics2D(int width, 
                                       int height, 
                                       BufferedImage bi, 
                                       Graphics g) {

        Graphics2D g2 = null;

        if (bi != null) {
            g2 = bi.createGraphics();
        else {
            g2 = (Graphics2Dg;
        }

        g2.setBackground(getBackground());
        g2.setRenderingHint(KEY_ANTIALIASING, AntiAlias);
        g2.setRenderingHint(KEY_RENDERING, Rendering);

        if (clearSurface || clearOnce) {
            g2.clearRect(00, width, height);
            clearOnce = false;
        }

        if (texture != null) {
            // set composite to opaque for texture fills
            g2.setComposite(AlphaComposite.SrcOver);
            g2.setPaint(texture);
            g2.fillRect(00, width, height);
        }

        if (composite != null) {
            g2.setComposite(composite);
        }

        return g2;
    }

    // ...demos that extend Surface must implement this routine...
    public abstract void render(int w, int h, Graphics2D g2);


    /**
     * It's possible to turn off double-buffering for just the repaint 
     * calls invoked directly on the non double buffered component.  
     * This can be done by overriding paintImmediately() (which is called 
     * as a result of repaint) and getting the current RepaintManager and 
     * turning off double buffering in the RepaintManager before calling 
     * super.paintImmediately(g).
     */
    public void paintImmediately(int x,int y,int w, int h) {
        RepaintManager repaintManager = null;
        boolean save = true;
        if (!isDoubleBuffered()) {
             repaintManager = RepaintManager.currentManager(this);
             save = repaintManager.isDoubleBufferingEnabled();
             repaintManager.setDoubleBufferingEnabled(false);
        }
        super.paintImmediately(x, y, w, h);

        if (repaintManager != null) {
            repaintManager.setDoubleBufferingEnabled(save);
        }
    }


    public void paint(Graphics g) {

        super.paint(g);

        Dimension d = getSize();

        if(biw != d.width || bih != d.height) {
            toBeInitialized = true;
            biw = d.width;
            bih = d.height;
        }

        if (imageType == 1)
            bimg = null;
        else if(bimg == null || toBeInitialized) {
            bimg = createBufferedImage((Graphics2D)g,
                                       d.width, d.height, imageType-2);
            clearOnce = true;
        }

        if (toBeInitialized) {
            if (animating != null)
              animating.reset(d.width, d.height);
            toBeInitialized = false;
            startClock();
        }

        if (animating != null && animating.thread != null) {
            animating.step(d.width, d.height);
        }
        Graphics2D g2 = createGraphics2D(d.width, d.height, bimg, g);
        render(d.width, d.height, g2);
        g2.dispose();

        if (bimg != null)  {
            g.drawImage(bimg, 00null);
            toolkit.sync();
        }

        if (perfMonitor) {
            LogPerformance();
        }
    }


    public int print(Graphics g, PageFormat pf, int pithrows PrinterException {
        if (pi >= 1) {
            return Printable.NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2Dg;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g2d.translate(pf.getImageableWidth() 2,
                      pf.getImageableHeight() 2);
        
        Dimension d = getSize();

        double scale = Math.min(pf.getImageableWidth() / d.width,
                                pf.getImageableHeight() / d.height);
        if (scale < 1.0) {
            g2d.scale(scale, scale);
        }

        g2d.translate(-d.width / 2.0, -d.height / 2.0);

        if (bimg == null) {
            Graphics2D g2 = createGraphics2D(d.width, d.height, null, g2d);
            render(d.width, d.height, g2);
            g2.dispose();
        else {
            g2d.drawImage(bimg, 00this);
        }

        return Printable.PAGE_EXISTS;
    }


    public void startClock() {
        orig = System.currentTimeMillis();
        start = orig;
  frame = 0;
    }

    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 = name + " " + rel+" ms";
                if (animating == null || animating.thread == null) {
                    frame = -1;     
                }
            else {
                String s1 = Float.toString((REPORTFRAMES/(rel/1000.0f)));
                s1 = (s1.length() 4? s1.substring(0,s1.length()) : s1.substring(0,4);
                perfStr = name + " " + s1 + " fps";
            }
            if (outputPerf) {
                System.out.println(perfStr);
            }
            start = end;
        }
        ++frame;
    }



    // System.out graphics state information.
    public void verbose() {
        String str = "  " + name + " ";
        if (animating != null && animating.thread != null) {
            str = str.concat(" Running");
        else if (this instanceof AnimatingSurface) {
            str = str.concat(" Stopped");
        }

        str = str.concat(" " + GlobalControls.screenCombo.getSelectedItem());

        str.concat((AntiAlias == VALUE_ANTIALIAS_ON  " ANTIALIAS_ON "
                                                       " ANTIALIAS_OFF ");
        str.concat((Rendering == VALUE_RENDER_QUALITY"RENDER_QUALITY "
                                                       "RENDER_SPEED ");

        if (texture != null) {
            str = str.concat("Texture ");
        }

        if (composite != null) {
            str = str.concat("Composite=" + composite.getAlpha() " ");
        }

        Runtime r = Runtime.getRuntime();
        r.gc();
        float  freeMemory = (floatr.freeMemory();
        float totalMemory = (floatr.totalMemory();
        str = str.concat(((totalMemory - freeMemory)/1024"K used");
        System.out.println(str);
    }


    public static void createDemoFrame(Surface surface) {
        final DemoPanel dp = new DemoPanel(surface);
        Frame f = new Frame("Java2D Demo - " + surface.name);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing    (WindowEvent e) { System.exit(0)}
            public void windowDeiconified(WindowEvent e) { dp.start()}
            public void windowIconified  (WindowEvent e) { dp.stop()}
        });
        f.add("Center", dp);
        f.pack();
        f.setSize(new Dimension(500,300));
        f.setVisible(true);
        if (surface.animating != null) {
            surface.animating.start();
        }
    }
}
 Java-2D-Examples
 Mix
Name Size Time
 demos  2007-11-23
AnimatingControlsSurface.java4 k   2007-08-08
AnimatingSurface.java4 k   2007-08-08
CloningFeature.java7 k   2007-08-08
ControlsSurface.java3 k   2007-08-08
CustomControls.java5 k   2007-08-08
CustomControlsContext.java3 k   2007-08-08
DemoFonts.java4 k   2007-08-28
DemoGroup.java14 k   2007-08-08
DemoImages.java5 k   2007-08-08
DemoPanel.java5 k   2007-08-28
GlobalControls.java6 k   2007-08-08
GlobalPanel.java5 k   2007-08-08
Intro.java63 k   2007-08-08
Java2Demo.java22 k   2007-08-28
Java2DemoApplet.java8 k   2007-08-08
MemoryMonitor.java12 k   2007-08-08
PerformanceMonitor.java7 k   2007-08-08
RunWindow.java13 k   2007-08-08
Surface.java16 k   2007-08-08
TextureChooser.java8 k   2007-08-08
Tools.java16 k   2007-08-08
w___w_w__.__ja__v__a_2___s___._c___o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.