Results 1 to 8 of 8
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Difference between Applet and JApplet

    This program will not set the background color to black unless I replace the "extends JApplet" with "extends Applet" but why? what's the difference between them? (between JApplet and Applet) Also, when I hover my mouse over "Applet7" in the class header, I get this message: "The serializable class Applet7 does not declare a static final serialVersionUID field of type long." I'm using Eclipse. This is the code:

    Java Code:
    import javax.swing.JApplet;
    import java.awt.*;
    
    public class Applet7 extends JApplet {
    
    	private final int APPLET_WIDTH = 400;
    	private final int APPLET_HEIGHT = 400;
    	
    	private int[] xRocket = {261,216,216,194,194,324,324,302,302};
    	private int[] yRocket = {96,171,331,351,387,387,351,331,171};
    	
    	public void init() {
    		setBackground(Color.black);
    		setSize(APPLET_WIDTH,APPLET_HEIGHT);
    	}
    	public void paint(Graphics applet7) {
    
    		applet7.setColor(Color.cyan);
    		applet7.fillPolygon(xRocket, yRocket, xRocket.length);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    How? I replaced the paint(Graphics applet7) method with this:

    Java Code:
    	public void paintComponent(Graphics applet7) {
    		applet7.setColor(Color.cyan);
    		applet7.fillPolygon(xRocket, yRocket, xRocket.length);
    
    	}
    And nothing appears now :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're still trying to paint directly in the JApplet, only now it's worse, because JApplets don't even have a paintComponent method.

    Again, you need to paint in a JPanel
    Again, you need to override the JPanel's paintComponent method
    And again this JPanel needs to be the contentPane of the JApplet (or else added to the JApplet's contentPane BorderLayout.CENTER).

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    e.g.,
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Fu2Applet extends JApplet {
       private final int APPLET_WIDTH = 400;
       private final int APPLET_HEIGHT = 400;
    
       public void init() {
          try {
             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                   createGUI();
                }
             });
          }
          catch (Exception e) {
             System.err.println("createGUI didn't successfully complete");
          }
       }
    
       private void createGUI() {
          // the Fu2 JPanel can either be the contentPane or added to the contentPane
          getContentPane().add(new Fu2());
          setSize(APPLET_WIDTH,APPLET_HEIGHT);
       }
    }
    
    @SuppressWarnings("serial")
    class Fu2 extends JPanel {
       private int[] xRocket = {261,216,216,194,194,324,324,302,302};
       private int[] yRocket = {96,171,331,351,387,387,351,331,171};
       
       public Fu2() {
          //setBackground(Color.black);
       }
       
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(Color.cyan);
          g.fillPolygon(xRocket, yRocket, xRocket.length);
       }
    }
    Last edited by Fubarable; 01-11-2010 at 03:50 AM. Reason: Added super.paintComponenet(g)

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Well, this just shows I need to step back because most of it does not make any sense if you look at it through my eyes. I got questions on the stuff that is not so obscure:

    javax.swing.SwingUtilities.invokeAndWait
    ^ y not New Thread? Y even make a thread?

    How is the paintComponent method instantiated? It's never called.

    The ideal question: What is the difference between JApplet and Applet?
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    javax.swing.SwingUtilities.invokeAndWait
    ^ y not New Thread? Y even make a thread?
    Swing components should be created on the EDT, and JApplets should not complete init until the Swing components have been fully created. The invoke and wait assures that this will happen. Also, it says to do this in the applet tutorial which you should read.

    How is the paintComponent method instantiated? It's never called.
    Please read this article as it explains this far better than I can: Painting in AWT and Swing

    The ideal question: What is the difference between JApplet and Applet?
    I'm still learning Swing, so I don't know all the differences, not even half the differences, but mainly one is a Swing app and the other AWT. One uses heavy weight components, the other light weight.

  8. #8
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Ah okay. Honestly, I don't think I should get involved with Swings, GUI, JApplets just yet. At least not until I learn the core of Java Applications (Polymorphism, Generics, Advance Concurrency). Reason I was exploring this is because the textbook on Java supplied by my school has a 1 section on graphics for every chapter and I like to look at those occasionally.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. JApplet on MAC
    By ld_pvl in forum Java Applets
    Replies: 0
    Last Post: 08-23-2009, 02:40 PM
  2. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM
  3. Applet Vs JApplet ?? which is better for animation?
    By Mba7eth in forum New To Java
    Replies: 3
    Last Post: 09-09-2008, 05:55 PM
  4. How to clear cache of an Applet/JApplet
    By tanmoy.b81 in forum AWT / Swing
    Replies: 0
    Last Post: 08-11-2008, 12:54 PM
  5. Database to JApplet
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 06:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •