EvilZone
Programming and Scripting => Java => : parad0x August 14, 2013, 06:15:46 PM
-
I was programming applets but the first applet worked whereas my second applet which asks the user to input two numbers and then prints their sum isn't working and the second problem is that even the JNLP file isn't working for any file. It says Invalid Path.
Here is my applet code:
import java.awt.Graphics;
import javax.swing.JOptionPane;
import javax.swing.JApplet;
public class AdditionApplet extends JApplet
{
private float sum;
public void init()
{
String s1 = JOptionPane.showInputDialog("Enter first Number");
String s2 = JOptionPane.showInputDialog("Enter second number");
float num1 = Float.parseFloat(s1);
float num2 = Float.parseFloat(s2);
sum = num1 + num2;
}
public void paint( Graphics g){
super.paint(g);
g.drawRect(25, 25, 250, 40);
g.drawString("The sum is: " + sum, 50, 50);
}
}
And here is my JNLP file:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp codebase=.
href="AdditionApplet.jnlp"
>
<information>
<title>Addition Applet</title>
<vendor>Oracle Corporation</vendor>
<shortcut>
<desktop/>
</shortcut>
<offline-allowed/>
</information>
<resources>
<java version="1.6+"/>
<jar href="Addition.jar" main="true"/>
</resources>
<applet-desc name="AdditionApplet"
main-class="AdditionApplet"
width="400"
height="400">
</applet-desc>
</jnlp>
-
What exactly isn't working in your first program? I haven't coded applets that much, so I can't really help.
-
"isn't working" is not an appropriate error description. You applet works fine for me.
And for the path problem: Where did you put the file exactly?
-
I corrected the paths specified but now in FF it says Your security settings have blocked a local application from running. How to run this in browser?
-
I corrected the paths specified but now in FF it says Your security settings have blocked a local application from running. How to run this in browser?
This should help you out :-
http://docs.oracle.com/javase/tutorial/deployment/webstart/deploying.html (http://docs.oracle.com/javase/tutorial/deployment/webstart/deploying.html)
http://docs.oracle.com/javase/tutorial/deployment/applet/deployingApplet.html (http://docs.oracle.com/javase/tutorial/deployment/applet/deployingApplet.html)
http://docs.oracle.com/javase/tutorial/deployment/applet/html.html (http://docs.oracle.com/javase/tutorial/deployment/applet/html.html)
Learn about it here :-
http://docs.oracle.com/javase/tutorial/deployment/applet/html.html (http://docs.oracle.com/javase/tutorial/deployment/applet/html.html)
-
This is not the answer to your problem, however it is always good practice to include the init as well as the start, destroy, and stop methods in your applet. Also you should make it implement Runnable so you can have a nice starting point (run method) for your program. The init method should have this in it if you use Runnable:
Thread t = new Thread(this);
t.start();