copying a graphics object

T

tiewknvc9

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
 
M

Monique Y. Mudama

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

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.
 
T

tiewknvc9

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...
 
M

Monique Y. Mudama

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...

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.
 
T

tiewknvc9

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... :(
 
O

Oliver Wong

tiewknvc9 said:
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
 
M

Monique Y. Mudama

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... :(

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.
 
R

Raymond DeCampo

tiewknvc9 said:
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

It sounds liek you are trying to implement double buffering. Try
looking here:

<http://www.google.com/search?q="double+buffering"+Java>

HTH,
Ray
 
T

Thomas Weidenfeller

tiewknvc9 said:
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
 
R

Roedy Green

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.
 
O

Oliver Wong

Roedy Green said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top