Image Buffering : Image « 2D Graphics GUI « 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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
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
Ruby
PHP
Python
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
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » ImageScreenshots 
Image Buffering
Image Buffering

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class SimpleBufferedImageDemo extends JFrame {
  DisplayCanvas canvas;

  JRadioButton buffButton, nonBuffButton;

  JButton displayButton, clearButton;

  public SimpleBufferedImageDemo() {
    super();
    Container container = getContentPane();

    canvas = new DisplayCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(22));
    panel.setBorder(new TitledBorder(
        "Select an Option and Display Image..."));

    buffButton = new JRadioButton("Buffered");
    buffButton.addActionListener(new ButtonListener());
    nonBuffButton = new JRadioButton("Non-Buffered"true);
    nonBuffButton.addActionListener(new ButtonListener());
    ButtonGroup group = new ButtonGroup();
    group.add(buffButton);
    group.add(nonBuffButton);

    displayButton = new JButton("Display");
    displayButton.addActionListener(new ButtonListener());
    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ButtonListener());

    panel.add(nonBuffButton);
    panel.add(buffButton);
    panel.add(displayButton);
    panel.add(clearButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowEventHandler());
    pack()
    setVisible(true)
  }

  class WindowEventHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }

  public static void main(String arg[]) {
    new SimpleBufferedImageDemo();
  }

  class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Object obj = e.getSource();

      if (obj instanceof JRadioButton) {
        JRadioButton button = (JRadioButtonobj;
        if (button.equals(buffButton)) {
          canvas.buffered = true;
        else if (button.equals(nonBuffButton)) {
          canvas.buffered = false;
        }
      }

      if (obj instanceof JButton) {
        JButton button = (JButtonobj;
        if (button.equals(displayButton)) {
          canvas.display = true;
          canvas.repaint();
        else if (button.equals(clearButton)) {
          canvas.clear = true;
          canvas.repaint();
        }
      }
    }
  }
}

class DisplayCanvas extends Canvas {
  boolean display = false;

  boolean clear = false;

  boolean buffered = false;

  Image displayImage; 
  DisplayCanvas() {
    displayImage = Toolkit.getDefaultToolkit().getImage("largeJava2sLogo.jpg");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);
    try {
      mt.waitForAll();
    catch (Exception e) {
      System.out.println("Exception while loading.");
    }

    if (displayImage.getWidth(this== -1) {
      System.out.println("No *.jpg file");
      System.exit(0);
    }
    setBackground(Color.white);
    setSize(400225);
  }

  public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2Dg;

    if (display) {
      if (buffered) {
        BufferedImage bi = (BufferedImagecreateImage(getWidth(),
            getHeight());

        // Draw into the memory buffer.
        for (int i = 0; i < getWidth(); i = i
            + displayImage.getWidth(this)) {
          for (int j = 0; j < getHeight(); j = j
              + displayImage.getHeight(this)) {
            bi.createGraphics().drawImage(displayImage, i, j, this);
          }
        }
        // Draw the buffered Image on to the screen
        g2D.drawImage(bi, 00this);
      }
      // This block of code draws the texture directly onto the screen.
      else if (!buffered) {
        for (int i = 0; i < getWidth(); i = i
            + displayImage.getWidth(this)) {
          for (int j = 0; j < getHeight(); j = j
              + displayImage.getHeight(this)) {
            g2D.drawImage(displayImage, i, j, this);
          }
        }
      }
      display = false;
    }else if (clear) {
      g2D.setColor(Color.white);
      g2D.clearRect(00, getWidth(), getHeight());
      clear = false;
    }
  }
}

           
       
Related examples in the same category
1. Image size Image size
2. Image demoImage demo
3. Paint an IconPaint an Icon
4. Image Processing: Brightness and ContrastImage Processing: Brightness and Contrast
5. Image with mouse drag and move eventImage with mouse drag and move event
6. Image Animation and ThreadImage Animation and Thread
7. Image Color Gray EffectImage Color Gray Effect
8. Image Effect: CombineImage Effect: Combine
9. AffineTransform demoAffineTransform demo
10. Image Effect: Rotate Image using DataBufferImage Effect: Rotate Image using DataBuffer
11. Image Effect: Sharpen, blurImage Effect: Sharpen, blur
12. Image scale
13. Image crop
14. Demonstrating the Drawing of an Image with a Convolve Operation
15. Demonstrating Use of the Image I/O Library
16. Adding Image-Dragging Behavior
17. Sending Image Objects through the ClipboardSending Image Objects through the Clipboard
18. Anti AliasAnti Alias
19. Image OperationsImage Operations
20. Image ViewerImage Viewer
21. Standalone Image Viewer - works with any AWT-supported format
22. Toolkit.getImage() which works the same in either Applet or Application
23. Image Processing Test Image Processing Test
24. Image Transfer TestImage Transfer Test
25. Double Buffered Image
26. Graband Fade: displays image and fades to black
27. Graband Fade with Rasters
28. Rotate Image 45 Degrees
29. Convert java.awt.image.BufferedImage to java.awt.Image
30. Filter image by multiplier its red, green and blue color
31. Drags within the imageDrags within the image
w__w_w__._jav__a2___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.