Link to home
Start Free TrialLog in
Avatar of lomidien
lomidien

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can simply draw over it
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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lomidien
lomidien

ASKER


//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
That's because Graphics#paint() doesn't exist....  :o\
But then how to accomplish the effect?
Perhaps this is stupid enough to work:


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


?
Sorry, try this instead:

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

Dammit... still wrong. lol.. Before I go any further, can you tell me what class 'gp' instantiates please?
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
Good man.  :-)
:-) You caught my slip ;-)