10 Replies - 4340 Views - Last Post: 19 January 2009 - 08:20 PM Rate Topic: -----

#1 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Cant get this to run java applet

Posted 19 January 2009 - 04:18 PM

as thetitle says i cant get this to run no errors pop up but the pics dont wanna show.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.net.URL;
import java.applet.*;
import java.awt.Graphics.*; 

public class FarmAnimal extends JApplet {
	private String images[] = {"bird.gif", "cat.jpeg", "cow.jpeg","dog.jpeg", "duck.jpeg", "frog.jpeg", "horse.jpeg","pig.jpeg", "rooster.jpeg", "sheep.jpeg"};
	private String myWords[] = {"Bird", "Cat", "Cow", "Dog", "duck", "frog", "horse", "Rooster", "Sheep"};
	private ImageIcon icon[];
	
   
	public void init() {
	
	

   
 
	this.setSize(800, 500);
	this.setVisible(true);
   	
   	icon = new ImageIcon[images.length];
	for(int i = 0; i < images.length; i++)
	icon[i] = new ImageIcon(images[i]);
	}
	
	public void paint(Graphics g)
	{
	super.paint(g);
	for(int i = 0; i < images.length; i++)
	g.drawImage(icon[i].getImage(), 10, 10, icon[i].getImageObserver());
	
	

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Cant get this to run java applet

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Cant get this to run java applet

Posted 19 January 2009 - 04:59 PM

When referring to your icon variable, prepend it with "this" on the front so that Java knows you are referring to the one in your class. (Just like you did for setVisible etc) Otherwise change the name of your class variable from "icon" to something else like "iconArray". I believe you are having a naming conflict with one of the libraries you are importing there.

Once you add "this" keyword when referring to the variable in your class or changing the variable name, you will see it start working again.

Keep in mind also when graphics paint. Right now the graphics will draw whenever paint is triggered which is when the window changes size or a refresh. Not when other windows move over the top of your picture.

:)
Was This Post Helpful? 0
  • +
  • -

#3 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 05:04 PM

i keep getting this error what could that mean. i looked in my directory and all those exist


 
GE2001 version 1.4 - Copyright 2008 Xinox Software
Error : Invalid path, "C:\Program Files\Java\jdk1.6.0_10\bin\java.exe" -classpat
h "C:\Program Files\Java\jdk1.6.0_10\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6
.0_10\lib\dt.jar;C:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;C:\Program File
s\Java\jdk1.6.0_10\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_10\jre\l
ib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_10\jre\lib\ext\sunjce_provi
der.jar;C:\Program Files\Java\jdk1.6.0_10\jre\lib\ext\sunmscapi.jar;C:\Program F
iles\Java\jdk1.6.0_10\jre\lib\ext\sunpkcs11.jar"
Press any key to continue...


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Cant get this to run java applet

Posted 19 January 2009 - 05:16 PM

It means that you have an invalid class path, the path(s) Java uses to find various files including libraries, compilers and runtime environment files. I take it you are using JCreator? Look up JCreator's help files on how to setup the path. If you have deleted any folders or files from those paths, you might want to put them back.

:)
Was This Post Helpful? 0
  • +
  • -

#5 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 05:17 PM

but i havent touched any files in java in months from when i first set it up
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Cant get this to run java applet

Posted 19 January 2009 - 05:35 PM

Try putting your code in a notepad file and compile it with javac and see if that works for you.

1) Copy the code, put it into notepad.
2) Save the file as FarmAnimal.java in the directory where you have installed Java
3) Open a command prompt (go to Run and type "cmd")
4) Change directories to where you saved the file and where Java is installed.
5) Type "javac FarmAnimal.java" If everything is successful, the prompt should come back up without printing anything.
6) Then put the class in an html file and run the file using appletviewer or your browser.

If this procedure works then it is your IDE that is the problem and is misconfigured somewhere. If you get errors here about the classpath as well, then your JDK/JRE classpath is not setup correctly.

:)
Was This Post Helpful? 0
  • +
  • -

#7 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 05:46 PM

when i do it nothing happens just skips a line and goes back to the destination lcation
Was This Post Helpful? 0
  • +
  • -

#8 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 05:54 PM

Java Plug-in 1.6.0_10
Using JRE version 1.6.0_10 Java HotSpot(tm) Client VM
User home directory = C:\Users\George
----------------------------------------------------
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>
----------------------------------------------------


Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
	at FarmAnimal.paint(FarmAnimal.java:29)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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)
java.security.AccessControlException: access denied (java.io.FilePermission Images/bird.jpeg read)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkRead(Unknown Source)
	at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
	at sun.awt.SunToolkit.getImage(Unknown Source)
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at FarmAnimal.init(FarmAnimal.java:22)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.io.FilePermission Images/bird.jpeg read)

 

thats the error i get when i run it

This post has been edited by Roemeo: 19 January 2009 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 06:37 PM

Your setVisible() and setSize()
will call paint();

initialize your IconImage before calling these 2
Was This Post Helpful? 0
  • +
  • -

#10 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 07:03 PM

Java Plug-in 1.6.0_10
Using JRE version 1.6.0_10 Java HotSpot(TM) Client VM
User home directory = C:\Users\George

java.security.AccessControlException: access denied (java.io.FilePermission Images/bird.jpeg read)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkRead(Unknown Source)
	at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
	at sun.awt.SunToolkit.getImage(Unknown Source)
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at FarmAnimal.init(FarmAnimal.java:21)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.io.FilePermission Images/bird.jpeg read)

 


ok now im getting a diff error not to sure what it means though
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Cant get this to run java applet

Posted 19 January 2009 - 08:20 PM

Applet can only read from where they are coming

Assuming your image file are in the same directory then your Applet you will have to do something like that

URL url = getCodeBase();
for(int i = 0; i < images.length; i++) {
	ImageIcon[i] = new ImageIcon(getImage(url, images[i]));
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1