Display image on a Java button - java

xstButton.setIcon(new ImageIcon("D://icon-tender-check-press.png"));
I am using this line of code to display an image on a Java button.
I cannot see the desired image on the button, need help!!!

You could try it like this:
Image image = ImageIO.read(getClass().getResource("D://icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
But i would suggest to create a Folder in your project to store images:
Image image = ImageIO.read(getClass().getResource("images/icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
Although i am not exactly sure what your question is

Probably file "D://icon-tender-check-press.png" doesn't exists or is not a valid image.
Check it exists first.
File f = new File("D://icon-tender-check-press.png");
if(f.exists() && !f.isDirectory()) {
System.out.println("File exists");
}

Related

How to change a code of putting image within a label from desktop to project file

So I have this code It's work when the image at my desktop, I added the image at src file put I couldn't convert it can you tell me what is the problem? this the code it set the image to fit the label too:
public void ScalImage() {
ImageIcon image = new ImageIcon("C:\\Users\\HP\\Desktop\\ath3.png");
Image img = image.getImage();
Image imgScale = img.getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_SMOOTH);
ImageIcon scaliedicon = new ImageIcon(imgScale);
jLabel2.setIcon(scaliedicon);
}
I tried to say: ImageIcon image = new ImageIcon("ath3.png");
didn't work
So I have solved it I wrote like that
ImageIcon image = new ImageIcon(getClass().getResource("ath3.png"));
It's work for me. I wish I'm right

Download .svg or .xml to internal memory and show it in ImageView

I need to download vector images from server and display them in ImageViews. I succesfully downloaded the images to external storage, but now I'm having a problem displaying them.
I'm using a SVG library from larvalabs and here's the code:
File image = new File(Environment.getExternalStorageDirectory(), "image.svg");
if (image.exists()) {
try {
InputStream imageStream = new FileInputStream(image);
SVGBuilder builder = new SVGBuilder();
builder.readFromInputStream(imageStream);
SVG svg = builder.build();
Drawable draw = svg.getDrawable();
((ImageButton) view).setImageDrawable(draw);
imageStream.close();
} catch (Exception e) {...
It turns out that the InputStream is empty. But the file is there, the path shows to the correct directory, the file is as it should be, so I don't know what else could be the problem. I'm adding a print screen from debugger if it helps.
Debugger Print Screen

javafx read javafx.scene.image.Image to ImageIO.write(), from CMYK to RGB

What I want to do is read an image from FileChooser and write it to file. I had to store the image in a javafx.scene.image.Image so that I can display it and clip it inside a circle. I have a little problem with trying to write the image that I got from javafx.scene.image.Image to file. The conversion process is not fluid, converts from CMYK to RGB (therefore turning my picture to some pink thing.
Please, I have checked a lot of other sources, and no one has been able to give me a notable solution
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(parent);
// get Image from selectedFile
Image userImg = = new Image( selectedFile.toURI().toURL().toString() );
if ( userImg != null ) {
String format = "jpg";
String filename = "d:\\pictureName."+ format;
File file = new File(filename);
// convert Image to BufferedImage
BufferedImage bufferedImage = SwingFXUtils.fromFXImage( userImg, null);
try {
// this is where i want to convert the color mode
// from cmyk to rgb, before i write it to file
ImageIO.write( bufferedImage, format, file );
} catch (IOException e) {
System.out.println("Exception :: "+ e.getMessage() );
}
}
Why do you think that there is some CMYK to RGB conversion happening? I suspect the reason for your "pink thing" is something different. The easiest way to find out is to change your output format from jpeg to png and see whether it makes a difference.
I think you are again one of the many people who are hit by this bug https://bugs.openjdk.java.net/browse/JDK-8119048 which is not considered important enough to be fixed. If you read the comments in there you will find a work-arround. Basically the idea is to copy the image after the conversion into a new image without alpha channel.
I'd really like to know how many more people have to waste their time until this bug finally gets enough attention to be fixed.

java jLabel image does not update

I'm creating a feedback label that displays the picture that the user has chosen in a file dialog.
The moment when a picture file is selected, the label will update itself into that image of which the user has clicked.
The first time when the picture is chosen it works fine, however when another picture is chosen for the 2nd time onwards, it remains as the first picture.
Codes:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//browse button
FileDialog fd = new FileDialog(this, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.jpg"); // jpg files only
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null) {
System.out.println("You cancelled the choice");
} else {
savePicture("temp"); // save it in temp.jpg. This overwrites any existing picture.
ImageIcon imgThisImg = null;
imgThisImg = new ImageIcon(absfilePath+ "/temp.jpg");
jLabel7.setIcon(null);
jLabel7.setIcon(imgThisImg);
jLabel7.revalidate();
jLabel7.repaint();
}
During debugging, the moment after savePicture() function is executed, the directory picture is updated. Therefore it's not an issue with overwritting the file. The file is overwritten correctly, why does it still display the previous image? Is there a cache or something that i need to clear?
Thanks.
Using ImageIO to the read file works best. Can be achieved using the following line.
jLabel7.setIcon(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Cameron Gillespie\\Documents\\NetBeansProjects\\OnlineCabsClient\\src\\images\\taxiBackground.png")))));
You are taking the label then setting the icon. Creating a new label and ImageIcon. Then using ImageIO to read the file. Reading the image and printing it to the label.

How to get picture from *.docx with all changes using POI?

Let's review the relationship between "docx" and "pictures":
As I understand it, *.docx stores original pictures (pictures at the moment when you copy/paste them into Word). And every time when you use that picture, Word makes a "link" to original picture.
But if you make some changes to that picture (for example resize, crop or change color) Word remembers your changes, modifying the "link" (add some special tags). That's great, because you will never lose quality of your picture!
Let's get a picture from our *.docx file. To do that I use this code snippet:
XWPFDocument wordDoc = new XWPFDocument( pathToFile );
for (XWPFParagraph p : wordDoc.getParagraphs()) {
for (XWPFRun run : p.getRuns()) {
for (XWPFPicture pic : run.getEmbeddedPictures()) {
byte [] img = pic.getPictureData().getData()
File outputfile = new File ( pathToOutputFile );
BufferedImage image = ImageIO.read(new ByteArrayInputStream(img));
ImageIO.write(image , "png", outputfile);
}
}
}
But this way I get the original pictures from *.docx. If, for example, you cropped out a section from your picture and gave me the rest, then I always find the whole image in outputfile. That's not good.
Does anyone know how to get the picture with all changes that someone made to it in Word?

Categories