Add border to ImageView (JavaFX) - java

I'm trying to add a border to my Image/ImageView, but I don't know how to do it.
When I press the button three randomly generated images are added to the GridPane. I've tried adding -fx-border-color and -fx-border-width properties through an external CSS file and then applying it but that way hasn't worked.
Any help is appreciated!
btnLever.setOnAction((ActionEvent e) ->
{
// New array for loading in the images from 'generateImages' method
String[] fruits = new String[3];
fruits = generateImages();
// GAME WILL RUN IF THE CREDITS IS GREATER THAN 0
if(credits >0)
{
// IMAGES GENERATED
Image img1 = new Image(fruits[0]);
Image img2 = new Image(fruits[1]);
Image img3 = new Image(fruits[2]);
// ADDS IMAGES TO THE GRID
grid.add(new ImageView(img1), 0, 0);
grid.add(new ImageView(img2), 1, 0);
grid.add(new ImageView(img3), 2, 0);

Related

display gifs in java jars using eclipse

Hi I am unsure on how to format my animated gif images to let them show on Jars created on eclipse.
try {
//ImageIcon titleIcon = new ImageIcon(ImageIO.read(getClass().getResource("title.gif")));
title = new ImagePicture (new ImageIcon(ImageIO.read(getClass().getResource("title.gif"))), 0, 0);
//title = new ImagePicture (new ImageIcon("title.gif"), 0, 0);
}//end try
catch (IOException e) {
}//end catch
//set title bounds
title.setBounds(260, 0, 400, 100);
That is my code right now for an animated GIF, Thank you for your input.
Hard to give a good example without more context, but you can try adding the ImageIcon to a JLabel like this.
URL url = this.getClass().getResource("title.gif");
Icon title = new ImageIcon(url);
JLabel titleLbl = new JLabel(title);
//You have to add titleLbl to a container after this, of course

JavaFX create a new image from an existing one

In JavaFX u can create a new Image from a string(path), how would i go about creating a new Image from an existing javafx.scene.image.image?
as following:
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);
But instead of the path an actual image object.
I want to change the size of the image.
There is typically no need to create a new Image instance in order to perform rescaling. The API allows you to view or draw scaled versions of an existing Image instance. For example, given
Image image = new Image("my/res/flower.png");
you can create an ImageView that displays a scaled version with
ImageView imageView = new ImageView(image);
imageView.setFitWidth(100);
imageView.setFitHeight(150);
or you can draw a scaled version to a canvas with
Canvas canvas = ... ;
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, 100, 150);
A late response, but since I ran into the same issue (I needed an Image to pass to setDragView() ), here is the solution I have implemented:
Image makeThumbnail(Image original) {
ImageView i = new ImageView(original);
i.setFitHeight(64); i.setFitWidth(64);
return i.snapshot(new SnapshotParameters(), new WritableImage(64, 64));
}

Scaling an Image and positioning it at 0,0 in WPF

I have created BitMapSource from a list of RGBA pixels:
BitmapSource bmp = BitmapSource.Create(imageStrideInPixels, height, 96, 96, PixelFormats.Bgra32, null, imageData, imageStrideInPixels * pixelWidth);
I then create an image from the BitMapSource:
// create image and set image as source
Image BmpImg = new Image();
BmpImg.SetValue(Canvas.ZIndexProperty, 0);
BmpImg.Width = imageScaleWidth;
BmpImg.Height = imageScaleHeight;
BmpImg.Source = bmp;
I then add the Image to the Canvas:
mycanvas.Width = imageScaleWidth;
mycanvas.Height = imageScaleHeight;
mycanvas.Children.Clear();
mycanvas.Children.Add(BmpImg);
Canvas.SetLeft(BmpImg, 0); // to set position (x,y)
Canvas.SetTop(BmpImg, 0);
The problem is that it is not getting scaled to imageScaleWidth and imageScaleHeight, and it is being displayed half way down the canvas.
Note, I was able to do this in Java SWT by:
imageData = imageData.scaledTo(imageScaleWidth, imageScaleHeight);
gc.drawImage(imageData, 0, 0);
You can scale your image using a ScaleTransform:
// scale the original bitmap source
var transformedBitmap = new TransformedBitmap(
bmp,
new ScaleTransform(
imageScaleWidth / (double) bmp.PixelWidth,
imageScaleHeight / (double) bmp.PixelHeight));
// create image and set image as source
Image bmpImg = new Image();
bmpImg.SetValue(Canvas.ZIndexProperty, 0);
bmpImg.Source = transformedBitmap;
mycanvas.Width = imageScaleWidth;
mycanvas.Height = imageScaleHeight;
mycanvas.Children.Clear();
mycanvas.Children.Add(bmpImg);
Note that your image will be positioned at offset 0, 0 by default.
Instead of this
mycanvas.Children.Add(BmpImg);
Try this
mycanvas.Background = new VisualBrush(BmpImg);
This should render properly.
Are you sure the image is half way down the canvas and not the canvas itself is centered in its parent? I tested it and it appears that you can control the canvas position by setting vertical/horizontal alignment on canvas' parent. And also it scales properly when using the code you provided. However I created a BitmapSource in a different way. I used the following code:
PngBitmapDecoder decoder = new PngBitmapDecoder(new Uri(#"..."), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmp = decoder.Frames[0];

How to add two images in one cell in iText?

I'm currently creating a system that generates a PDF. However, I cannot put two - three images in a single cell. I tried for-looping it but it has borders. What should i do?
Please take a look at the ImagesInCell example. It uses three images:
public static final String IMG1 = "resources/images/brasil.png";
public static final String IMG2 = "resources/images/dog.bmp";
public static final String IMG3 = "resources/images/fox.bmp";
These are the image instances:
Image img1 = Image.getInstance(IMG1);
Image img2 = Image.getInstance(IMG2);
Image img3 = Image.getInstance(IMG3);
The easiest way to add multiple images to a single cell is by using addElement multiple times:
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(50);
table.addCell("Different images, one after the other vertically:");
PdfPCell cell = new PdfPCell();
cell.addElement(img1);
cell.addElement(img2);
cell.addElement(img3);
table.addCell(cell);
document.add(table);
The result looks like this:
As you can see, the images were scaled automatically, to fit the width of the cell. If that isn't what you want, you have to improve your question, because you only claim that you can't add three images to the same cell, whereas this simple example proves the exact opposite.
Maybe you want something that looks like this:
In the first row with images, we use the same addElement() method as before, but we change the width percentage of the image to 20%:
cell = new PdfPCell();
img1.setWidthPercentage(20);
cell.addElement(img1);
img2.setWidthPercentage(20);
cell.addElement(img2);
img3.setWidthPercentage(20);
cell.addElement(img3);
table.addCell(cell);
In the second row with images, we use a different approach: we have wrapped the images inside Chunk objects, so that we can put them next to each other:
Paragraph p = new Paragraph();
img1.scalePercent(30);
p.add(new Chunk(img1, 0, 0, true));
p.add(new Chunk(img2, 0, 0, true));
p.add(new Chunk(img3, 0, 0, true));
cell = new PdfPCell();
cell.addElement(p);
table.addCell(cell);
Observe that I scaled the first image. The three images wouldn't fit next to each other if that image kept its original size.
Wrapping an image inside a Chunk has the advantage that we can mix images and text:
p = new Paragraph("The quick brown ");
p.add(new Chunk(img3, 0, 0, true));
p.add(" jumps over the lazy ");
p.add(new Chunk(img2, 0, 0, true));
cell = new PdfPCell();
cell.addElement(p);
table.addCell(cell);

The whole file icon is not visible in javafx tableview

I have a JavaFX tableview in which I'm listing files and their properties, including the ICON of the file. My problem is that a part of the icon is missing.
I don't know how to describe it completely so I included an image below.
My tableview is to the right and windows explorer is to the left.
Here is the code:
#Override
public void updateItem(ImageIcon imageIcon, boolean empty){
if (imageIcon != null){
// I have tried adding minimum width and height too
HBox box= new HBox();
// JavaFX scene imageview
ImageView imageView = new ImageView();
// Have tried with and without setFitHeight
imageView.setFitHeight(16);
imageView.setFitWidth(16);
// This line prints: 16 16
System.out.println(imageIcon.getIconWidth() + " " + imageIcon.getIconHeight());
// Create BufferedImage of the imageicon
BufferedImage bi = new BufferedImage(
imageIcon.getIconWidth(),
imageIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
imageIcon.paintIcon(null, g, 0, 0);
g.dispose();
// BufferedImage to fxImage
Image fxImage = SwingFXUtils.toFXImage(bi, null);
imageView.setImage(fxImage);
box.getChildren().addAll(imageView);
setGraphic(box);
}
}
Thanks.
UPDATE
I have google around some more and i copied an example directly and pasted into a new project, and the same thing happened with the icons. In the example, the user was probably using windows 7 by judging the look of the icons, so maybe this is just the way it looks in windows 8. It was the code in the answer at the bottom with a picture: www.stackoverflow.com/questions/28034432/

Categories