How to load an image in Java using toolkit and new URL - java

I need to use a JPEG image in my Java applet.
In my applet class, I define the image name and create an object to ImageBuffer class.
String iname= "image1.jpg";
b = new ImageBuffer(iname,this);
In the ImageBuffer class, I call
Image image = null;
image = Toolkit.getDefaultToolkit().getImage(new URL(applet.getCodeBase(),fileName));
While this does not flag an error and image is not null anymore, it does not initialize image correctly. The height and width are -1. The url of the path however appears to be correct : /C:/Users/..../image1.jpg
How do I correctly load the image? It is in the bin file of my Eclipse project currently.

The height and width are -1.
Use a MediaTracker to monitor the asynchronous loading progress of the image. Alternately use ImageIO to load the image prior to the next code line.

If the image is placed in the correct location, then this would fix it
image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(new URL(applet.getCodeBase(),fileName))).getImage();
If the code returns a NullPointerException it means the image is not placed in the correct directory.

Related

How to add src path image to my pdf using Image.getInstance() method?

Here in my web-application under Images folder pdficon.png image is available I want to add this image using Image.getInstance() method but whaen I tring like this..
image = Image.getInstance("images/pdficon.png");
Here it showing Exception is
IOException :: C:\Users\Developpc\Downloads\wildfly-9.0.2.Final\bin\images\pdficon.png (The system cannot find the path specified)
So,what I need to do....
Signup for some free image hosting account and upload your image. After uploading your image you can get the URL of the image and put that URL for image.getInstanceMethod.
Image img = Image.getInstance(IMG_URL);
If you are using that image multiple times then create an string constant and use that.

Updated Image not getting reflected

I am trying to crop image.There is an ajax call made to the server end where cropping takes place and original file gets replaced with cropped image.
Now when i get back the control, i still see the old image even though cropped image exists at the same location.
Changes reflect only after page refresh which i don't want users to do it.Code for crop image is as follows
BufferedImage originalImgage = ImageIO.read(new File(filePath+"\\"+subFolder+"\\"+fileName));
ImageIO.write(originalImgage,extention,new File(filePath+"\\"+subFolder+"\\"+dateStamp+"_"+fileName));//save original image
BufferedImage SubImgage = originalImgage.getSubimage(xAxis,yAxis,width,height);
File outputfile = new File(filePath+"\\"+subFolder+"\\"+fileName);
ImageIO.write(SubImgage,extention,outputfile);
pls help
Thanks
Try to replace the image src parameter with the new path returned from ajax in ajax success call back.
Well after struggling a lot finally found the problem.The image is cached by the browser. So whenever u do any operations on image on the same file location , the latest copy is not download.Instead old image from cache is still referred.
Hence the solution was to make browser download latest copy.This was simple.
Append dummy parameter to file path forcing browser to download
rand = new Date().getTime();
var image_Path = filePath+"?crop="+rand

Import image GUI not appearing

I am trying to upload an image on a GUI. Here is what my code reads right now:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
myLabelK = new JLabel();
myLabelK.setBounds(500,100,200,200);
myLabelK.setIcon(icon);
myPanel.setLayout(null);
myPanel.add(myLabelK);
Validate();
add(myPanel);
setVisible(true);
When I run my program this image is not popping up on my GUI.
The nameSearched.getImage() is calling a method in a different class which returns an image entered into the system. For example, Peter.jpg.
Please help me figure out how to get my image on the screen.
If you are just using Peter.jpg as you relative path name,
Using an IDE, you image should be ditrectly below the ProjectRoot directory
ProjectRootDir
Peter.jpg
src
If you wanted to have your image in an images folder:
use "images\Peter.jpg"
ProjectRootDir
images
Peter.jpg
src
Maybe you want to retrieve the image like this
ImageIcon icon = new ImageIcon("Peter.jpg");
Another possibility, If you do this:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
then nameSearched.getImage() must return either a String fie path of the image
or an Image object.
Maybe you just want nameSearched.getImage() to return a String from an array of String paths. Not sure what nameSearched.getImage() actually does so I can't go into further explanation or correction

how to display image using its path

i have a the path of an image in a string i want to display it on a jlabel using the path. please help me.
if(!ar.get(16).equals("empty")){
String photo=(String)ar.get(16);
System.out.println(photo);
// if(!photo.equals(""))
// pic.setText(photo);
Image image=Toolkit.getDefaultToolkit().getImage(photo);;
ImageIcon img=new ImageIcon(image.getScaledInstance(view.jLabel5.getWidth(), view.jLabel5.getHeight(), 0));
//JpegReader jrdr=new JpegReader();
//view.jLabel5.setSize(img, image.getWidth());
view.jLabel5.setPreferredSize(new Dimension(100, 100));
view.jLabel5.setIcon(img);
}
If the image is an embedded resources (ie lives within the application context/is bundled with the application Jar), then you need to use getResource to gain access to it..
Toolkit.getDefaultToolkit().getImage expects that the String passed to it is a file on the file system.
If the image is embedded, then you will need to use something more like...
Toolkit.getDefaultToolkit().getImage(getClass().getResource(photo))
To load it.
If the image is being loaded from the file system, you could use
File file = new File(photo);
if (file.exists()) {
// Attempt to load the image
} else {
// Show error message.
}
Because of the way Toolkit#getImage works, it will not provide any details if the image fails to load for some reason.
Instead, you should be using ImageIO, which will throw an IOException if it was unable to load the image for some reason...
BufferedImage img = ImageIO.read(getClass().getResource(photo));
or
BufferedImage img = ImageIO.read(new File(photo));
Depending on where the image is located.
Take a look at Reading/Loading an Image.
You should also avoid calling setPreferredSize explicitly and simply allow JLabel to make it's own choices...
Put the image file in the project. Under a seperate folder.
ImageIcon image = new ImageIcon(this.getClass().getResource("/images/abc.jpg"));
JLabel imageLabel = new JLabel(image, JLabel.CENTER);
If you want to load the image for any other location on your computer then,
ImageIcon image = new ImageIcon("C:/images/image.png");
JLabel imagelabel = new JLabel(image);
Make sure your paths are correct. If you photo string path is "photo.jpeg", your path should look something like this
ProjectRoot
photo.jpeg
src
If you want to put the photo.jpeg in a directory images, you should use "images/photo.jpeg"
ProjectRoot
images
photo.jpeg
src
This is considering you're using an IDE like Netbeans or Ecplise.

Retrieve an image from the web in java

I am trying to read an image that resides somewhere on the web from my Java program. So far I have successfully loaded an image by using the following code.
URL url = new URL("http://www.google.com/images/nav_logo4.png");
Image img = Toolkit.getDefaultToolkit().getImage(url);
What I want to know is why this code (which is the first i tried) does not work:
BufferedImage img = ImageIO.read(new File("http://www.google.com/images/nav_logo4.png"));
This would have the benefit of giving me a BufferedImage. Also, how can I make the above code block until the image is loaded? I know I can use an ImageObserver, but is there a simpler way?
When I try the second option, I get this exception:
javax.imageio.IIOException: Can't read input file!
A File cannot refer to a URL.
Although I haven't tried it, there appears to be a ImageIO.read(URL) method, which can take an URL as the input as an URL object.
I would presume it would be called as follows:
ImageIO.read(new URL("http://url/to/my/image.png"));
File objects cant read from URLs

Categories

Resources