vicky
vicky

Reputation: 1046

How to add one image onto another in java

I have an image with a plain background. Now I need to place another image onto it at (x, y) location. How is it possible?

Upvotes: 4

Views: 10170

Answers (2)

npinti
npinti

Reputation: 52185

You could use the drawImage method. Maybe something like so:

try
{
    BufferedImage source = ImageIO.read(new File("..."));
    BufferedImage logo = ImageIO.read(new File("..."));

    Graphics g = source.getGraphics();
    g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
    e.printStackTrace();
}

Upvotes: 10

StanislavL
StanislavL

Reputation: 57381

Create a BufferedImage with desired size. Use getGraphics() of the image and paint the first image and then the second one. Graphics has the method

public abstract boolean drawImage(Image img, int x, int y, 
                      Color bgcolor,
                      ImageObserver observer)

Upvotes: 12

Related Questions