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/
Related
I am trying to attach different images into a image. This is happening but after attaching the color of the image changes. Here is my existing code
BufferedImage baseImg = getBaseImage(src);
Graphics2D g = baseImg.createGraphics();
BufferedImage bmg = getBufferedImageFromSrc(path);
String[] coords = coord.split(",");
g.drawImage((Image)bmg,(int) Double.parseDouble(coords[0]),(int) Double.parseDouble(coords[1]), 60,60,Color.white,null);
g.dispose();
String path = "C://resources/temp.jpeg";
ImageIO.write(bimg, "jpeg", new File(path));
i am able to add the image to the base image at the specified co-ordinates but the base image colour is getting changed.
Here is a sample image
Thanks in advance
I'm trying to add a background in a PDF using Scalable Vector Graphics (.svg). Initially the recommended way was to transform the SVG into an Image object and then use scaleToFit to get it to the right size, then add it to the document. This works partially as it transforms the small and scalable SVG into a Bitmap. Next I've made a PdfFormXObject in order to get back the scaling by having it drawn on each page. However, now it does not display anything at all.
ByteArrayInputStream inputStream = new ByteArrayInputStream(backgroundBytes);
PdfFormXObject svg = SvgConverter.convertToXObject(inputStream, pdf);
PdfCanvas canvas = new PdfCanvas(pdf.getFirstPage());
Rectangle rect = new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight());
canvas.addXObject(svg, rect);
How should I be adding SVG backgrounds to iText 7 PDFs? Can this be done properly in the first place? I have not been able to find good code examples.
Update:
Here is the code for converting the SVG to a properly scaled Image. The issue with this is that it works for adding the image, but it adds it as an element so it pushes everything else down.
ByteArrayInputStream inputStream = new ByteArrayInputStream(svgAsBytes);
Image image = SvgConverter.convertToImage(inputStream, pdf);
image.scaleAbsolute(PageSize.A4.getWidth(), PageSize.A4.getHeight());
int totalPages = pdf.getNumberOfPages()+1;
for(int pageNumber = 1; pageNumber < totalPages; pageNumber++ ) {
document.add(image);
}
Hi i have a similar Problem. I am trying to add a SVG image as a table cell background. The problem is that i can not scale the image. Here is my code:
InputStream triangleSteam = this.getClass().getResourceAsStream("/AH-AddressTriangle.svg");
Image triangle = SvgConverter.convertToImage(triangleSteam, pdfDocument);
//triangle.setHeight(UnitValue.createPointValue(127.83f));
//triangle.setWidth(UnitValue.createPointValue(78.63f));
triangle.scaleAbsolute(78.63f, 127.83f);
Cell headerCell_12 = new Cell();
headerCell_12.setBorder(Border.NO_BORDER);
headerCell_12.setPadding(0f);
headerCell_12.setHeight(UnitValue.createPointValue(127.83f));
headerCell_12.setWidth(UnitValue.createPointValue(78.63f));
headerCell_12.setNextRenderer(new ImageBackgroundCellRenderer(headerCell_12, triangle));
headerTable.addCell(headerCell_12);
And here ist the BackgroundCellRenderer I am using:
protected class ImageBackgroundCellRenderer extends CellRenderer {
Image img;
public ImageBackgroundCellRenderer(Cell modelElement, Image img) {
super(modelElement);
this.img = img;
}
#Override
public IRenderer getNextRenderer() {
return new ImageBackgroundCellRenderer((Cell) modelElement, img);
}
#Override
public void draw(DrawContext drawContext) {
try {
img.scaleToFit(getOccupiedAreaBBox().getWidth(), getOccupiedAreaBBox().getHeight());
drawContext.getCanvas().addXObject(img.getXObject(), getOccupiedAreaBBox());
super.draw(drawContext);
} catch(Exception e) {
e.printStackTrace();
}
}
}
The background is added but the image is not scaled (see picture)
Screenshot from Adobe Illustrator
The SVG becomes even bigger that it actually was!
Thank you!
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));
}
I was looking at this question and I was looking at the first answer.
So I tried to use this code:
public static Image getIcon(String fileName) throws Exception {
File file = new File(fileName);
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);
ImageIcon imageIcon = (ImageIcon) icon;
Image image = imageIcon.getImage();
return image;
}
Which does return an Image (or throws an Error) but the Image has terribly low resolution.
I am assuming that this is because the 16x16 Image is returned.
Is there any way to state which Image I want to be returned?
Java offers you two possibilities to retrieve file icons.
You already know the first one:
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(FILENAME));
that gives you a 16x16 pixel result.
The other one using ShellFolder
Icon icon = new ImageIcon(ShellFolder.getShellFolder(new File(FILENAME)).getIcon(true));
will retrieve you the larger one (32x32) depending on the boolean flag getLargeIcon in the getIcon method.
I'm sorry for you but more is (at the moment) not possible with the java default libraries. Interest exists as you can read in this JDK bugreport.
But nothing has been done so far.
If you really want to have larger versions you will need to retrieve them with the OS depending native calls or store them manually as local application ressources.
Note: If you have problems accessing ShellFolder you should read this question.
I used this method:
protected ImageIcon getImageIcon() {
File f = new File((iconPath!=null)?iconPath:"");
if (!f.isFile() || !f.canRead()) {
iconPath = Constants.getDefaultPreviewIconPath();
}
ImageIcon icon = new ImageIcon(iconPath, titolo);
return new ImageIcon(Utils.getScaledImage(
icon.getImage(),
Constants.getICON_WIDTH(),
Constants.getICON_HEIGTH()));
}
where getScaledImage is:
public static Image getScaledImage(Image srcImg, int w, int h) {
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
I want to give a visual indication that a node has been transferred to clipboard with a "Cut" action. One intuitive look used by at least one proprietary OS is to make this the same image, but slightly transparent.
I'd quite like to know whether it is in fact possible somehow to use the icons used by the Windoze OS (W7)... but I'd be more intrigued if it were possible to interfere in some way (in the renderer) with the Icon, by somehow messing with the Graphics object used by Icon.paintIcon() ... just for a given node, obviously. I'm not clear where an Icon goes hunting for the Graphics object it uses when it is painted ... any enlightenment would be most welcome.
later
Many thanks to MadProgrammer. Spotted this possibility as a way of extracting obfuscated visuals with a view to their manipulation: https://home.java.net/node/674913 ... it works. Putting code here in case of broken link...
public class IconTest {
public static void main(String[] args) {
Icon leafIcon = UIManager.getIcon("Tree.leafIcon");
// ... ("Tree.closedIcon") ("Tree.openIcon")
BufferedImage img1 = new BufferedImage(leafIcon.getIconWidth(),
leafIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = img1.createGraphics();
g.drawImage(((ImageIcon) leafIcon).getImage(), 0, 0, null);
g.dispose();
try {
ImageIO.write(img1, "PNG", new File("leafIcon.png"));
} catch (IOException e) {
System.out.println("Error writing to file leafIcon" + ", e = " + e);
System.exit(0);
}
}
}
Then use MadProgrammer's technique to alter the image in any way one likes: change transparency, colour, etc. Great stuff.
I'd quite like to know whether it is in fact possible somehow to use the icons used by the Windoze OS (W7)
FileSystemView#getSystemIcon will give you the OS's icon representation of a given File, for example...
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("ThatImportantDoc.docx"));
I want to give a visual indication that a node has been transferred to clipboard with a "Cut" action. One intuitive look used by at least one proprietary OS is to make this the same image, but slightly transparent.
You need to paint the previous Icon to BufferedImage, which has had a AlphaComposite applied to it, for example
BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
You then need to wrap the resulting BufferedImage in a ImageIcon, which allows you to pass the image as a Icon to the rest of the API.
JPanel panel = new JPanel();
panel.add(new JLabel(icon));
panel.add(new JLabel(new ImageIcon(img)));
JOptionPane.showMessageDialog(null, panel, "Icon", JOptionPane.PLAIN_MESSAGE);
To get this to finally work, you will need to provide a TreeCellRenderer capable of supporting your functionality. Have a look at How to Use Trees for more details
Just one tweak enabling me to do what I mainly wanted to do: get the UI images "from the source code".
public class IconTest {
public static void main(String[] args) {
// OS folder icon
// Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("."));
// proprietary word processor
// Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("Doc1.docx"));
// taken from PNG file
// Icon icon = new ImageIcon( "openIcon.png" );
// taken directly from the Java images held somewhere (?) in the code
Icon icon = UIManager.getIcon("Tree.leafIcon");
// Icon icon = UIManager.getIcon("Tree.openIcon");
// ... ("Tree.closedIcon") ("Tree.openIcon")
BufferedImage img = new BufferedImage( icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setComposite(AlphaComposite.SrcOver.derive( 0.5f));
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
JPanel panel = new JPanel();
panel.add(new JLabel(icon));
panel.add(new JLabel(new ImageIcon(img)));
JOptionPane.showMessageDialog(null, panel, "Icon", JOptionPane.PLAIN_MESSAGE);
}
}