Hey guys for some reason my code displays in the applet viewer in jgrasp and in eclipse, but for some reason it wont display via an html file in any browser. Any Ideas? Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class FamilyTreeTabbedPane extends JApplet implements ActionListener, MouseListener{
	//Make buttons and labels local to the class so that they can be accessed by all methods, and the action performed method
	JButton gButton, gTwoButton, gThreeButton,pButton, pTwoButton, pThreeButton,sButton, sTwoButton, sThreeButton,gPlay,gStop,pPlay,pStop,sPlay,sStop;
	JLabel grandText, parentText, siblingText;
	//create audio clips
	AudioClip gac,pac,sac;
	JTabbedPane tabPane;
	public void init(){
		//initialize my jtabbedpane
	    tabPane = new JTabbedPane();
		//set up tabpane mouse listener to listen for when clicked event
		tabPane.addMouseListener(this);
		//set size of the applet
		setSize(1400, 610);
		//create a new tab with an icon and the appropriate returned panel
		tabPane.addTab("Grandparent",new ImageIcon("grandIcon.jpg"), grandparentsTab());
		tabPane.addTab("Parents",new ImageIcon("parentIcon.jpg"),parentTab());
		tabPane.addTab("Siblings",new ImageIcon("siblingIcon.jpg"),siblingTab());
		//set the color of the tabbed panels
		tabPane.setBackgroundAt(0, Color.CYAN);
		tabPane.setBackgroundAt(1, Color.GREEN);
		tabPane.setBackgroundAt(2, Color.magenta);
		//add tabPane to the applet
		add(tabPane);
	}
//I normally would not have created three different method names, It adds alot of tedious work that can easily be circumvented by overloading the same one
//but in the instructions it said create three seperate methods for each tab, and I wasn't sure if method overloading was allowed, so I played it safe.	
	public JPanel grandparentsTab(){
		//create jpanel and Button
		JPanel grand, borderFrame, audioPanel;
		//setup audio clip
		gac = getAudioClip( getCodeBase(),"make my dreams come true.wav");
		//create jButtons
		gButton = new JButton(new ImageIcon("Grandparent.jpg"));
		gTwoButton = new JButton(new ImageIcon("Grandparent2.jpg"));
		gThreeButton = new JButton(new ImageIcon("Grandparent3.jpg"));
		gPlay = new JButton("Play");
		gStop = new JButton("Stop");
		//create jLabel
		grandText = new JLabel("               ",JLabel.CENTER);
		//create jpanels
		grand = new JPanel();
		audioPanel=new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		//add action listeners
		gButton.addActionListener(this);
		gTwoButton.addActionListener(this);
		gThreeButton.addActionListener(this);
		gPlay.addActionListener(this);
		gStop.addActionListener(this);
		//add to panel
		grand.add(gButton);
		grand.add(gTwoButton);
		grand.add(gThreeButton);
		audioPanel.add(gPlay);
		audioPanel.add(gStop);
		//add items to borderFrame
		borderFrame.add(grand,BorderLayout.CENTER);
		borderFrame.add(grandText,BorderLayout.NORTH);
		borderFrame.add(audioPanel,BorderLayout.SOUTH);
		//return to panel
		return borderFrame;
	}
	//refer to grandTab
	public JPanel parentTab(){
		JPanel parent, borderFrame, audioPanel;
		pac = getAudioClip( getCodeBase(),"sweet disposition.wav");
		pButton = new JButton(new ImageIcon("Parents.jpg"));
		pTwoButton = new JButton(new ImageIcon("Parents2.jpg"));
		pThreeButton = new JButton(new ImageIcon("Parents3.jpg"));
		pPlay = new JButton("Play");
		pStop = new JButton("Stop");
		parentText = new JLabel("                    ",JLabel.CENTER);
		audioPanel=new JPanel();
		parent = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		pButton.addActionListener(this);
		pTwoButton.addActionListener(this);
		pThreeButton.addActionListener(this);
		pPlay.addActionListener(this);
		pStop.addActionListener(this);
		parent.add(pButton);
		parent.add(pTwoButton);
		parent.add(pThreeButton);
		audioPanel.add(pPlay);
		audioPanel.add(pStop);
		borderFrame.add(parent,BorderLayout.CENTER);
		borderFrame.add(parentText,BorderLayout.NORTH);
		borderFrame.add(audioPanel,BorderLayout.SOUTH);
		return borderFrame;
	}
	//refer to grandTab
	public JPanel siblingTab(){
		JPanel sibling, borderFrame, audioPanel;    
		sac = getAudioClip( getCodeBase(),"shes got you high.wav");
		sButton = new JButton(new ImageIcon("siblings.jpg"));
		sTwoButton = new JButton(new ImageIcon("siblings2.jpg"));
		sThreeButton = new JButton(new ImageIcon("siblings3.jpg"));
		sPlay = new JButton("Play");
		sStop = new JButton("Stop");
		siblingText = new JLabel("               ",JLabel.CENTER);
		audioPanel=new JPanel();
		sibling = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		sButton.addActionListener(this);
		sTwoButton.addActionListener(this);
		sThreeButton.addActionListener(this);
		sPlay.addActionListener(this);
		sStop.addActionListener(this);
		sibling.add(sButton);
		sibling.add(sTwoButton);
		sibling.add(sThreeButton);
		audioPanel.add(sPlay);
		audioPanel.add(sStop);
		borderFrame.add(sibling,BorderLayout.CENTER);
		borderFrame.add(siblingText,BorderLayout.NORTH);
		borderFrame.add(audioPanel,BorderLayout.SOUTH);
		return borderFrame;
	}
	public void actionPerformed(ActionEvent event) {
		Object src = event.getSource();
		
		if(src == gButton)
			grandText.setText("This is my grandma!");
		else if(src == gTwoButton)
			grandText.setText("This is my grandma and my Family!");
		else if(src == gThreeButton)
			grandText.setText("This is my grandma and my mother!");
		else if(src == pButton)
			parentText.setText("My mom, dad, and my nephew!");
		else if(src == pTwoButton)
			parentText.setText("My mom and dad dancing!");
		else if(src == pThreeButton)
			parentText.setText("My mom and dad 80's style!");
		else if(src == sButton)
			siblingText.setText("Me my brother and my sister!");
		else if(src == sTwoButton)
			siblingText.setText("Wow was I young here!");
		else if(src == sThreeButton)
			siblingText.setText("Me and my sister!");
		else if(src == gPlay){
			stopMusic(sac);
			stopMusic(pac);
			gac.play();
		}else if(src == gStop)
			stopMusic(gac);
		else if(src == pPlay){
			stopMusic(gac);
			stopMusic(sac);
			pac.play();
		}else if(src == pStop)
			stopMusic(pac);
		else if(src == sPlay){
			stopMusic(gac);
			stopMusic(pac);
			sac.play();
		}else if(src == sStop)
			stopMusic(sac);
		
	}
	private void stopMusic(AudioClip clip){
		//check if clip is playing if it is stop it
		if(clip != null)
			clip.stop();
	}
	public void mouseClicked(MouseEvent me)
	{
	
		int tabindex = tabPane.getSelectedIndex();
		
		if (tabindex == 0)
		{
			gac.play();
			pac.stop();
			sac.stop();
		}
		else if (tabindex == 1)
		{
			pac.play();
			gac.stop();
			sac.stop();
		}
		else if (tabindex == 2)
		{
			sac.play();
			gac.stop();
			pac.stop();
		}
	}
	
	public void mouseEntered(MouseEvent me){}
	public void mouseExited(MouseEvent me){}
	public void mousePressed(MouseEvent me){}
	public void mouseReleased(MouseEvent me){}

}
<html>
<head> <title>Thomas Stephen Brown</title></head>
<body><applet code="FamilyTreeTabbedPane.class" width="1400" height = "610"></applet></body>
</html>

Recommended Answers

All 31 Replies

Are there any error messages in the browser's java console?

BTW The APPLET tag's code= attribute refers to a class not a file.
Try removing the .class

yea here is what im getting sorry didnt know you could check.


Java Plug-in 1.6.0_24
Using JRE version 1.6.0_24-b07 Java HotSpot(TM) Client VM
User home directory = C:\Users\Thomas
----------------------------------------------------
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.security.AccessControlException: access denied (java.io.FilePermission grandIcon.jpg 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 FamilyTreeTabbedPane.init(FamilyTreeTabbedPane.java:25)
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 grandIcon.jpg read)

if you run an applet once via a browser, the browser will remember your applet, and will output what it has in its memory instead of the new version of the applet. therefore, you need to clean your browser's memory to run the new version of your applet if you've changed it. otherwise, everything seems to be fine. one more thought: are you sure you've saved your html file with an html suffix?

access denied (java.io.FilePermission grandIcon.jpg read)

This error says that the applet was denied permission to read a file from the local system.
Unsigned applets loaded from a server are NOT allowed to read files from the local system.

You should put ALL the files the applet needs into a jar file on the server, reference that jar file in the <APPLET tag's archive= attribute and use the getResource() method to get a URL to read the file from the jar file.

I compiled the code and ran it. I got a frame with three tabs, no pictures... I missed the FilePermission error posted here, no error when I compiled/ran the code. my bad!

This is so frustrating hahah. Yea I'm running it from my home computer, and I've tested it on others, and it does roughly the same thing. Any other ideas, NormR1 my professor is very strict when it comes to using methods that are not in the book so unfortunately I can't do that. I think it has something to do with the policy info on my computer but I tried changing it and I don't think it helped.

How are you executing the applet?
Where are all the files? Are they all local, no server?
How do you load the html into the browser?

Your code works for me for the few images I changed from yours to mine:

Java Plug-in 1.6.0_20
Using JRE version 1.6.0_25-b06 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Owner

----------------------------------------------------
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>
----------------------------------------------------

Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/make my dreams come true.wav
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/sweet disposition.wav
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/shes got you high.wav

When i debug the applet using eclipse or jgrasp it automatically creates a html file for me, I click on this html file which opens either firefox, chrome, or internet explorer. Tested them all.

The files are all stored locally. It pretty much looks like its hanging. Thanks for your help btw.

looks like its hanging

Where/when does the: java.security.AccessControlException: access denied (java.io.FilePermission grandIcon.jpg read)
happen?
This exception shouldn't happen for all local files.

Add lots of printlns to your code to show where the execution flow goes. If it is hanging, then the place it is hanging will be after the last print out and before the next one that didn't print.

So I added teh printlns but im not sure how to see them other then when i run them via jgrasp applet viewer or eclipse. In the browser how would i see them. I think the problem is that as soon as it tries to load the first image it throws the exception, so it hangs because of that. Do I have to sign it or something. It's wierd because I have a few other peoples from my class, and there's all work fine.

I have added a bunch of printlns but I dont know how to see them other than in the jgrasp console. In the browser they dont display. I believe the reason its hanging is because of the fact that its throwing an exception on the first image it tries to load. Very strange indeed. I have a few other people assignments that have virtually the same thing, but theres loads. Any clue why? Maybe the digital signature? O man this is a headaache.

It should not throw an AccessControlException for local files.
I'm confused by that.

You do understand that you must tell us exactly what is happening!!
You can't assume something or think that all errors are the same.

One problem I have believing you is that the line number in the error message:
at FamilyTreeTabbedPane.init(FamilyTreeTabbedPane.java:25)
line 25 is NOT where the image is being read in the code you posted which shows the first ImageIcon being called at line 20.
That means you have made changes between what you posted and when you got the error.

Im very sorry I didn't realize that I was wrong and that I meant to post this one. So sorry.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class FamilyTreeTabbedPane extends JApplet implements ActionListener, MouseListener{
	//Make buttons and labels local to the class so that they can be accessed by all methods, and the action performed method
	JButton gButton, gTwoButton, gThreeButton,pButton, pTwoButton, pThreeButton,sButton, sTwoButton, sThreeButton;
	JLabel grandText, parentText, siblingText;
	//create audio clips
	AudioClip gac,pac,sac;
	JTabbedPane tabPane;
	public void init(){
	
		//initialize my jtabbedpane
	   tabPane = new JTabbedPane();
		//set up audio
		gac = getAudioClip( getCodeBase(),"make my dreams come true.wav");
		pac = getAudioClip( getCodeBase(),"sweet disposition.wav");
		sac = getAudioClip( getCodeBase(),"shes got you high.wav");
		//set up tabpane mouse listener to listen for when clicked event
		tabPane.addMouseListener(this);
		//set size of the applet
		setSize(1400, 610);
		//create a new tab with an icon and the appropriate returned panel
		tabPane.addTab("Grandparent",new ImageIcon("grandIcon.jpg"), grandparentsTab());
		tabPane.addTab("Parents",new ImageIcon("parentIcon.jpg"),parentTab());
		tabPane.addTab("Siblings",new ImageIcon("siblingIcon.jpg"),siblingTab());
		//set the color of the tabbed panels
		tabPane.setBackgroundAt(0, Color.CYAN);
		tabPane.setBackgroundAt(1, Color.GREEN);
		tabPane.setBackgroundAt(2, Color.magenta);
		//add tabPane to the applet
		add(tabPane);
	}
//I normally would not have created three different method names, It adds alot of tedious work that can easily be circumvented by overloading the same one
//but in the instructions it said create three seperate methods for each tab, and I wasn't sure if method overloading was allowed, so I played it safe.	
	public JPanel grandparentsTab(){
		//create jpanel and Button
		JPanel grand, borderFrame;
		//setup audio clip
				//create jButtons
		gButton = new JButton(new ImageIcon("Grandparent.jpg"));
		gTwoButton = new JButton(new ImageIcon("Grandparent2.jpg"));
		gThreeButton = new JButton(new ImageIcon("Grandparent3.jpg"));
		//create jLabel
		grandText = new JLabel("               ",JLabel.CENTER);
		//create jpanels
		grand = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		//add action listeners
		gButton.addActionListener(this);
		gTwoButton.addActionListener(this);
		gThreeButton.addActionListener(this);
		//add to panel
		grand.add(gButton);
		grand.add(gTwoButton);
		grand.add(gThreeButton);
		//add items to borderFrame
		borderFrame.add(grand,BorderLayout.CENTER);
		borderFrame.add(grandText,BorderLayout.NORTH);
		//return to panel
		return borderFrame;
	}
	//refer to grandTab
	public JPanel parentTab(){
		JPanel parent, borderFrame;
		pButton = new JButton(new ImageIcon("Parents.jpg"));
		pTwoButton = new JButton(new ImageIcon("Parents2.jpg"));
		pThreeButton = new JButton(new ImageIcon("Parents3.jpg"));
		parentText = new JLabel("                    ",JLabel.CENTER);
		parent = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		pButton.addActionListener(this);
		pTwoButton.addActionListener(this);
		pThreeButton.addActionListener(this);
		parent.add(pButton);
		parent.add(pTwoButton);
		parent.add(pThreeButton);
		borderFrame.add(parent,BorderLayout.CENTER);
		borderFrame.add(parentText,BorderLayout.NORTH);
		return borderFrame;
	}
	//refer to grandTab
	public JPanel siblingTab(){
		JPanel sibling, borderFrame;    
		sButton = new JButton(new ImageIcon("siblings.jpg"));
		sTwoButton = new JButton(new ImageIcon("siblings2.jpg"));
		sThreeButton = new JButton(new ImageIcon("siblings3.jpg"));
		siblingText = new JLabel("               ",JLabel.CENTER);
		sibling = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		sButton.addActionListener(this);
		sTwoButton.addActionListener(this);
		sThreeButton.addActionListener(this);
		sibling.add(sButton);
		sibling.add(sTwoButton);
		sibling.add(sThreeButton);
		borderFrame.add(sibling,BorderLayout.CENTER);
		borderFrame.add(siblingText,BorderLayout.NORTH);
		return borderFrame;
	}
	public void actionPerformed(ActionEvent event) {
		Object src = event.getSource();
		
		if(src == gButton)
			grandText.setText("This is my grandma!");
		else if(src == gTwoButton)
			grandText.setText("This is my grandma and my Family!");
		else if(src == gThreeButton)
			grandText.setText("This is my grandma and my mother!");
		else if(src == pButton)
			parentText.setText("My mom, dad, and my nephew!");
		else if(src == pTwoButton)
			parentText.setText("My mom and dad dancing!");
		else if(src == pThreeButton)
			parentText.setText("My mom and dad 80's style!");
		else if(src == sButton)
			siblingText.setText("Me my brother and my sister!");
		else if(src == sTwoButton)
			siblingText.setText("Wow was I young here!");
		else if(src == sThreeButton)
			siblingText.setText("Me and my sister!");
		
	}
	public void mouseClicked(MouseEvent me)
	{
	
		int tabindex = tabPane.getSelectedIndex();
		
		if (tabindex == 0)
		{
			gac.play();
			pac.stop();
			sac.stop();
		}
		else if (tabindex == 1)
		{
			pac.play();
			gac.stop();
			sac.stop();
		}
		else if (tabindex == 2)
		{
			sac.play();
			gac.stop();
			pac.stop();
		}
	}
	
	public void mouseEntered(MouseEvent me){}
	public void mouseExited(MouseEvent me){}
	public void mousePressed(MouseEvent me){}
	public void mouseReleased(MouseEvent me){}

}

That doesn't change that fact that locally executed applets should not get AccessControlException.

Do you have a .java.policy file in this folder: C:\Users\Thomas
Have you used the policytool to make any changes to permissions?

Nope I dont believe that I do. I'm at school right now so I cant check but I know there is a policy file in the bin folder of eclipse that grants permissions to everything, which i assume is for the appletviewer. And no i have not used the policytool. I have found a solution though. It circumvents reading from im assuming the stream, by getting the base code of the image initially so that it never actually pulls the image from its location.

I'm using

"anImageIconHere"= new ImageIcon(getImage(getCodeBase(), "siblings.jpg"));

to set the image to the imageicon and then i use the imageicon as the parameter rather than creating it t and setting it when the method is called

I think your solution works because getCodeBase returns the URL the applet was loaded from (ie the server), so your getImage downloads the image file from that location, thus not needing local file access or any special permissions. I believe this is the "correct" way to do it anyway, rather than fiddling with policy files on every machine that might run this applet.

it never actually pulls the image from its location.

It must ultimately read the file from the local system. If it were this easy to bypass the security requirements, there really is not any security at all.

Hi Norm. Why do you say "It must ultimately read the file from the local system"?
I'm assuming the image files are in the same place (on the server) as the applet itself, so getImage(getCodeBase(), "someFile.jpg") will read the image from wherever the applet was loaded from, which is allowed by standard applet security... or have I missed the point somewhere?

I didn't think there was a server involved with this problem. I thought that All the files were on the OP's local disk. Hence my remark that all the files were being read from the local disk.
I wasn't thinking about getting files from a server. The getCodeBase() should work as you say.

Earlier I asked: Where are all the files? Are they all local, no server?
The OP's answer: The files are all stored locally.

There was no mention of a server, so I assumed that all the files are local.

Strange!!?? Need a webcam to look over the OPs shoulder and see what he is doing.

OK...
When a browser loads an applet from the local file system then the machine in question is both client and server... I think what's happening here is that when viewing the applet in a browser, even if the applet is loaded from the local file system, the security rules still apply - ie the applet can only load files from the codeBase (the URL where the applet was loaded from). Now maybe the codeBase happens to be on the local machine, but accessing via codeBase doesn't count as a "local file access".
What say you?

The program works for me. I'll try hiding my .java.policy file and see.

gssing via codeBase doesn't count as a "local file access"

That was what prompted my comment about there being no security if its that easy to read from the local system. More FM.

This code: tabPane.addTab("Parents",new ImageIcon( "images/right.gif"),parentTab());
works without my .java.policy file in the C:\Documents and Settings\Owner\ folder.
Loading the applet from an html file into Firefox.
My java console:

Java Plug-in 1.6.0_20
Using JRE version 1.6.0_25-b06 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Owner

----------------------------------------------------
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>
----------------------------------------------------

codeBase=file:/D:/JavaDevelopment/Testing/ForumQuestions5/
Loaded image: file:/D:/JavaDevelopment/Testing/ForumQuestions5/images/left.gif
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/make my dreams come true.wav
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/sweet disposition.wav
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/shes got you high.wav


left.gif was loaded using: getImage(getCodeBase(), "images/left.gif"))

Assuming I'm right, you can only read from (parts of) the browser's local file system if the applet was loaded from that local file system in the first place - so it doesn't give you access to anyone else's file systems.
What's FM?

Are you able to get the Exception?

FM= F****** magic


!!!!
Moved my testing code to a virgin folder and now get the exception. I thought I had hidden my .java.policy file, but I guess not.

Will take a look after breakfast.

I haven't been running the code here, so no Exceptions! But my guess is that if you move one of those files to a different directory (so it's not in the codeBase) without a security policy you won't be able to access it.
Thanks for "FM" BTW, I'm adding that to my vocabulary!

Check crossing posts.

Enjoy your breakfast. Talk later. J

Just noticed this in the sun docs re unsigned applets

They that are loaded from the local file system (from a directory in the user's CLASSPATH) have none of the restrictions that applets loaded over the network do.

So it seems I was wrong for applets loaded from a local directory that is in the classpath. This may be adding to the confusion when test cases are being run.

Seems like we're going to learn something with this one. Just ran some tests with different browsers: All failed with the security exception, EXCEPT for Firefox 4.0.1
It was able to read the file!!??

More tests later.

Here's my test program. getImage() is able to read from the local disk. The other classes and methods I've tried so far get the exception.
My code:

/*  Following for quick test in AppletViewer
<applet code="TestFileAccessApplet" width="400" height = "400"></applet>
*/

import java.io.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;

public class TestFileAccessApplet extends JApplet {

   boolean gotExcp1 = false, gotExcp2 = false;   // flags for continued tests
   Image img; // used to hold getImage() value

   public void init() {
      System.out.println("codeBase=" + getCodeBase()); //<<<codeBase=file:/H:/Work/
     try {
        File f1 = new File("duke.gif");    // From local folder vs images/ - Same problem
        System.out.println("f1=" + f1 + ", exists=" + f1.exists());    // exists() gets Exception here

        FileInputStream fis = new FileInputStream(f1);  //<<<<<<< exception here in Chrome, etc
        int aByte = fis.read();
        fis.close();
        System.out.println("aByte=" + aByte);
   
     }catch(Exception x) {
       x.printStackTrace(); 
       gotExcp1 = true;   // this failed. try another
     }

     // Try another way if above got exception
     if(gotExcp1) {
       System.out.println("Second try with getCodeBase()");
       try{
         URL codeBase = getCodeBase();
         InputStream is = codeBase.openStream();                // This gets Exception
         DataInputStream dis = new DataInputStream(is);
        int aByte = dis.read();
        dis.close();
        System.out.println("aByte2=" + aByte);
       }catch(Exception x) {
        x.printStackTrace(); 
        gotExcp2 = true;     // this failed. try another
       }
     }


     // Try yet another way
     if(gotExcp2) {
        URL url = getCodeBase();
        img = getImage(url, "images/AppletImg.jpg");   // This WORKS !!! <<<<<<<<<<
        repaint();          // call paint)( with img

        ImageIcon imgIcon = new ImageIcon(img);
        JButton gButton = new JButton(imgIcon);                 // This works !!!      <<<<<<<<<<<<<<
        System.out.println("after getImage(getCodeBase... img=" + img);
        add(gButton);   // Show what we have !!!

//        JButton xButton = new JButton(new ImageIcon("images/right.gif"));   // This gets exception
//        add(xButton); 
     }

   } // end init()

   public void paint(Graphics g) {
     super.paint(g);
     if (img != null) 
       g.drawImage(img, 0, 0, this);      // This works 
   }

} // end class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.