7 Replies - 440 Views - Last Post: 14 October 2011 - 03:13 PM Rate Topic: -----

#1 AggressiveFish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 21-April 11

JApplet and Images

Posted 05 October 2011 - 07:13 AM

Hi,
I have a JApplet application that I would like to be able to feed in an image name through a param tag and have it display within the JApplet. There could be hundreds of choices to choose from for the image so I was wondering what would be the best way to achieve handling where the images are stored and how they are accessed. I was hoping I would just have a directory (underneath getCodeBase()) called images and access them from there. Is this possible? I try running the JApplet from the commandline (the application rests on another server that I am mapped to - P: drive) but I get the error :
Error occured on opening of image access denied (java.io.FilePermission file:/P:
/Inetpub/wwwroot/dir1/dir2/images/apple.jpg read)
The html page with the JApplet nested is located in P:/Inetpub/wwwroot/dir1/dir2 if that helps.

Any help would be appreciated.
AF

Is This A Good Question/Topic? 0
  • +

Replies To: JApplet and Images

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: JApplet and Images

Posted 05 October 2011 - 07:25 AM

Create a method that will use a JFileChooser for them to select the file, get the path. In a different method have a String path as the params for it, and in the previous method call this method sending the path of the new file as the params, create an image using the path, add it to the JApplet.
Was This Post Helpful? 0
  • +
  • -

#3 AggressiveFish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 21-April 11

Re: JApplet and Images

Posted 05 October 2011 - 07:45 AM

I'm not concerned about how to prompt for the image as that's already handled. What I need to know is why there are permission issues running from commandline like I did and where should the images be stored? I've seen the images stored in a jar but this may not be practical since so many images. I want to first of get past the permission erro I'm getting running from commandline and then move onto testing from the browser.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JApplet and Images

Posted 05 October 2011 - 09:35 PM

Applet gave a getCodeBase() method that returns the URL from where it is issued

http://download.orac...l#getCodeBase()

If your .jpg, .png are in the same directory you can access them

View PostFuzzyness, on 05 October 2011 - 10:25 AM, said:

Create a method that will use a JFileChooser for them to select the file, get the path.

Never tried it but I seriously doubt you can use JFileChooser from an Applet :)
It will try to start by the local file system that it can acccess
May be you can initialized with getCodeBase() but I seriously doubt

This post has been edited by pbl: 05 October 2011 - 09:58 PM

Was This Post Helpful? 1
  • +
  • -

#5 AggressiveFish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 21-April 11

Re: JApplet and Images

Posted 06 October 2011 - 03:36 AM

Hey pbl,
Yeah, used the getCodeBase and tacked on an additional directory on it to keep images separate. Running it from commandline the getCodeBase function returns something like file://P:/dir1/dir2/.... and when I try using getImage it fails giving an error related to file permission read. I checked the permissions on the image and gave it full permissions for Everyone and it still failed with the same error. Any ideas why it would fail in such a way from commandline? Thanks again.

AF
Was This Post Helpful? 0
  • +
  • -

#6 AggressiveFish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 21-April 11

Re: JApplet and Images

Posted 07 October 2011 - 08:47 AM

My computer has the C: drive from another computer mapped as the P: drive and the JApplet, images, etc.., reside on the P: drive. I'm running appletviewer from my own computer while in the P: drive at commandline. I've posted the source code and the html file that has the JApplet embedded in it. The apple.jpg is just a jpg of an apple, simple image. The error I get when running from commandline is :
Error occured on opening of image access denied (java.io.FilePermission file:/P:
/Inetpub/wwwroot/dir1/dir2/images/apple.jpg read)

package diagram;
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.util.*;
import java.awt.Image;
import java.awt.Dimension;
import java.net.*;
import javax.swing.JLabel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.*;
import java.util.List;

public class ObjTableDisp extends JApplet {
List<Integer> objectCounter = new ArrayList<Integer>();
List<Image> imageLst = new ArrayList<Image>();
int typesObject;
int objPerCnt = 0;
String objPerStr, indImage, urlString;
JPanel drawObj;
URL baseURL;

public void init () {
JPanel newOne;
//--- Create Object ---------
try {
typesObject = Integer.parseInt(getParameter("objectCount"));
} catch (Exception err) {
typesObject = -1;
}
//---- Get number of objects being passed in for each object type and link for each ------
baseURL = this.getCodeBase();
System.out.println("Types : " + typesObject);
for (int objLoop=1;objLoop <= typesObject;objLoop++) {
objPerStr = this.getParameter("objectCnt"+objLoop);
indImage = this.getParameter("indImage"+objLoop);
System.out.println(objPerStr + " + " + indImage);
if (objPerStr != null && indImage != null) {
objPerCnt = Integer.parseInt(objPerStr);
urlString = baseURL.toString()+"images/apple.jpg";
System.out.println("Found one "+urlString);
try {
imageLst.add(Toolkit.getDefaultToolkit().getImage(urlString)); 
} catch (Exception ert) {
System.out.println("Error occured on opening of image " + ert.getMessage());
}
objectCounter.add(objPerCnt); 
}
}
Box topLevel = new Box(BoxLayout.X_AXIS);
newOne = new JPanel () {
public void paint(Graphics g) { 
super.paint(g); 
g.drawImage(imageLst.get(1), 200, 200, this);
}
};
topLevel.add(newOne);
this.add(topLevel);
this.setSize(new Dimension(600,600));
this.setVisible(true);
}
}



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<applet name="japp" width="600" height="600" codebase="P:/Inetpub/wwwroot/dir1/dir2/" code="diagram.ObjTableDisp.class">
<param name="objectCount" value="1" />
<param name="objectCnt1" value="1" />
<param name="indImage1" value="apple.jpg" />
</applet>
</body>
</html>


This post has been edited by macosxnerd101: 07 October 2011 - 08:48 AM
Reason for edit:: With almost 65 posts, you should know the rules. Use code tags!!!

Was This Post Helpful? 0
  • +
  • -

#7 AggressiveFish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 21-April 11

Re: JApplet and Images

Posted 14 October 2011 - 08:00 AM

So images embedded in JLabels do not resize when their JLabels resize. If my images are say 500x500 and I end up wanting to display them in a JApplet 100x100 (let's say) how can I easily downsize the images? Is there an easy way to do this? Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JApplet and Images

Posted 14 October 2011 - 03:13 PM

You want something like that ?

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;

import javax.swing.*;

// garanties that we display the whole image
class ImageLabel extends JLabel {
	Image image;
	ImageObserver imageObserver; 
	
	ImageLabel(String filename) {
		ImageIcon icon = new ImageIcon(filename);
		image = icon.getImage();
		imageObserver = icon.getImageObserver();
	}

	public void paint( Graphics g ) {
		super.paint( g ) ;
		g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1