Solved

Buffered Image with Glass pane

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 160
Chrome and Firefox Java 5 88
Why is enum singleton a better approach than static factory 3 66
Html Table Looping (part 2) 5 41
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 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