I am trying to load an image into my applet with the following source code:
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class plum extends JApplet {
Image img;
public void init() {
img = getImage(getCodeBase(), "waves2.jpg");
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
g.drawString("I am in the Applet", 35, 15);
}
}
I am using the policytool program to try and allow the applet to "read" the jpeg image file and show it in my web browser.
At the moment I receive the following error when trying to execute my applet.
I tried to configure with the policytool but I still receive the same error when running the applet. I set the codebase in the policy tool to the directory my html file is in that launches the applet. I used the file:///dir/dir1/dir2 type of syntax instead of http:// or https:// for the CodeBase field in the policy tool.
Does anyone know how I can fix this?
UPDATE
I have configured the policytool like so:
I have also configured it so that the codebase has all the permissions. In either case the picture does not show up, but the error is not thrown. Maybe I fixed it I really do not know. The picture does not show up but the error message is no longer shown saying that there is a permissions problem with reading the file.
Related
I'm on my first week using Java. I don't know very much about applets, and I'm trying to learn more using a book. I've already done some applets that contain simple animation, but when I tried drawing an image I've got stock on this:
access denied( "java.io.FilePermission" "[image]" "read").
Anyone who can help me?
import java.awt.Graphics;
import java.awt.Image;
public class Wave extends java.applet.Applet{
Image waveimg;
public void init(){
waveimg=getImage(getCodeBase(),"wave.jpg");
}
public void paint(Graphics g){
g.drawImage(waveimg, 10,10,this);
}
}
This is because Applets need permissions to read/write from/to files. Maybe this page can help you: How Can An Applet Read Files On The Local File System
You have to give your applets the permission to read/write the images.
Refer this link to grant access to it.
http://docs.oracle.com/javase/tutorial/security/tour1/wstep2.html
I have a JFrame application working nicely. However now I'd like to run it on the web as an Applet. This is what I've done:
import MyPackage.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MyName extends JApplet
{
public void init() {
setSize(600,450);
new MyName()
}
public MyName() {
JShellFrame frame = new JShellFrame(true, null, null);
frame.setVisible(true);
}
}
How can I make an html file to run this applet? Also, I have an external jar file that the applet will need. Does the applet not need a main method?
Check out Getting Started With Applets. It covers relevant methods and life cycle. It mentions main method as well:
Unlike Java applications, applets do not need to implement a main
method.
Deployment section covers HTML file details. For dependency jars you can specify more than one jar in archive attribute of applet tag.
However now I'd like to run it on the web..
Then drop this nonsense and launch the frame from a link using Java Web Start. I say 'nonsense' for two reasons.
JWS has existed since Java 1.2, & has been discussed in these forums several times in the last few days in regard to applets. Seems you are not doing much research.
Of the 'gargantuan' 2 lines of applet code code shown above, one of them is ill-advised and the other is either pointless or would risk creating a stack overflow error (could not be bothered trying it to find out which).
here's some html that will work:
<applet archive = "appName.jar" width = 900 height = 506 code = "main.class"/>
You can change the width and height according to the size your app needs. Also make sure to put the url as a path to the jar.
EDIT: To those idiots who negged this question merely over the title, the button is clearly meant to be pressed if the question is out of the blue, without any effort put into it at all. I have researched and I have asked, and I have tried. All I am asking for is help.
It's hardly necessary to say just how much work I have put into trying to find a solution to my problem - I have asked questions, Googled, read documents, you name it, but all to no avail.
What I want to do is something I thought I could figure out within minutes: How to run images with JApplet, and use these images in the paint(Graphics g) function. I am running the JavaProject.html file from the build (also known as bin) folder, and it is from the file system, not HTTP. I did not include the "package" line in my code as well.
A recap of my journey is that the following have not worked for me:
This is my HTML file:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="500" height="600">
</applet>
</body>
</html>
This method gives me the "Access Denied" "java.io.FilePermission" "Image.jpg" "read" Error. Needless to say, trying to work with images on a website does not work either. This one is one of the more frustrating ones because it works to with other people, yet not for me.
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image image;
public void init()
{
image=getImage(getDocumentBase(),"/Space.gif");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,20,20,this);
}
}
So that one didn't work. They suggested the getResourceAsStream method, so I tried that.
Image image;
Exception lastException=null;
try
{
image=ImageIO.read(getClass().getResourceAsStream("/Space.gif"));
}
catch (IOException ex)
{
lastException=ex;
}
But this one ended up giving me the "Illegal Argument Exception" input=null! Error.
This is my file arrangement: http://oi61.tinypic.com/5ohydc.jpg
Most other methods do not really work or are just far too complex to write down here, but none seem to work. I ask then, is my last resort just to get this thing signed? I have no idea how to go about doing that, and I think it's ridiculous that I even have to go through the trouble just to display images on my JApplet.
I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience, but I would really appreciate any help. I am new to Java, so I can't really discuss much technically, but I pick up from examples rather quickly.
The first method you attempted does work. The only change you need to make is to remove the slash ( / ) before the file name you are attempting to use. Here's your original code (which works fine) with that one character removed:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image image;
public void init()
{
image = getImage(getDocumentBase(),"Space.gif");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,20,20,this);
}
}
Note that in order for this to work, your 'space.gif' image must be in the root directory of your project, alongside the Java file that references it, like in this picture:
See how the image is in the 'default package' directory where my JavaProject.java file is also located? Use the code above, make sure your image is in the right place, and your program will run successfully. Here's mine in a running state:
The reason that the second attempt is failing is that it can't find the resource at the path you gave. That causes getResourceAsStream to return null which then results in the exception message that you saw.
You say that the error message in the first case was "Access Denied" "java.io.FilePermission" "Image.jpg" "read" or something. But according to the code, you are not trying to load "Image.jpg". That might be the root of your original problem.
A related issue is that in the getResourceAsStream version, you are using the wrong absolute path. You've used the path "/Space.gif", but given where you have put the file, it should be "/javaproject/Space.gif". Alternatively, since you are calling getResourceAsStream as on a class in the same (javaproject) package that contains the file, you could also use a relative path; i.e. "Space.gif".
I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience ...
Actually, the real solution is to carefully read the documentation for the classes / methods you are using, rather than replying on random examples you found on the internet.
I am creating an windows desktop swt application.
I need to change the frame icon, for that I used
frame.setIconImage((new ImageIcon("C:\\Documents and Settings\\arjuns\\Desktop\\logo1 copy.png")).getImage());
The icon is displaying when I manually run the code from eclipse, but when I create an installer using Install4j the icon is not appearing.
Can anyone please help me.
URL url = ClassLoader.getSystemResource("ressources/logo.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
setIconImage(img);
This is similar to the previous answer, but I need to add a bit of information.
You can still use a direct path to your image (C:/User/logo.png) BUT imagine you give your program to someone else, he wont have the image in that specific path.
So I recemmend you insert it in your project like so:
(I usualy do a sperate package for any ressources).
so it will become ressources/logo.png and it will work for anybody opening your project.
import java.awt.*;
import javax.swing.*;
class set extends JFrame
{
set()
{
setSize(100,100);
setVisible(true);
setIconImage(new ImageIcon("navbit-home.png").getImage());
}
public static void main (String[] args) {
new set();
}
}
please set appropriate path.like C:/Documents and Settings/arjuns/Desktop/logo1copy.png
The image should be available in the JAR file you create. Then use getResource() to get the image from the jar file.
For example,
URL resource = this.getClass().getResource("resources/logo.png");
frame.setIconImage(new ImageIcon(resource).getImage());
Here the logo.png is located under 'resources' folder of the class file where this code is executed.
OK so I started with this:
import java.awt.*;
import java.applet.*;
public class Oval extends Applet {
public void paint (Graphics g)
{
g.setColor(Color.red);
g.fillOval(50,50,70,70);
}
}
Saved it as Oval.java. Then I ran javac Oval and it spits out the class file. Linked the class file to my HTML and all was good. Then I tried to change the color of the oval to say Color.blue and everything was not so good. I just changed the code in the Oval.java file, then re-ran the javac Oval and the result was a "new" class file... but it's the output is the exact same.
Do I have to "reset" the memory space or something? I have tried for some time to get the answer but I simply lack the vocabulary to accurately ask the question.
Sounds like the browser is caching the class file. I would try
Running the applet using the Applet Viewer
If that works, then it's a browser caching issue (clear your browser's cache)
But if it doesn't look different, or like you want in the Applet Viewer, then it must be something in your local environment (either it's not building the source file you want, or the class is going somewhere unexpected)
It could be that your browser is caching the previous version of your applet. Try clearing / disabling your cache.
Maybe the old version is in your browser-cache? Try hitting the follwing keys on your page:
Windows: ctrl + F5
Mac/Apple: Apple + R or command + R
Linux: F5
You don't have to reset the java environment, ever.
You might have a browser window open, and your browser is attempting to be "smart" avoiding reload of a page which is already "fetched". Depending on the browser, you will need to learn how to clear the browser cache.