I have a TableItem and i wish to add an image in it.
I use this code :
Image image = new Image(Display.getCurrent(), 400, height);
image.setBackground(COLOR_RED);
item.setImage(5, image);
The problem is the background color is never setting in the image. My goal is to get a transparent image background.
How can I do that?
Please read the JavaDoc of Image.setBackground. That method does not fill your image with red color.
Also, by setting an image to a TableItem you cannot draw anything, like text, transparently below that image. If you want to do something like that, you'll need "owner draw", see here for a tutorial on that.
Related
I have a Button with text and an ImageView as icon. The image might be of any resolution. Currently, the button will adapt its size to fit the image, e.g. if the image is 400x400, the button will be huge and the text will be tiny next to it.
What I want though, is the image to fit into the button. It should always be as tall as the text is.
ImageView img = new ImageView(image);
button = new Button("Some text", img);
button.setStyle("-fx-background-radius: 6em;");
Wouldn't have thought the solution to this is so easy:
img.setPreserveRatio(true);
img.setFitHeight(button.getFont().getSize());
does the trick for me. JavaFX is weirdly unintuitive sometimes. If the font size may change (e.g. via CSS styling), use a binding instead:
img.fitHeightProperty().bind(Bindings.createDoubleBinding(() -> button.getFont().getSize(), button.fontProperty()));
You can set your image height via
image_view.getLayoutParams().height = YOURHEIGHT
as discussed here.
I would suggest setting your text size to an appropriate value and use the above command to scale the image to the same height.
Test around whether you can size the image with em, otherwise use cm/inch.
If there a problems with image distortion when setting height, refer to this thread.
As the title says, how can I do to remove the background from an imageIcon?
Let's say.. I've a JTree (but it can be every JComponent) and I would like to set an icon for its leaf nodes.
myDefaultTreeCellRendere.setLeafIcon(new ImageIcon(icon));
The icon has the white background and I would like removing that. Is that possible?
I read some answers on Stackoverflow but there a BufferedImage is used and the icon is retrieved from the web and stored in an URL object... My icon is local, instead.
You need to use an image format that supports transparency, such as GIF or PNG. You can either look for a new icon with transparent background, or you can modify your current image with an image editor (Photoshop or Gimp will do).
What you could do is to give to the ImageIcon object the same size as that of the image. For example, if your image has a sioze of 40x40, you do
ImageIcon img = new ImageIcon("path_to_image");
img.setPreferredSize(new Dimension(40, 40));
myDefaultTreeCellRendere.setLeafIcon(img);
+1 if it works :D
I have two RenderedImages. I want to do an Overlay Operation with these two images and therefore they need to match in data type and the number of bands.
The problem I have is that one image has 3 bands (RGB) and the second image has 4 bands (ARGB).
My question is how can I add an Alpha Channel to the first image so I can do the Overlay Operation?
EDIT
Ok, I found a method of adding an Alpha Channel to the first image. Below is the code. I simply created a single banded constant image and merged it with my first image.
ParameterBlock pb = new ParameterBlock();
pb.add(new Float(finalImage.getWidth())).add(new Float(finalImage.getHeight()));
pb.add(new Byte[] {new Byte((byte)0xFF)});
RenderedImage alpha = JAI.create("constant", pb);
finalImage = BandMergeDescriptor.create(finalImage, alpha, null);
The problem I have now is that everytime I add an overlay the image changes colors. All the colors become nuances of red or pink. When I add a second overlay, the image becomes normal again, but the first overlay changes colors. All black areas become white.
Also the background of the overlay is not transparent. It is grey.
Below are examples of the images, so you see how the change colors:
As you can see, the picture and overlays change colors and the background of the overlay isn't transparent.
Can you help me solve this problem, so that the image is always displayed correctly? Thanks!
You can try to create a new BufferedImage with ARGB-model, and just paint the non-transparent background picture into this new BufferedImage. Then you have a BufferedImage with alpha-channel (although all of the pixels are opaque), so the Composition should hopefully work.
im not sure about TYPE_4BYTE_ARGB as I usually work with BufferedImages of TYPE_INT_ARGB but I have often used the method of drawing a RGB BufferedImage to a new ARGB BufferedImage and then drawing that onto other things without problem. the change in color would suggest an unwanted change is being made to the other channels in the overlay process as it doesn't seem to be specific to a specific image. If your overlay operation is similar to drawing one image onto another with alpha I would probably suggest using the Graphics.drawImage()/drawRenderedImage() method for the overlay itself not to mention the background should not even need alpha in this case.
the code:
public RenderedImage overlay(RenderedImage back, RenderedImage front, AffineTransform overlayTransformation)
{
BufferedImage newBack = new BufferedImage(back.getWidth(), back.getHeight(), TYPE_3BYTE_RGB);
newBack.setData(back.getData());
Graphics2D graphics = (Graphics2D)(newBack.getGraphics());
graphics.drawRenderedImage(front, overlayTransformation);
return newBack;
}
you may want to ensure the original back Raster is not modified though.
I'm making a simple side scroller game in Java but am having trouble making the camera move with the person. My plan was to create a buffered image that could adjust the region of the image that is displaying and simply have it follow my character. However, I couldn't find any functions in the API to change the region of the buffered image that's displayed, so how could I do this? Thanks for any help provided.
//The level is created in an 800x400 pixel size, is there any way I can change
//the region myImage displays?
myImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
Well while you will be drawing your image you have a lot of options for example this function can draw any rectangle from your original image on any rectangle of the surface you get Graphics object from.
I am developing a small program which cuts images by the color.
That's will be easiest to explain using this example image:
And I want to create a new image just with the purple form, without the black frame.
Does anyone have any ideas? I am using Java 2D so I think that I need to create an Object "Shape" with the purple area of the first image.
If the image is literally like the one you show, you could:
load the image into a BufferedImage (with ImageIO.read())
create a new BufferedImage of the same size, ensuring it has an alpha layer (e.g. set its type to BufferedImage.TYPE_4BYTE_ABGR)
"manually" go through each pixel in turn in the loaded BufferedImage, getting the pixel colour with getRGB() and checking if it's black
if the colour is black, set the corresponding pixel to transparent in the new image, else to the original colour from the first image (see setRGB() method)
save the new image (with ImageIO.write())
There are fancier ways, but this simple method is nice and understandable and will work fine for images of the type you showed.
You need to use some flood-fill algorithm that finds the boundries of the purple area:
Wikipedia has a page on it with excellent pseudo code and animations.
http://en.wikipedia.org/wiki/Flood_fill