GWT Image setUrl() - java

i am using - and new to - GWT and i made an Image Widget to view an image. This image is found on my file system i wrote.
String src = "file:///D:/myfolder/myfile.jpg";
Image image = new Image();
image.setUrl(src);
but image doesn't appear!

You should specify either the whole URL (http://www.example.com/img/myfile.jpg) or (better yet) just relative to the root: /img/myfile.jpg. And, of course, you have to place your images in your WAR directory.
That's the simple setup. If you have more images and want to optimize fetching them (many images -> many requests to the server), have a look at the ClientBundle.

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

Caching Zoomable Web Image

Aquery
Zoomable Web Image
In addition to ImageView, a WebView can be used to display an image along with Android build in zoom support for WebView. Image will be centered and fill the width or height of the webview depending on it's orientation.
I using,
String url1 = "file:///mnt/sdcard/image.jpg";
aq.id(R.id.webView1).progress(R.id.progress).webImage(url1);
I noticed that even if I cut off the connection and re-open that application, the image is still displayed. Is it cached? I looked into its sd card folder, nothing there. I'd like to utilize cached images instead of fetching it many times.
Also, I am both using Universal-Image-Loader and Android Query. I am just concern if they can both read cached images.
//Aquery
//returns the cached file by url, returns null if url is not cached
File file = aq.getCachedFile(url);
The url1 is being downloaded every time you call
aq.id(R.id.webView1).progress(R.id.progress).webImage(url1);
Your cached file is being returned to File file object but you arent making use of it in

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

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.

Load Java Image inside package from a class in a different package

I have a Java project called MyProject. I have a few different packages (keeping names simple for the purpose of this question), as follows:
src/PackageA
src/PackageA/PackageAa
src/PackageA/PackageAa/PackageAaa
src/PackageB
src/PackageB/PackageBa
src/PackageB/PackageBa/PackageBaa
I have a class
src/PackageA/PackageAa/PackageAaa/MyJavaFile.java
And I have an image
src/PackageB/PackageBa/PackageBaa/MyImage.png
Inside of MyJavaFile.java, I would like to declare an Image oject of MyImage.png
Image img = new Image(....what goes here?...)
How can I do this?
You could either call Class.getResource and specify a path starting with /, or ClassLoader.getResource and not bother with the /:
URL resource = MyJavaFile.class
.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
or:
URL resource = MyJavaFile.class.getClassLoader()
.getResource("PackageB/PackageBa/PackageBaa/MyImage.png");
Basically Class.getResource will allow you to specify a resource relative to the class, but I don't think it allows you to use ".." etc for directory navigation.
Of course, if you know of a class in the right package, you can just use:
URL resource = SomeClassInPackageBaa.class.getResource("MyImage.png");
(I'm assuming you can pass a URL to the Image constructor in question. There's also getResourceAsStream on both Class and ClassLoader.)
you can use relative path since the the relative path is project folder.
ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png");
/folderB/folderBa/folderBaa/MyImage.png
The image can stored into a project folder location .eg: /images/MyImage.png
Then try:
Image img = new Image(/images/MyImage.png);
Using a file path is not possible when running a program that's in a jar file, especially if the program is being loaded as an applet or WebStart application then you can use ClassLoader to get image.
use the following code to load the images:
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
This IS the best way to handle all images and icons in a JAR App.
Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.
Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:
URL iconUrl = this.getClass().getResource("/image-iconb.png");
Toolkit tk = this.getToolkit();
imageIcon = tk.getImage(iconUrl);
(imageIcon is just a constructor declared Image variable)
Now you can set a window icon as simply as:
setIconImage(imageIcon );
and at the same time use the same variable when setting the System TrayIcon by declaring:
trayIcon = new TrayIcon(imageIcon, "SystemTray Demo", popupMenu);
The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.
As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.
Much better in my view.
Use the getResource method to read resources inside the src root. For example, the following code retrieves images from a folder src/images.
// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create icons
Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif"));
Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif"));
The example assumes that the following entries exist in the application's JAR file:
images/save.gif
images/cut.gif
Image img = new Image("./src/PackageB/PackageBa/PackageBaa/MyImage.png");
This shall go the path of the image is first inside src (source) then package so the program would access the image this way.

Categories

Resources