Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2007

Tip: Looking for answers? Try searching our database.

How an I use a copy of Graphics2D ?

Thread view: 
djubre@myway.com - 02 Jan 2007 15:00 GMT
Is there a way I can use a Graphics2D instance of a JComponent, and
"display" it into another JComponent?
To clarify, I have a JPanel with a lot of staff drawn onto its
Graphics2D instance. I don't have the access to it's
paintComponents(Graphics g) method. I want to use it as a picture
(image, whatever) and display it as a content of another JPanel.
What I need is to obtain Graphics instance with panel's gtGraphics(),
and underlay it to another JPanel.
Knute Johnson - 02 Jan 2007 17:10 GMT
> Is there a way I can use a Graphics2D instance of a JComponent, and
> "display" it into another JComponent?
[quoted text clipped - 4 lines]
> What I need is to obtain Graphics instance with panel's gtGraphics(),
> and underlay it to another JPanel.

That is not how it works.  The component's Graphics is used to draw on
it.  Why don't you just put the JPanel with the drawing on it where you
want it to show up?

Signature

Knute Johnson
email s/nospam/knute/

Daniel Pitts - 02 Jan 2007 19:56 GMT
> > Is there a way I can use a Graphics2D instance of a JComponent, and
> > "display" it into another JComponent?
[quoted text clipped - 8 lines]
> it.  Why don't you just put the JPanel with the drawing on it where you
> want it to show up?

Alternatively, if you want it to appear in two or more places, render
it to a BufferedImage, and render that BufferedImage to all places
needed.
You could also use a ImageIcon and JLabel to render the image for you!

Hope this helps.
Daniel.
djubre@myway.com - 03 Jan 2007 10:15 GMT
> That is not how it works.  The component's Graphics is used to draw on
> it.  Why don't you just put the JPanel with the drawing on it where you
> want it to show up?

I need the Graphics2D instance to do some modifications. I can't change
the original renedering because it is not my code.
All I can do is obtain the Graphics2D instance with getGraphics(), and
hope I can do someting with it.
Can I make an image out of the Graphics2D instance.
I think of the Graphics2D instance as a drawing surface. Can I
transform it to an image? Can I use it somewhere else, after all the
drawings were done.
Knute Johnson - 03 Jan 2007 15:11 GMT
>> That is not how it works.  The component's Graphics is used to draw on
>> it.  Why don't you just put the JPanel with the drawing on it where you
[quoted text clipped - 8 lines]
> transform it to an image? Can I use it somewhere else, after all the
> drawings were done.

Extend the component that you want to draw on, call
super.paintComponent() from the new components paintComponent method and
then draw away.  This isn't going to work if it is some sort of active
component like a button or something.

import java.awt.*;
import javax.swing.*;

public class test {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                class OriginalThing extends JPanel {
                    public OriginalThing() {
                        setPreferredSize(new Dimension(400,300));
                    }
                    public void paintComponent(Graphics g) {
                        g.setColor(Color.BLUE);
                        g.fillRect(0,0,getWidth(),getHeight());
                    }
                }
                class ThingIWantToDrawOnToo extends OriginalThing {
                    public void paintComponent(Graphics g) {
                        super.paintComponent(g);
                        g.setColor(Color.RED);
                        g.drawString("I drew this on the original!",20,30);
                    }
                }

                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                f.add(new ThingIWantToDrawOnToo());

                f.pack();
                f.setVisible(true);
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

djubre@myway.com - 03 Jan 2007 15:45 GMT
Thanks for the try, but that's not what I need. I need the component's
Graphic AFTER the drawing is completely done, so invoking
super.paintComponents(g) is not suitable.
Fred Kleinschmidt - 03 Jan 2007 16:40 GMT
> Thanks for the try, but that's not what I need. I need the component's
> Graphic AFTER the drawing is completely done, so invoking
> super.paintComponents(g) is not suitable.

You seem to misunderstand what a Graphics is. It is NOT the
image that is drawn on the screen.

It merely contains things like background and foreground color,
clip rectangles, font, line style, line overlap style, etc., that will
be used by its other methods to render what the component's
paintComponent method uses do draw on the screen.

For example, if the component wants to draw four lines using
four deifferent colors, it will have to modify the Graphics to set
the correct color to use, then call g.drawLine(). This must be
repeated for each line. The lines themselves are NOT part of the
Graphics component. The same thing is true about all of the
text, images, etc. that may be drawn by the component.
Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Daniel Pitts - 03 Jan 2007 16:44 GMT
> Thanks for the try, but that's not what I need. I need the component's
> Graphic AFTER the drawing is completely done, so invoking
> super.paintComponents(g) is not suitable.

I don't think you understand how these things work...
Graphics is a rendering object. It does NOT represent what has already
been drawn.  You would have to do either a screen capture, or ask the
component to repaint itself where you want it.  Why do you need to wait
until AFTER the drawing is complete?
Knute Johnson - 04 Jan 2007 00:38 GMT
> Thanks for the try, but that's not what I need. I need the component's
> Graphic AFTER the drawing is completely done, so invoking
> super.paintComponents(g) is not suitable.

When super.paintComponent() returns the original drawing is completely
done.  Did you even try the example?  It does exactly what you want.

Signature

Knute Johnson
email s/nospam/knute/

Andrew Thompson - 04 Jan 2007 01:48 GMT
> > Thanks for the try, but that's not what I need.
...
> ..Did you even try the example?

Pfft.. That would require me to copy the code,
paste it somewhere, save it to file (under the
correct name - no less), compile, run it, and
look at the screen.

...Are you /insane/?

>...It does exactly what you want.

(sigh) Can't you just post the compiled binary
to me?  Why make it so *hard*?

Sorry Knute, but without even having compiled
your code, I laughed out loud when the OP came
back with his initial condolences on your 'failure'.

I'll add mine.   "There, there.  There there."   ;-)

Andrew T.
Judy Szikora - 03 Jan 2007 17:13 GMT
>>That is not how it works.  The component's Graphics is used to draw on
>>it.  Why don't you just put the JPanel with the drawing on it where you
[quoted text clipped - 8 lines]
> transform it to an image? Can I use it somewhere else, after all the
> drawings were done.

Think of the Graphics2D as a pencil or paintbrush that draws on a
particular drawing surface, not as the drawing surface itself.

Sounds to me like what you might need is to get a BufferedImage of the
appropriate size and type, get a Graphics object from it, and with that
Graphics object call your panel's paintComponent. That will give you a
copy of the panel's rendering in the BufferedImage so you can work with it.

Signature

Judy Szikora, Apprisant Technologies Inc.
http://www.apprisant.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.