Solved

Buffered Image with Glass pane

Posted on 2000-03-14
2
378 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_
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 191
JList custom Cell Renderer refresh 15 56
couple of eclipse 5 35
reverse digits of a number using for loop 5 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

816 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

10 Experts available now in Live!

Get 1:1 Help Now