Solved

Buffered Image with Glass pane

Posted on 2000-03-14
2
399 Views
Last Modified: 2008-03-06
I have a frame that is using the border layout.
I've placed a buffered image in the center section of the layout. I'd like to use the Glass pane to draw temporary graphics but from what I've read, the Glass pane covers the whole frame. Is it possible to have the same functionaly as the Glass pane but for areas that are specifiable by me?

Any suggestions will be appreciated.
Thanks
0
Comment
Question by:Aixsar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2617308
what's the problem with GlassPane ? post  YOUR code.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 2617388
Aixsar,

I don't think you need to use GlassPane. If you already have a BufferedImage, you can draw directly on the BufferedImage (or a temporary copy of it) before rendering the final image.

Here is some modified source from the Java 2D Graphics programming book from O'Reilly. You can get the original examples by going to http://www.oreilly.com/catalog/java2d and following the "Examples" link on the left side of the page. In addition to the source below, you'll need ApplicationFrame.java and Raphael.jpg to get this to work.

Jim Cakalic

---------- ImageDuplicity.java ----------
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;

import com.sun.image.codec.jpeg.*;

public class ImageDuplicity extends Component implements ActionListener {
    public static void main(String[] args) {
        ApplicationFrame f = new ApplicationFrame("ImageDuplicity v1.0");
        f.setLayout(new BorderLayout());
        Component c = new ImageDuplicity();
        JButton b = new JButton("Press me");
        b.addActionListener((ActionListener)c);
        f.add(b, BorderLayout.NORTH);
        f.add(c, BorderLayout.CENTER);
        f.setSize(200, 250);
        f.center();
        f.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        createOffscreenImage(!overdrawn);
        this.repaint();
    }

    private BufferedImage mImage;
    private boolean overdrawn = false;

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        if (mImage == null) {
            // If the offscreen image is not defined, create it.
            createOffscreenImage(false);
        }
        // Render the offscreen image.
        g2.drawImage(mImage, 0, 0, this);
    }

    private void createOffscreenImage(boolean withEllipses) {
        // Create a BufferedImage the same size as this component.
        Dimension d = getSize();
        int w = d.width, h = d.height;
        mImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        // Obtain the Graphics2D for the offscreen image.
        Graphics2D g2 = mImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // Load an image from a file.
        try {
            String filename = "Raphael.jpg";
            InputStream in = getClass().getResourceAsStream(filename);
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
            BufferedImage image = decoder.decodeAsBufferedImage();
            in.close();
            // Draw the loaded image on the offscreen image.
            g2.drawImage(image, 0, 0, w, h, null);
        } catch (Exception e) {
            System.out.print(e);
        }
        if (withEllipses == true) {
            // Draw some concentric ellipses.
            g2.setStroke(new BasicStroke(2));
            Color[] colors = { Color.red, Color.blue, Color.green};
            for (int i = -32; i < 40; i += 8) {
                g2.setPaint(colors[Math.abs(i) % 3]);
                g2.drawOval(i, i, w - i * 2, h - i * 2);
            }
        }
        overdrawn = withEllipses;
    }
}
---------- end ----------
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question