Java Forum / General / February 2006
copying a graphics object
tiewknvc9 - 02 Feb 2006 20:21 GMT Hi thanks for reading.
I have a component that I would like to draw to, the only thing is that I would like to draw a graphics2d object onto the component. The object has already been created in a previous function, and when I set the paintComponent's graphics to it, it does nothing...
it just looks blank.
the component contains this code in its paintComponent function...
public void paintComponent(Graphics g) { super.paintComponent(g);
Graphics2D x = (Graphics2D)g; x = m_g2dPreviouslyMadeGraphic; } }
let me know if you have any ideas... Thanks
Monique Y. Mudama - 02 Feb 2006 20:52 GMT > Hi thanks for reading. > [quoted text clipped - 17 lines] > > let me know if you have any ideas... Thanks This looks awfully similar to another recently-posted question.
You assigned an object to a variable. What do you expect it to do at this point?
You could do something like,
g.drawRect (0, 0, 4, 5);
And it would use the color, clipping, etc attributes of g.
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
tiewknvc9 - 02 Feb 2006 21:00 GMT the variable is another graphics2d object that was previously created, so Im trying to paint the old object onto the new one...
or copy it to the new one so that it can be displayed in the scrollpane...
Im stuck, sorry for posting similar questions...
Monique Y. Mudama - 02 Feb 2006 21:12 GMT > the variable is another graphics2d object that was previously > created, so Im trying to paint the old object onto the new one... [quoted text clipped - 3 lines] > > Im stuck, sorry for posting similar questions... I am honestly not sure whether you are using Graphics2D in a way that I simply haven't learned before, or if you don't understand what Graphics2D is supposed to do for you.
But I suspect it's the latter.
You draw things by calling g.drawRect(), g.drawLine(), etc. As far as I know, Graphics2D objects don't "remember" a particular drawing and let you repeat it.
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
tiewknvc9 - 02 Feb 2006 21:18 GMT What I have done, is drawn a graphics2d object in the constructor. i stored that graphics2d object (which has been strings and shapes drawn onto it with a black background).
I know that overriding paintComponent and retrieving the graphics2d object and drawing to it using drawRect and etc, will produce a drawn graphics object in the jscrollpane.
However I am trying to draw the graphics2d object that has already been created, rather than drawing it in the paintComponent. Since a lot is being drawn, it would save a good deal of computing time to be able to draw the previously made graphic2d object.
And please dont talk down to me, I find it rude... :(
Monique Y. Mudama - 02 Feb 2006 21:56 GMT > What I have done, is drawn a graphics2d object in the constructor. > i stored that graphics2d object (which has been strings and shapes [quoted text clipped - 10 lines] > > And please dont talk down to me, I find it rude... :( I wasn't trying to talk down to you. I was honestly trying to point out that, while I thought you were probably trying to make Graphics2D do something it can't do, I could be wrong.
I'm sorry that it came across rudely.
There may be a way to do what you're describing, but I would ask whether prematurely optimizing -- are you sure this will make a difference in the performance of your app? The reason I ask is that I've been amazed at how much I can draw in Java without any perceptible impact on performance.
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Oliver Wong - 02 Feb 2006 21:34 GMT > the variable is another graphics2d object that was previously created, > so Im trying to paint the old object onto the new one... > > or copy it to the new one so that it can be displayed in the > scrollpane... The Graphics2D doesn't actually store the image you drew; rather it is the object you use to do the drawing. It's like the difference between a piece of paper and a pen. The paper stores the image, the pen draws the image.
I believe Java breaks the analogy here in that Graphics2D objects are associated with specific Images (e.g. the pens only work on the paper they're associated with; you can't use a pen from a different piece of paper).
Since you've already drawn something somewhere, you could probably copy the image from that object, then pass it to your new graphics object, and tell it to draw an exactl duplicate of the original image. To determine whether or not this is possible, I'd have to see the code where you actually draw the image, and how it interacts with the code where you want to draw the image a second time.
To make sure you post enough information without posting too much information and bore your readers, try to craft your code into an SSCCE. http://mindprod.com/jgloss/sscce.html
- Oliver
Raymond DeCampo - 04 Feb 2006 21:57 GMT > Hi thanks for reading. > [quoted text clipped - 17 lines] > > let me know if you have any ideas... Thanks It sounds liek you are trying to implement double buffering. Try looking here:
<http://www.google.com/search?q=%22double+buffering%22+Java>
HTH, Ray
 Signature This signature intentionally left blank.
Thomas Weidenfeller - 06 Feb 2006 07:46 GMT > I have a component that I would like to draw to, the only thing is that > I would like to draw a graphics2d object onto the component. You have been repeatedly told that a Graphics2D object can not be drawn onto something.
/Thomas
 Signature The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 06 Feb 2006 09:27 GMT On Mon, 06 Feb 2006 08:46:55 +0100, Thomas Weidenfeller <nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone who said :
>You have been repeatedly told that a Graphics2D object can not be drawn >onto something. That's not quite true. IIRC you can get a Graphics object corresponding to an Image then paint away on it, as if it were the screen.
There is also the Robot method of capturing bits written to the screen already.
A graphics object is sort of like a blind image. You can write it, but you can't read it. Is that correct? What it in under the hood is a hardware specific representation of what you drew. I'm guessing it might be for example the REGEN buffer in a video card, a list of PostScript commands for a printer, a list of Windows intermediate printer commands for a Windows printer. So it makes sense you can't necessarily convert back to image.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Oliver Wong - 06 Feb 2006 20:24 GMT > On Mon, 06 Feb 2006 08:46:55 +0100, Thomas Weidenfeller > <nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone [quoted text clipped - 17 lines] > printer commands for a Windows printer. So it makes sense you can't > necessarily convert back to image. My understanding of the OP's request is this:
(S)he creates a Graphics2D object (or has one created). (S)he then calls the various draw() methods on that Graphics2D object (e.g. drawRect, drawLine, etc.) (S)he then wishes to use that Graphics2D like a rubber stamp, pasting the image (s)he drew once onto several images without making new sequence of calls to drawRect, drawLine, etc.
In other words, (s)he is erroneously assuming that the Graphics2D object is storing the image that it drew, when actually that image would be stored in the corresponding Image object, and not in the Graphics2D object.
- Oliver
Free MagazinesGet 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 ...
|
|
|