I have a group of JRadioButtons. I want to use image instead of text in my JRadioButtons. So I believe i will have to use this:
JRadioButton(Icon icon, boolean selected)
Now the issue is that I am not sure about how to create this icon. I have the image that I want to use and I have copied the image in my source code folder. It is in .tiff format. i want to read that .tiff image (inputStream I believe) and convert that to icon so that i can have my JRadioButtons.
Please help in implementing this.
Thanks in advance.
Assuming you put the image in your source folder, in the package com.foo.bar, and that your build process copies this file with your classes so that it's in the classpath when running the application (that's what all IDEs do by default), you can just use
new ImageIcon(MyClass.class.getResource("/com/foo/bar/MyImage.png"))
to get you an icon.
I'm not sure that Java has native support for the tiff format, so you might have to convert the image to another supported format to load it (gif, JPEG and PNG will work fine).
If you're getting a NullPointerException, it probably means that the image is not at the path you're indicating.
You said you stuck it right into the src folder so this should work:
new ImageIcon(getClass().getResource("icon.jpg"))
Related
I have a scraped few images from a website. The scraper did not download the image files with the correct extension.
I changed one of the file's extension to ".jpg" and it opened in Picasa. So I wrote a Java program to change all downloaded image file extensions to ".jpg".
Now when I tried to open images in Java, using Swing, it's not displaying the image. I am using this code to display. It is working fine with other images downloaded manually from web.
String path = "resources\\images\\"+gamePlayer.getName()+".jpg";
JLabel image = new JLabel(new ImageIcon(path));
I even tried converting image files to a different format by a third part software(Format Factory) but even it couldn't read the file.
I can open and view all the images in Picasa.
How do I display scraped images with incorrect extensions using Swing ?
Picassa is probably inspecting the file signature (https://en.wikipedia.org/wiki/List_of_file_signatures) rather than the file extension and therefore opens it correctly. You can't just rename a file and expect your code to understand how to open/display it.
In my project I need to save a JPG image to android gallery. Unfortunately, Unity tools do not provide such function in-the-box, so I must implement this function myself. As far as I know, I need to make a little plugin to do that and already figured how to make a simple plugin, but I have questions about my specific goal.
So, the main part is: I have texture in Unity, I encode it to JPG format, resulting in a byte array. Then I convert this byte array into a string to be able to pass this string via android bridge into method located in a java class. Everything that's going to happen further is a black box for me.
I know the basic theory: I need to convert the string into an image somehow, specify the folder for the pictures of my app, save the image into this folder, and somehow refresh device memory so the image is visible in the gallery. But the implementation is a big mystery for me. I know it can be done after a few days of digging, but I guess that this can be done in one-two simple methods.
Can anybody give me some hints? Are there any issues between conversion from C# code in Unity into Java code in, for example, Eclipse?
My app is saving images into a folder. Simultaneously another piece of software watches over this folder and prints pasted images.
Problem I've encountered is that printing app can send on printer half rendered image or doesn't react to saved images at all.
I'm using javax.imageio.ImageIO.write(RenderedImage im, String formatName, File output) to save BufferedImage into png format.
If locking required please provide a code example with explicit locks. If not explain why.
I've tested on Windows, JRE 1.8
Basically, you should write the image to a temporary file and then rename it to the final file name. this way the file is completely written before the other app becomes aware of it.
You probably want to implement some kind of locking mechanism that files are only read when they are completely written, or not at all.
I need to do this school project where I would take in a text file, convert it to a pdf file (but also format the text so there are different fonts, colors, etc). I thought it would be a cool addon to my project if I were to make a window that would show a visual of how the text file looked before the conversion, and how the pdf file looked after the conversion. I searched up how to take screenshots using java and I was able to find this code snippet.
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screenshot.png"));
However, this would only take a screenshot of the screen in its current state. Is there any way that the java program would be able to take a screenshot of the text file I had, and the pdf file I created afterwards without these two files already being opened and displayed on the screen?
I am using GWT and I am using Image Widget to view an image. This image is located in my file system.
I wrote the following line of code:
String src = "file:///D:/myfolder/myfile.jpg";
Image image = new Image();
image.setUrl(src);
Please note I need to show only local images; not from the server. It may sound strange but I need to show from the client machine. Assume all clients will have same image and same path.
Thanks.
I believe that what are you trying is not possible due to "Same Origin Policy" in browsers: Same Origin Policy Wikipedia article.
It's very unusual that you have to specify full path with drive name.
There are many ways to get your context path and then make the path relative to it.
I've just tried to setUrl to the GWT's Image in your way. Everything was fine.
Try to check your path, image extension etc.
Maybe your do something wrong when add image to the layout?