Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Simple Applet Troubles

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple Applet Troubles

    Having problems with this code, basically once the applet starts up all that is displayed is "The sum is 0.0". The Option panes are not poping up at all.
    Java 1.7 / Eclipse.


    Also getting a warning @ public class Practice : The serializable class Practice does not declare a static final serialVersionUID field of type long.
    If anyone knows what the means that would be super helpful, Any help is much appreciated.

    I went ahead and uploaded the applet : http://Pixeltainment.comule.com

    import java.awt.*;
    import javax.swing.*;
     
    public class Practice extends JApplet {
    	// What the Init method is .. is basically being about to put stuff ... in it ?
    	private double sum;
     
    	public void inti(){
    		// Init() is short for initialization
    		String fn = JOptionPane.showInputDialog("Enter first Number");
    		String sn = JOptionPane.showInputDialog("Enter second number");
     
    		// Convert the string to double
    		double n1 = Double.parseDouble(fn);
    		double n2 = Double.parseDouble(sn);
     
    		sum = n1 + n2;
     
     
     
     
    	}
    	public void paint(Graphics g){
    		super.paint(g);
    		g.drawString("The sum is "+sum, 25 , 50);
    	}
    }
    Last edited by Veldimare; August 21st, 2012 at 08:47 PM. Reason: Additional Information


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Applet Troubles

    Your code doesn't have a valid init() method -- check your spelling:

    // this should be spelled "init"
    public void inti() { 
       // ...
    }

    Don't forget to use @Override annotations to make sure that the methods you think you are overriding you actually are overriding:

    @Override
    public void inti() {  // this will cause a compilation error until the spelling is fixed.
       // ...
    }

    Also you'll want to draw in the paintComponent method of a JComponent such as a JPanel rather than draw in the paint method of a JApplet or any other top-level window.
    Last edited by curmudgeon; August 21st, 2012 at 09:09 PM.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Veldimare (August 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Applet Troubles

    .. Thank you first off. I am kind of in one of those facepalm moments now, Also thank you for the tips with @Override and the JComponent. Really appreciate it.

    While we are on the subject, If you don't mind would you be able to go into detail about JComponent / JPanal vs JApplet ?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Applet Troubles

    Quote Originally Posted by Veldimare View Post
    Also getting a warning @ public class Practice : The serializable class Practice does not declare a static final serialVersionUID field of type long.
    If anyone knows what the means that would be super helpful, Any help is much appreciated.
    Your Practice class is extending JApplet, a class that implements the Serializable interface. This means that Practice by extension also implements this interface, and the interface's contract stipulates that classes that implement it should declare and initialize a static final serialVersionUID long variable. This variable is useful for if you want to serialize or de-serialize objects since the version ID will let you know if the objects you are dealing with are the correct expected version. For instance you wouldn't want to de-serialize a version 1.0 object into code that uses the 2.0 version of your class.

    But having said all this, I'm going to suggest you ignore it all since it is *very* rare that you'll ever want to serialize a class that extends JApplet or any Swing component. Rather if you use serialization, you'll likely want to serialize the logical part of your program, not its GUI representation. I suggest that you place the @SuppressWarnings("serial") annotation just above your Practice class declaration which will tell the compiler not to give you this warning.

  6. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Applet Troubles

    Quote Originally Posted by Veldimare View Post
    .. Thank you first off. I am kind of in one of those facepalm moments now, Also thank you for the tips with @Override and the JComponent. Really appreciate it.
    You're quite welcome!

    While we are on the subject, If you don't mind would you be able to go into detail about JComponent / JPanal vs JApplet ?
    When you paint in a top-level window's paint method, you are painting over not just the work portion of the GUI but also its borders and child components, and this can result in some unintended side effects. Also since the paint method of a top-level window can only use drawing code from AWT (they are so-called "heavy-weight" components), you lose some of the benefits of Swing painting including its default double-buffering. This means that animations drawn in a JPanel's paintComponent method will likely flow much more smoothly than the same animation drawn in the paint method of a JFrame or JApplet. My classes that extend JApplet are usually very very small and only have code to create the GUI (usually code that creates a JPanel) and then places the GUI in my applet's contentPane, and that's it.

  7. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Applet Troubles

    Awsome, Thank you very much. It makes more sense now. Alright back to studying.

  8. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Applet Troubles

    Best of luck!

  9. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Applet Troubles

    Quote Originally Posted by bruizer View Post
    Well figured it states applet troubles / and creating new threads and clogging up others posting I figured to keep it within the same category or topic / thanks though no worries.
    But your question has nothing to do with the original question and so really doesn't belong in his thread.

    I'll just keep trying on my own thx.
    No worries, but if you get stuck, again consider creating a new question in this forum. It's not hard to do and it costs nothing.

Similar Threads

  1. Replies: 12
    Last Post: May 8th, 2012, 05:22 PM
  2. Painfully simple Applet problem.
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 4th, 2012, 08:09 AM
  3. Created simple game applet but wanting it to access a DSN on the server
    By hirsty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 21st, 2011, 03:11 AM
  4. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM
  5. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM