Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Snake X on August 07, 2011, 11:01:58 pm

Title: making an applet work
Post by: Snake X on August 07, 2011, 11:01:58 pm
This Applet is written correctly yet it will not run correctly. The only way I can view it correctly is to open the html file that has the <apple> tag in it in a Java IDE and comple it. However when I try to run it online, it gives an error. This should NOT be happening.

link: http://greyshadowencodes.webs.com/java/moonPhases.html

here is the code to the applet:

Code: [Select]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class moonPhases extends JApplet implements ActionListener {

JButton full = new JButton("Full moon");
JButton newmoon = new JButton("New moon");
JButton waxingcrescent = new JButton("Waxing Crescent");
JButton waxinggibbous = new JButton("Waxing Gibbous");
JButton waningcrescent = new JButton("Waning Crescent");
JButton waninggibbous = new JButton("Waning Gibbous");
JButton firstquarter = new JButton("First quarter");
JButton thirdquarter = new JButton("Third quarter");
JLabel label1, label2, label3, label4, label5, label6, label7, label8;
Container contentArea;
Graphics g;

public void init() {
contentArea = getContentPane();
contentArea.setBackground(Color.cyan);

FlowLayout manager = new FlowLayout();
contentArea.setLayout(manager);

full.addActionListener(this);
newmoon.addActionListener(this);
waxingcrescent.addActionListener(this);
waningcrescent.addActionListener(this);
waxinggibbous.addActionListener(this);
waninggibbous.addActionListener(this);
firstquarter.addActionListener(this);
thirdquarter.addActionListener(this);

full.setEnabled(true);
newmoon.setEnabled(false);
waxingcrescent.setEnabled(false);
waningcrescent.setEnabled(false);
waxinggibbous.setEnabled(false);
waninggibbous.setEnabled(false);
firstquarter.setEnabled(false);
thirdquarter.setEnabled(false);

contentArea.add(full);
contentArea.add(newmoon);
contentArea.add(waxingcrescent);
contentArea.add(waxinggibbous);
contentArea.add(waningcrescent);
contentArea.add(waninggibbous);
contentArea.add(firstquarter);
contentArea.add(thirdquarter);

setContentPane(contentArea);
}
public void actionPerformed(ActionEvent event) {
Image fullmoonImg = getImage(getCodeBase(),"full moon.jpg");
ImageIcon pica = new ImageIcon(fullmoonImg);

Image newmoonImg = getImage(getCodeBase(),"new moon.png");
ImageIcon picb = new ImageIcon(newmoonImg);

Image waxingcrescentImg = getImage(getCodeBase(),"waxing crescent.jpg");
ImageIcon picc = new ImageIcon(waxingcrescentImg);

Image waxinggibbousImg = getImage(getCodeBase(),"waxing gibbous.jpg");
ImageIcon picd = new ImageIcon(waxinggibbousImg);

Image waningcrescentImg = getImage(getCodeBase(),"waning crescent.jpg");
ImageIcon pice = new ImageIcon(waningcrescentImg);

Image waninggibbousImg = getImage(getCodeBase(),"waning gibbous.jpg");
ImageIcon picf = new ImageIcon(waninggibbousImg);

Image firstquarterImg = getImage(getCodeBase(),"first quarter moon.jpg");
ImageIcon picg = new ImageIcon(firstquarterImg);

Image thirdquarterImg = getImage(getCodeBase(),"third quarter moon.jpg");
ImageIcon pich = new ImageIcon(thirdquarterImg);

if(event.getSource()==full) {
newmoon.setEnabled(true);
label1 = new JLabel(pica);
contentArea.add(label1);
setContentPane(contentArea);
}
if(event.getSource()==newmoon) {
waxingcrescent.setEnabled(true);
label1 = new JLabel();
label2 = new JLabel(picb);
contentArea.add(label1);
contentArea.add(label2);
setContentPane(contentArea);
}
if(event.getSource()==waxingcrescent) {
waxinggibbous.setEnabled(true);
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel(picc);
contentArea.add(label3);
setContentPane(contentArea);
}
if(event.getSource()==waxinggibbous) {
waningcrescent.setEnabled(true);
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel(picd);
contentArea.add(label4);
setContentPane(contentArea);
}
if(event.getSource()==waningcrescent) {
waninggibbous.setEnabled(true);
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel();
label5 = new JLabel(pice);
contentArea.add(label5);
setContentPane(contentArea);
}
if(event.getSource()==waninggibbous) {
firstquarter.setEnabled(true);
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel();
label5 = new JLabel();
label6 = new JLabel(picf);
contentArea.add(label6);
setContentPane(contentArea);
}
if(event.getSource()==firstquarter) {
thirdquarter.setEnabled(true);
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel();
label5 = new JLabel();
label6 = new JLabel();
label7 = new JLabel(picg);
contentArea.add(label7);
setContentPane(contentArea);
}
if(event.getSource()==thirdquarter) {
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel();
label5 = new JLabel();
label6 = new JLabel();
label7 = new JLabel();
label8 = new JLabel(pich);
contentArea.add(label8);
setContentPane(contentArea);
}
}
}

edit: the console produced this error

Code: [Select]

Java Plug-in 1.6.0_26
Using JRE version 1.6.0_26-b03 Java HotSpot(TM) Client VM
User home directory = C:\Users\Jacob
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------


java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
at java.awt.LightweightDispatcher$3.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Title: Re: making an applet work
Post by: jnesselr on August 07, 2011, 11:11:28 pm
What is it telling you console-wise.  For me, it's not initializing the applet, which tells me either your html file to call the applet is wrong, or your initialization routine is wrong.
Title: Re: making an applet work
Post by: BlakPilar on August 07, 2011, 11:14:51 pm
What version of Java is your app targeting?
Title: Re: making an applet work
Post by: Snake X on August 07, 2011, 11:15:34 pm
ok heres my html source:

Code: [Select]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
code = "moonPhases.class"
width = "1000"
height = "1000"
>
</applet>
</center>
</body>
</html>

That was generated when i clicked 'java applet' under 'choose project type' when i created a new project.

edit: Blak, i dont think that has anything to do with it.
Title: Re: making an applet work
Post by: BlakPilar on August 07, 2011, 11:18:09 pm
edit: Blak, i dont think that has anything to do with it.
Code: [Select]
java.lang.UnsupportedClassVersionError: moonPhases : Unsupported major.minor version 51.0
Are you sure?
Title: Re: making an applet work
Post by: Snake X on August 07, 2011, 11:19:46 pm
yes im definitely sure.

Also grph, My Init routine should be fine. That is how i learned in Adv. computer science class how to create an init for an applet, so theres hopefully nothing wrong there.
Title: Re: making an applet work
Post by: jnesselr on August 07, 2011, 11:20:32 pm
lol, hey BlakPilar, go type "java -version" on a command line and tell me what it says please.
Title: Re: making an applet work
Post by: Snake X on August 07, 2011, 11:22:56 pm
I just cant seem to figure out why something like this wouldnt work.. :S I know its programmed correctly because it works when i run the html file from jcreator, but when the html file is looked at in chrome... it just doesnt run
Title: Re: making an applet work
Post by: BlakPilar on August 07, 2011, 11:24:32 pm
Version "1.6.0_21".

I just figured that since the console said that there was an unsupported class version error, it had something to do with the version. Guess my thinking is flawed, or the console is spewing out randomness.

EDIT: Snake, doesn't run in IE9, either.
Title: Re: making an applet work
Post by: Ti-Programmer on August 07, 2011, 11:27:27 pm
Try building with a '-target 1.6' - that might work

Source:
https://bugzilla.redhat.com/show_bug.cgi?id=288991
Title: Re: making an applet work
Post by: Snake X on August 07, 2011, 11:28:31 pm
Java applets are browser independent. Well no sense in derailing this topic any further.. I need to get to the bottom of this. This fix will be a reference to another new java applet im currently creating which is also similarly structured, being I just used that applet as a base to work from since i havnt created anything recently and almost forgot it completely after not using the language a lot.

edit: how do I pass arguments in the java compiler?

edit 2: ti, I'm running 1.7.. should i just change that to -target 1.7?

edit 3: this did nothing :(
Title: Re: making an applet work
Post by: BlakPilar on August 07, 2011, 11:41:40 pm
I think I read somewhere that 1.4 or 1.5 is the most common version of Java installed, but I'm not sure if that would help.
Title: Re: making an applet work
Post by: Snake X on August 07, 2011, 11:46:21 pm
current version: 1.7. This is what I updated my JDK and JRE to. As long as I am not using any functions that are not defined in a JDK version, I am fine.
Title: Re: making an applet work
Post by: BlakPilar on August 08, 2011, 12:41:38 am
No, I know that's the new version, but it's not the most commonly installed one (I think). That's what I meant.
Title: Re: making an applet work
Post by: Snake X on August 08, 2011, 08:36:32 am
I FIXED IT!!!

The reason is, java (the regular download everyone installs at java.com) is only 1.6u26 which in my case i had it compiled in jdk 1.7 whereas 1.7 =/= 1.6u26 so I reinstalled the old version and BAM it worked :D :D :D
Title: Re: making an applet work
Post by: BlakPilar on August 09, 2011, 01:22:11 am
...So I was kinda sorta right? :P