I have an applet with some buttons in it, the buttons have image icons. I also have made an HTML file with this applet. Whenever I open this page from server (Apache tomcat) an exception occurs:
java.lang.reflect.InvocationTargetException.
But if I run without the icons, there are no problems. Can anyone help me, so that I can load the applet with the button icons?
Image myImage = getToolkit().createImage("image/REC1.jpg");
ImageIcon myIcon = new ImageIcon(myImage);
button.setIcon(myIcon);
Toolkit.createImage(String):
Returns an image which gets pixel data from the specified file. The returned Image is a new object which will not be shared with any other caller of this method or its getImage variant.
This method first checks if there is a security manager installed. If so, the method calls the security manager's checkRead method with the specified file to ensure that the image creation is allowed.
I made 2 parts bold:
File - only ever points to the local file-system of the computer on which the applet is running. So are unsuitable for icons in the applet - they must be on the server. And..
Security Manager - which will not allow a sand-boxed applets to read or write to the local file-system. That might be the important part of the stack trace that is missing.
Applet resource access
Applets need to load resources from the 'home server' if sand-boxed, by URL.
The icons might be an embedded resource, inside a Jar referenced in the archive attribute of the applet element, but if they are loaded to the server as images, they can be accessed relative to the document base or code base. Here is what it might look like for an image named REC1.jpg in the image sub-directory of the directory that contains the HTML.
URL url = new URL(getDocumentBase(), "image/REC1.jpg");
Image myImage = getToolkit().createImage(url);
you can use .PNG file format also. and used small letter for file name.
Related
I have an applet, and need to open a stream to a file. This file is a local file located where the applet and HTML file are:
URL localURL = new URL(getCodeBase(), "pixs/icons.zip");
InputStream localInputStream =localURL.openStream();
It used to work fine, but after upgrading to java 1.7 build 25, getCodeBase() always return null.
This is actually documented!, Alas - there is no recommendation how to overcome it.
One things that had worked is to use full path:
URL localURL = new URL("file:c:/myFolder/pixs/icons.zip");
Is there another option to resolve that without using full path?
Perhaps you can use getDocumentBase instead. Depends on the structure of your setup, whether there is a close relation between the code base and the documents. No permanent solution: getDocumentBase was modified in the same way in 7u40, according to bug #8019177.
If not, then you can try using getResource to obtain a URL from your JAR, e.g. the class file of the applet, and then disassemble that URL to get at the location of the JAR and hence the code base. This is untested, so feel free to edit this post if you tried this.
Last but not least, since that change only affects local applets, you could run a (local or public) web server to serve that applet.
If you want a more official statement on this, I'll quote the #8017250 bug report:
If applet need to load resource:
if the resource is in applet JAR(s), they should be able to load it with ClassLoader.getResoruceAsStream directly, without needing the codebase information.
if the resource is in arbitary location, not inside applet JAR, they should have other ways to get to that location, since it's not part of the applet resource anyway. (e.g. user.home java system property, provided that their applet has all-permissions)
http://www.duckware.com/tech/java-security-clusterfuck.html (thanks to this post) mentions some other alternatives. The least likely one to be affected by future Oracle modifications apprers to be the use of location.href in the containing HTML page, e.g. writing the <applet> tag from JavaScript.
I'm going to make a java application with nifty-gui[1] using java web start
When user click on upload image, a file chooser is showed (FileOpenService)
After that, image should be visible in image control.
Unfortunately, nifty-gui can use images only from assets.
How to download this image into asset? Maybe there is a way to modify actual image using data from FileContent class?
This is solution, created after discussion [1]
assetManager.registerLoader(AWTLoader.class, "jpg");
assetManager.registerLocator("/", FileLocator.class);
image(new ImageBuilder() {{
filename("/home/marek/photo2.jpg");
}});
I'm using JFreeChart to generate a dynamic chart depending on the user input. I have a JSP with some textbox and combobox, the user makes the input and submits it, and the Action process it, generating an image of a chart. I need to display this image on the same JSP as before, below the textbox/combobox.
If I use response.setContentType("image/jpeg"); etc... then I get a page with the image alone. I thought of saving the image to a file and then access it with <img >, but I'm not sure that will work (need to save it to WebContent and I may not be able to access it always?).
Is there a way to somehow cache the image and then access it inside the JSP through an <img> or something? Maybe JFreeChart has an easy way to do what I want?
If it matters, I'm also using struts and spring on my webapp.
Thanks in advance.
I've not tried it, but you might look into org.jfree.chart.imagemap and a suitable URL generator from org.jfree.chart.urls. An outline of implementing a PieURLGenerator is illustrated here.
Well, if you generate the image on the server side, you could always just store it in a temp directory using something like a UUID to generate a unique name for it, and concatenating the image file extension on the end of it.
Make sure that the directory the image is generated is accessible on the webserver, and then send the URL path to the image file on the server back to the JSP using ajax (Direct Web Remoting), for display using Javascript.
Just make sure you also have a chron job or service to clear the older files out of the directory now and again.
You should have a servlet that can create the image you want solely from the URL. The URL can then contain an id, which maps back to an object in your program containing raw data in memory. The servlet then generates the image and returns it.
You can then simply set the url of the image in your current web page in Javascript, and it should be loaded.
This is because JSP's are character oriented which do not lend well to binary data so you need to have a servlet do it.
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?
I have a java applet that creates a JPEG file. I want to pass that file to a Javascript where it can display and print it. The only way I can think of doing this is to save the jpeg to a temporary storage area on the user's computer and then pass the path of the file to the javascript which picks it up and displays it. This raises a two questions:
Where should the applet store the file. If you suggest the temporary internet files folder, then how do I find that path to that folder?
Is there a better way to do this? Can I pass the JPEG directly from java to javascript without first writing out to a disk?
Thank you in advance for your help.
To store file on users's machine your applete should be signed, and user should give necessary permissions to your applet (through special dialog window which is shown automatically).
Read this article about modifying DOM from applet
Another approach is to save your image on the server (pass it from your applet to the server) and then reload page (or use Ajax, but in this case you probably have to make ajax calls every few seconds to check if the image is available on the server).
Can't you just have an applet that displays the picture and prints it?
I don't think it'd be possible to do this in IE before IE8 (and it's wimpy even in IE8), but in other browsers your applet could make the image data available to Javascript (please don't say, "a Javascript"; it's like saying, "a FORTRAN" or "a Java") and then from Javascript you could create an <img> tag with a "data URI". See this reference: http://en.wikipedia.org/wiki/Data_URI_scheme