Intermediate Images : Image « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Advanced Graphics » ImageScreenshots 
Intermediate Images
Intermediate Images
  
 


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/*
 * IntermediateImages.java
 *
 * Created on May 2, 2007, 10:58 AM
 *
 * Copyright (c) 2007, 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:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions 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 the TimingFramework project nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 *
 @author Chet
 */
public class IntermediateImages extends JComponent {

    private static final int SCALE_X = 20;
    private static final int SMILEY_X = 200;
    private static final int DIRECT_Y = 10;
    private static final int INTERMEDIATE_Y = 260;
    private static final int SMILEY_SIZE = 100;
    private static BufferedImage picture = null;
    private BufferedImage scaledImage = null;
    private BufferedImage smileyImage = null;
    private static final double SCALE_FACTOR = .1;
    private int scaleW, scaleH;
    
    /** Creates a new instance of IntermediateImages */
    public IntermediateImages() {
        try {
            URL url = getClass().getResource("A.jpg");
            picture = ImageIO.read(url);
            scaleW = (int)(SCALE_FACTOR * picture.getWidth());
            scaleH = (int)(SCALE_FACTOR * picture.getHeight());
        catch (Exception e) {
            System.out.println("Problem reading image file: " + e);
            System.exit(0);
        }
    }
    
    /**
     * Draws both the direct and intermediate-image versions of a 
     * scaled-image, timing both variations.
     */
    private void drawScaled(Graphics g) {
        long startTime, endTime, totalTime;
        
        // Scaled image
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        startTime = System.nanoTime();
        for (int i = 0; i < 100; ++i) {
            g.drawImage(picture, SCALE_X, DIRECT_Y, scaleW, scaleH, null);
        }
        endTime = System.nanoTime();
        totalTime = (endTime - startTime1000000;
        g.setColor(Color.BLACK);
        g.drawString("Direct: " ((float)totalTime/100" ms"
                SCALE_X, DIRECT_Y + scaleH + 20);
        System.out.println("scaled: " + totalTime);
        
        // Intermediate Scaled
        // First, create the intermediate image
        if (scaledImage == null ||
            scaledImage.getWidth() != scaleW ||
            scaledImage.getHeight() != scaleH)
        {
            GraphicsConfiguration gc = getGraphicsConfiguration();
            scaledImage = gc.createCompatibleImage(scaleW, scaleH);
            Graphics gImg = scaledImage.getGraphics();
            ((Graphics2D)gImg).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            gImg.drawImage(picture, 00, scaleW, scaleH, null);
        }
        // Now, copy the intermediate image into place
        startTime = System.nanoTime();
        for (int i = 0; i < 100; ++i) {
            g.drawImage(scaledImage, SCALE_X, INTERMEDIATE_Y, null);
        }
        endTime = System.nanoTime();
        totalTime = (endTime - startTime1000000;
        g.drawString("Intermediate: " ((float)totalTime/100" ms"
                SCALE_X, INTERMEDIATE_Y + scaleH + 20);
        System.out.println("Intermediate scaled: " + totalTime);
    }
    
    private void renderSmiley(Graphics g, int x, int y) {
  Graphics2D g2d = (Graphics2D)g.create();
        
  // Yellow face
  g2d.setColor(Color.yellow);
  g2d.fillOval(x, y, SMILEY_SIZE, SMILEY_SIZE);
        
  // Black eyes
  g2d.setColor(Color.black);
  g2d.fillOval(x + 30, y + 3088);
  g2d.fillOval(x + 62, y + 3088);
        
  // Black outline
  g2d.drawOval(x, y, SMILEY_SIZE, SMILEY_SIZE);
        
  // Black smile
  g2d.setStroke(new BasicStroke(3.0f));
  g2d.drawArc(x + 20, y + 206060190160);
        
        g2d.dispose();
    }
    
    /**
     * Draws both the direct and intermediate-image versions of a 
     * smiley face, timing both variations.
     */
    private void drawSmiley(Graphics g) {
        long startTime, endTime, totalTime;

        // Draw smiley directly
        startTime = System.nanoTime();
        for (int i = 0; i < 100; ++i) {
            renderSmiley(g, SMILEY_X, DIRECT_Y);
        }
        endTime = System.nanoTime();
        totalTime = (endTime - startTime1000000;
        g.setColor(Color.BLACK);
        g.drawString("Direct: " ((float)totalTime/100" ms"
                SMILEY_X, DIRECT_Y + SMILEY_SIZE + 20);
        System.out.println("Direct: " + totalTime);
        
        // Intermediate Smiley
        // First, create the intermediate image if necessary
  if (smileyImage == null) {
    GraphicsConfiguration gc = getGraphicsConfiguration();
    smileyImage = gc.createCompatibleImage(
                  SMILEY_SIZE + 1, SMILEY_SIZE + 1, Transparency.BITMASK);
    Graphics2D gImg = (Graphics2D)smileyImage.getGraphics();
    renderSmiley(gImg, 00);
    gImg.dispose();
  }
        // Now, copy the intermediate image
        startTime = System.nanoTime();
        for (int i = 0; i < 100; ++i) {
            g.drawImage(smileyImage, SMILEY_X, INTERMEDIATE_Y, null);
        }
        endTime = System.nanoTime();
        totalTime = (endTime - startTime1000000;
        g.drawString("Intermediate: " ((float)totalTime/100" ms"
                SMILEY_X, INTERMEDIATE_Y + SMILEY_SIZE + 20);
        System.out.println("intermediate smiley: " + totalTime);
    }
    
    
    protected void paintComponent(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(00, getWidth(), getHeight());
        drawScaled(g);
        drawSmiley(g);
    }
    
    private static void createAndShowGUI() {    
        JFrame f = new JFrame("IntermediateImages");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(360540);
        f.add(new IntermediateImages());
        f.setVisible(true);
    }
    
    public static void main(String args[]) {
        Runnable doCreateAndShowGUI = new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        };
        SwingUtilities.invokeLater(doCreateAndShowGUI);
    }
}


   
    
  
Related examples in the same category
1. Create an image that does not support transparency
2. Create an image that supports transparent pixels
3. Create an image that supports arbitrary levels of transparency
4. Creating a buffered image using Component.createImage().
5. Creating a Buffered Image from an Image
6. Drawing on a Buffered Image
7. If the buffered image supports transparency
8. Converting a Buffered Image (BufferedImage) from an Image
9. Getting and Setting Pixels in a Buffered Image
10. Scaling a Buffered Image
11. Shearing a Buffered Image
12. Translating a Buffered Image
13. Rotating a Buffered Image
14. Flipping a Buffered Image
15. Flip the image horizontally
16. Flip the image vertically and horizontally, equivalent to rotating the image 180 degrees
17. 2D Image Draw
18. Transform image
19. Creating a Image Zoomer using Graphics2D
20. Gaussian Blur DemoGaussian Blur Demo
21. Image reflectionImage reflection
22. Bloom DemoBloom Demo
23. Box Blur DemoBox Blur Demo
24. Brightness Increase DemoBrightness Increase Demo
25. Blur an ImageBlur an Image
26. Blur our image: Blur means an unfocused image
27. A reflected image: effect makes an illusion as if the image was reflected in water
28. Enlarge Image With AnimationEnlarge Image With Animation
29. Image ZoomingImage Zooming
30. Simple image handling and drawing interactionSimple image handling and drawing interaction
31. Unsharp Mask DemoUnsharp Mask Demo
32. Create a grayscale image with Java 2D tools
33. A 3x3 kernel that embosses an image.
34. A 3x3 kernel that blurs an image.
35. A 3x3 kernel that sharpens an image.
36. Embossing a Buffered Image
37. Brighten the image by 30%
38. Darken the image by 10%
39. Extend RGBImageFilter to create ColorFilter class
40. Extend RGBImageFilter to create AlphaFilter class
41. Enlarging an image by pixel replication
42. Shrinking an image by skipping pixels
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.