0

I got two Graphics objects, is it possible to combine them into one?

The answer of Hovercraft Full of Eels is useful, but not what I meant. Say I have two Graphics objects, private Graphics gr1 and private Graphics gr2. Now, how should I merge them in the paintComponent(Graphics) method to draw them to my (for example JPanel)?

The question is not at all about images, but about mere Graphics objects. Sorry, the first paragraph was misleading.

Edit:
In response to Hovercraft Full of Eels:

If you draw in the paintComponent() method, I find it annoying everything is gone if you redraw your screen, and if you want to move something you have to save the coordinates and dimensions of everything, adjust those and get them someway in your Graphics object.
The question I asked myself was accely: What object is best suited or saving a Graphics object. A Graphics object, I thought. but the problem is that if you have (for example) two rectangles, and one moves to the left and one moves to the right you can't if you have 1 Graphics object.
My solution was multiple graphics objects, which can be transformed to simulate movement and then drawn to the screen.
@Hovercraft Full of Eels I think you (and most SO'ers) think this is not a good solution, looking at your answer.
But, an answer saying You're doing it all wrong, you'd better stop programming doesn't help me at all, so please give me an alternative.

11684
  • 7,356
  • 12
  • 48
  • 71
  • Combine how? Impose? Composite? – trashgod Aug 18 '12 at 12:04
  • @trashgod You shouldn't ask ME, I have honestly no idea where you are talking about. My question in the first place was if it was even possible. But if you make your comment an answer wherein you explain which options there are (with reference/tutorial links would be ideal) there is a good chance I accept it. – 11684 Aug 18 '12 at 12:21
  • 1
    Regarding your edit, this is an unusual question and begs another question from me: How did you obtain these Graphics objects? You didn't get them by calling `getGraphics()` on a GUI component, did you? If so, that would be a dangerous thing to do. – Hovercraft Full Of Eels Aug 18 '12 at 18:18
  • of course I'm not dropping it's just that the explanation will be a rather long text, so I don't want to type it with my smartphone. @HovercraftFullOfEels – 11684 Aug 19 '12 at 07:32
  • @11684: please see **Edit 2** to my answer. – Hovercraft Full Of Eels Aug 19 '12 at 12:33

2 Answers2

3

You can draw any image into a BufferedImage by getting its Graphics context, drawing in it, and then disposing of the Graphics object. For example

// get a Graphics object from BufferedImage, img1
Graphics g = img1.getGraphics();

// use that g to draw another BufferedImage starting at x, y of 0, 0
g.drawImage(img2, 0, 0, null);

// clean up resources so as not to run out.
g.dispose();

Now img1 shows old img1 with img2 overlying it. If you want the images to be transparent, you'll need to read up on alpha composite and how to use it

Edit
Regarding your edited question:

I got two Graphics objects, is it possible to combine them into one?
The answer of Hovercraft Full of Eels is useful, but not what I meant. Say I have two Graphics objects, private Graphics gr1 and private Graphics gr2. Now, how should I merge them in the paintComponent(Graphics) method to draw it to my (for example JPanel)?
The question is not at all about images, but about mere Graphics objects. Sorry, the first paragraph was misleading.

This request is somewhat confusing to me since I think of Graphics objects as pens or paintbrushes that are used to draw something to an image or the screen. So I don't think that you draw a Graphics object to anything, but rather that you use a Graphics object as a tool to draw to something else that can display or store the graphics. You are going to have to put a lot more effort into asking your question, supplying us with enough details so that we don't have to keep guessing what it is you are trying to do. Please tell us the whole story.

Edit 2
In response to your latest edit to your question:

If you draw in the paintComponent() method, I find it annoying everything is gone if you redraw your screen, and if you want to move something you have to save the coordinates and dimensions of everything, adjust those and get them someway in your Graphics object. The question I asked myself was accely: What object is best suited or saving a Graphics object. A Graphics object, I thought. but the problem is that if you have (for example) two rectangles, and one moves to the left and one moves to the right you can't if you have 1 Graphics object.

No, a better solution I think is to use a BufferedImage to save that which should persist. When ever you wanted to add an image to the background BufferedImage, you'd obtain the BufferedImage's Graphics object by calling getGraphics() on it, then you'd draw your image to it by using the Graphics object's drawImage(...) method, then you'd call dispose() on the BufferedImage's Graphics object so as not to lose resources.

To draw the background BufferedImage in the background, you'd call it at the top of the paintComponent(...) method, usually just after calling the super.paintComponent(...) method.

My solution was multiple graphics objects, which can be transformed to simulate movement and then drawn to the screen. @Hovercraft Full of Eels I think you (and most SO'ers) think this is not a good solution, looking at your answer.

Yep, you guess correctly -- I believe that there are better solutions than the one you suggest.

An example of just what I'm getting at can be found in my code post in my answer to this question: changing-jpanel-graphics-g-color-drawing-line.

If you run my code, you'd see this:

enter image description here

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1 This example composes using the default `AlphaComposite.SRC_OVER` rule. – trashgod Aug 18 '12 at 12:47
  • Yes, but this is meant for working in the paintComponent method, to save my drawings for example, but say I have two drawings in two separate Graphics objects, how do I merge them? – 11684 Aug 18 '12 at 16:47
  • how should I draw the BufferedImage in the paintComponent() method? – 11684 Aug 20 '12 at 20:22
  • @11684: please see the code in the edit to my answer that I [linked to above](http://stackoverflow.com/a/6105437/522444). Note that you don't want to draw to the BufferedImage in the `paintComponent(...)` method, but instead you do that in other methods (MouseListener perhaps, Timer perhaps). In `paintComponent(...)` you draw the BufferedImage using `g.drawImage(...)` as well as drawing any transient sprites as necessary. – Hovercraft Full Of Eels Aug 20 '12 at 21:03
  • didn't know which answer to accept, both answered the question, but on different ways. Accepted one and upvoted the other. – 11684 Aug 21 '12 at 20:13
2
  • To impose images, use an appropriate layout, as shown here.

  • To compose images, use an appropriate composite rule, as shown here.

Addendum: @HFOE is correct. An instance of Graphics or Graphics2D, sometimes called a graphics context, is a transitory means to apply rendering methods; the target device or off-screen image must exist independently. Classes implementing the Shape interface are good candidates for encapsulating information about what to render in a given context. AffineTest and GraphPanel are examples.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045