Author Topic: Applet Problem  (Read 2638 times)

0 Members and 2 Guests are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Applet Problem
« on: 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:

Code: (Java) [Select]
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:
Code: (xml) [Select]
<?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>

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Applet Problem
« Reply #1 on: August 14, 2013, 10:14:17 pm »
What exactly isn't working in your first program? I haven't coded applets that much, so I can't really help.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Applet Problem
« Reply #2 on: August 15, 2013, 09:42:18 am »
"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?

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Applet Problem
« Reply #3 on: August 15, 2013, 09:53:31 am »
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?

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: Applet Problem
« Reply #4 on: August 15, 2013, 10:22:42 am »
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Applet Problem
« Reply #5 on: September 08, 2013, 08:19:03 pm »
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:

Code: [Select]
Thread t = new Thread(this);
t.start();
« Last Edit: September 08, 2013, 08:19:45 pm by Matriplex »
\x64\x6F\x75\x65\x76\x65\x6E\x00