Combining several images in Java - java

I have these several images which I need to be combined using BufferedImage in java. The logic of the program is this:
The user will be asked to input data (5 times) then the inputted value will be generated to image (I'm done coding with this part). My problem is how can I Combine them into one image only. Thanks!

You can create another new image. Depending on how you would like to combine the images you have two possibilities:
Call getGraphics() on the newly create BufferedImage and use drawImage() multiple times to draw your other images into the newly created one (for example to create a tiled image with all the images you create beforehand)
Call getRaster() on the newly create BufferedImage and use the methods of this object to draw information from your other images into this image (this way you can achieve any blend effects you might need)

Related

Java- Draw only in certain areas

I am trying to draw circles (representing people) on a PNG map of Earth. Obviously, I don't want people to be floating in the oceans as the simulated civilization expands. How would I create specified areas on the map that I can draw on? Can I simply restrict the background of the PNG?
I tried using the clip() method, but it still allowed spawning within the oceans. Any suggestions (or if you recommend clip(), how would you use it in this case) would be appreciated.
You somehow have to tell your program where drawing is allowed and where not. There are a bunch of approaches to this:
Try to define a set of features on your PNG image and only spawn (or don't spawn) civilizations if these features are met.
Make a "map" out of your map. Define areas on your PNG image where spawning is allowed and where spawning is not allowed. You can use this using the Polygon class for example.
Make a second PNG image in which you make all oceans purely transparent. Then, only spawn civilizations on the original image at locations where the image with transparency is non-transparent.
Just a bunch of ideas.

Get a BufferedImage output composed of several image layers

for my current work, I need to add images on top of other images at runtime. I already searched and I don't want to draw it on a Swing component, I use it in an external API.
Ideally, I would like a library (or a java-native way) with which I can specify several images to "layer", and it would return me a BufferedImage (or any Image object by the way).
If you want a single image (BufferedImage) in return, then you need to compose your images.
This answer may help you stackoverflow.com/questions/2318020/merging-two-images.
Have a look at the documentation for method drawImage to control position and scaling of any drawing through the Graphics object.

Java: Find image within image?

So, say I have two images, one which is a .bmp of some text and another which is a bufferedImage, how would I go about finding if the .bmp is inside the bufferedImage?
Im really lost on how to find an image within an image, a color is easier as its just one thing to search for but an image seems much harder...
One Solution to this Problem is "Template Matching".
This means sliding your Template (the image you want to find) over the Image (you want to search in) and at every Position compare the similiarity of all Pixels.
The Position of your Template in the Image is at the Maximum this procedure returned.
As suggested in the comments you can use OpenCV for this Task which support Template Matching.

How do i get one image from a grid of images in java?

I need to be able to take certain images from one big image on a grid. Sort of like how in the game minecraft, there are texture packs that retexture the way the game looks. In order to keep file size down, it is not all that many actual pictures. It is a grid of all the different block texture all on one picture.
I need to do something similar to this, but using this picture: http://f.cl.ly/items/122C0G3R3P422R2I452o/fontes_blanches_alpha.png
Specifically, i want to be able to call each character from this picture from an ArrayList sort of like:
(Pseudo code)
ArrayList<Pictures> chars = new ArrayList<Pictures>();
JFrame.add(chars.get(x));
So, how would i separate the pictures to display only part of it?
You could try loading the font image into a BufferedImage object. Then you can say bufferedImage.getSubimage(x,y,w,h) to get a subImage of type BufferedImage. When you have a subImage, you can add it to your "chars" ArrayList.

Create an Image out of a larger Image

For a game I'm making I am storing all the sprites for the map inside one large(er) image. I want to be able to create an instance of Image for each image inside of the larger image that has all the sprites.
So how would I create an instance of Image from a set position of another Image.
The basic solution (if all tiles in your tilesheet have the same size) is to use the getSubimage(xpos, ypos, XSIZE, YSIZE) method from the class BufferedImage.
Otherwise, you'll have to store a set of position and size for all sprites in another file.
Your question is similar to this others one.
Since you are using the java.awt.Image-class i am guessing you are trying to create a reasonable game using AWT? You really should take a look at a different technology like http://en.wikipedia.org/wiki/Java_OpenGL. The problem you ran into (partitioning a sprite-sheet) is typical for a lot of other problems (rotation...) you will run into if you try to develop a game without something like Open-GL.
Considering only the problem at-hand: you can easily solve this in Open-GL by binding the whole sprite-sheet as texture (glBindTexture()) and giving for each glVertex() a glTexCoord2f(), no need to cut-out parts of the sprite-sheet.

Categories

Resources