Results 1 to 8 of 8
- 01-10-2010, 11:44 PM #1
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); } }
-
When coding a japplet, do your painting in a jpanel (the content pane) in its paintComponent method.
- 01-11-2010, 01:13 AM #3
How? I replaced the paint(Graphics applet7) method with this:
And nothing appears now :(Java Code:public void paintComponent(Graphics applet7) { applet7.setColor(Color.cyan); applet7.fillPolygon(xRocket, yRocket, xRocket.length); }
-
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).
-
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 02:50 AM. Reason: Added super.paintComponenet(g)
- 01-11-2010, 03:07 AM #6
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)
-
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.
Please read this article as it explains this far better than I can: Painting in AWT and SwingHow is the paintComponent method instantiated? It's never called.
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.The ideal question: What is the difference between JApplet and Applet?
- 01-11-2010, 03:48 AM #8
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
-
JApplet on MAC
By ld_pvl in forum Java AppletsReplies: 0Last Post: 08-23-2009, 01:40 PM -
JFrame to JApplet or JApplet to JApplet
By ramesh.8189 in forum AWT / SwingReplies: 13Last Post: 02-08-2009, 06:14 AM -
Applet Vs JApplet ?? which is better for animation?
By Mba7eth in forum New To JavaReplies: 3Last Post: 09-09-2008, 04:55 PM -
How to clear cache of an Applet/JApplet
By tanmoy.b81 in forum AWT / SwingReplies: 0Last Post: 08-11-2008, 11:54 AM -
Database to JApplet
By Preethi in forum New To JavaReplies: 0Last Post: 03-26-2008, 05:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks