Solved

Buffered Image with Glass pane

Posted on 2000-03-14
2
366 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
2 Comments
 
LVL 16

Expert Comment

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

Accepted Solution

by:
Jim Cakalic earned 50 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 47
count7 challenge 12 66
array6 challenfge 6 62
topping2 challenge 13 55
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now