Solved

Buffered Image with Glass pane

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 77
jsp login check 12 43
JAVA API design with micro service cloud in mind 1 47
servlet and mdb, jms error 1 38
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

828 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