• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Graphics object to image

If I have an image and call .getGraphics() in order to retrieve a graphics context to draw to, how can I draw another graphics on top of it?

For instance, I have an image and I would like to superimpose an image of a component on top of it.  I don't see any methods in the api for this direct effect, but I'm sure there must be a way.

Thanks,
David
0
lomidien
Asked:
lomidien
  • 5
  • 4
  • 3
1 Solution
 
CEHJCommented:
You can simply draw over it
0
 
CEHJCommented:
i.e.

1.  "I have an image and call .getGraphics()"
2.  "... would like to superimpose an image of a component on top of it" - so call drawImage on the Graphics obtained above
0
 
CEHJCommented:
If you haven't got an image of the component, get *its* Graphics g2 and then call

g1.paint(g2);
0
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!

 
lomidienAuthor Commented:

//unfilteredScreenImage is a BufferedImage
Graphics g1 = unfilteredScreenImage.getGraphics();
           
//gp is a glasspane for a frame which contains some painted graphics
Graphics g2 = gp.getGraphics();


I get an error calling: g1.paint(g2) because it cannot find a method with a graphics object as an argument

Am I overlooking something?

Thanks,
David
0
 
InteractiveMindCommented:
That's because Graphics#paint() doesn't exist....  :o\
0
 
lomidienAuthor Commented:
But then how to accomplish the effect?
0
 
InteractiveMindCommented:
Perhaps this is stupid enough to work:


    Graphics g1 = unfilteredScreenImage.getGraphics();
    Graphics g2 = gp.getGraphics();
   
    g1.drawImage( g2, 0, 0, this );


?
0
 
InteractiveMindCommented:
Sorry, try this instead:

    Graphics g2 = gp.getGraphics();
    g1.drawImage( unfilteredScreenImage, 0, 0, this );

0
 
InteractiveMindCommented:
Dammit... still wrong. lol.. Before I go any further, can you tell me what class 'gp' instantiates please?
0
 
lomidienAuthor Commented:
Sorry IM, didn't hit refresh to see those responses and in the meantime I solved it via CEHJ's suggestion.

gp is a custom glassPane.

I fixed it by passing the g1 to the paint method of gp.

i.e. gp.paint(g1);

and that did the trick.

Thanks for all the help guys,
David
0
 
InteractiveMindCommented:
Good man.  :-)
0
 
CEHJCommented:
:-) You caught my slip ;-)
0

Featured Post

Industry Leaders: 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!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now