Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2008, 05:11 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 19
keffie91 is on a distinguished road
[SOLVED] PictureViewer applet
Hello I have build a PictureViewer applet that shows an image, But it doesn't work, I only see a very small square that is a few pixel width en high.
I just don't understand why it is not working.

PictureViewer.java:

Code:
import java.awt.*; import javax.swing.*; import java.net.*; import java.io.*; import java.applet.*; public class PictureViewer extends Applet { Image img; public void init(){ String pictureName = getParameter("pictureName"); img = getImage(getCodeBase(),pictureName); showStatus("Loads" + pictureName); MediaTracker mt = new MediaTracker(this); mt.addImage(img,0); try{mt.waitForID(0);} catch(InterruptedException e){} showStatus("Loading finished"); add(new ImagePanel(img)); setBackground(Color.white); } }
ImagePanel.java

Code:
import java.awt.*; public class ImagePanel extends Panel { private Image im; public ImagePanel(Image img){ im = img; setSize(im.getWidth(this),im.getHeight(this)); } public void paint(Graphics g){ g.drawImage(im,0,0,this); } }
PictureViewer.html

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PictureViewer Applet</title> </head> <body> <applet code= "PictureViewer.class" width="1020" height="900"> <param name="pictureName" value="Train.gif" /> </applet> </body> </html>


All the files are in the same map and the image Train.gif really exists.

Can someone help me?

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2008, 05:30 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,571
Norm is on a distinguished road
Add some println() statements to show the the size of the component displaying the image, and any other sizes you need to know. Put them in more than one place, ie in the paint() method and in the init()
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-26-2008, 08:03 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 19
keffie91 is on a distinguished road
I tried
Code:
println(img.getWidth(this));
and

Code:
int j1 = img.getWidth(this); String output = j1.toString(); add(output);
But that didn't work to, I don't know more ways to write te size of the image out. Can somebody show me how I have to write out the size?

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-26-2008, 08:12 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Calling setSize doesn't do any good till after realization. Override the getPreferredSize method to provide a size hint to the image components parent.
Code:
class ImagePanel extends Panel { private Image im; public ImagePanel(Image img){ im = img; // setSize(im.getWidth(this),im.getHeight(this)); } public void paint(Graphics g){ g.drawImage(im,0,0,this); } public Dimension getPreferredSize() { return new Dimension(im.getWidth(this),im.getHeight(this)); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-26-2008, 08:19 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 19
keffie91 is on a distinguished road
I have tried your solution but it doesn't work.
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by keffie91 : 07-26-2008 at 08:53 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-26-2008, 09:10 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,571
Norm is on a distinguished road
Quote:
that didn't work
Add println() statements won't make your program work! It will show you the values of variables so you can see if what you want to happen is happening.
The process is called debugging.
When a program is not working, you need to see what it is doing and then try to figure out how to make it do what you want it to do.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-26-2008, 11:35 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Some things that can cause problems with this:
1 — MediaTracker doesn't let you know if it was unable to find the file, if the image was unrecognizable/unreadable or if the data is corrupted. You have to use MediaTracker methods to inquire about the loading success.
2 — check the image path. For this the image is in the same file as the class files: the current directory for now.
3 — use appletviewer to develop your applets. Browsers and the java plug-in, aka, jre (Java Runtime Environment), both cache applets and run the cached classes instead of updated class files. So you have to change your class names for each compile-and-run round. You avoid this when you use appletviewer. To use appletviewer include an applet tag in a comment in your source file, compile and run it at the prompt like this:
Code:
C:\jexp>javac pictureviewer.java C:\jexp>appletviewer PictureViewer.java Java is looking for piano.jpg at this URL file:/C:/jexp/piano.jpg Loadspiano.jpg Loading status: COMPLETE Loading finished w = 400 h = 96
This works okay using an image in my current directory. Be sure to change the image name in the applet tag, and the class name as well if you're having caching problems.
Code:
// <applet code= "PictureViewer.class" width="400" height="400"> // <param name="pictureName" value="piano.jpg" /> // </applet> // use: atThePrompt>appletviewer PictureViewer.java import java.awt.*; import javax.swing.*; import java.net.*; import java.io.*; import java.applet.*; public class PictureViewer extends Applet { Image img; public void init(){ String pictureName = getParameter("pictureName"); System.out.println("Java is looking for " + pictureName + " at this URL " + getCodeBase() + pictureName); img = getImage(getCodeBase(),pictureName); System.out.println("Loads" + pictureName); MediaTracker mt = new MediaTracker(this); mt.addImage(img,0); try{ mt.waitForID(0); } catch(InterruptedException e) { System.out.println("interrupt"); } int status = mt.statusAll(false); showLoadingStatus(status); if(mt.isErrorAny()) { System.out.println("Loading failed"); } else { System.out.println("Loading finished"); } add(new ImagePanel(img)); setBackground(Color.white); } private void showLoadingStatus(int status) { String s = "unknown"; if((status & MediaTracker.LOADING) == MediaTracker.LOADING) { s = "LOADING"; } if((status & MediaTracker.ABORTED) == MediaTracker.ABORTED) { s = "ABORTED"; } if((status & MediaTracker.ERRORED) == MediaTracker.ERRORED) { s = "ERRORED"; } if((status & MediaTracker.COMPLETE) == MediaTracker.COMPLETE) { s = "COMPLETE"; } System.out.println("Loading status: " + s); } } class ImagePanel extends Panel { private Image im; public ImagePanel(Image img){ im = img; System.out.println("w = " + im.getWidth(this) + " h = " + im.getHeight(this)); } public void paint(Graphics g){ g.drawImage(im,0,0,this); } public Dimension getPreferredSize() { return new Dimension(im.getWidth(this),im.getHeight(this)); } }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-27-2008, 02:26 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 19
keffie91 is on a distinguished road
I have adapted the code and run it, The width en the Height are 1799 and 1127, so the size of the image is correct and there were no errors, but what i see is the very small square of a few pixels width en high.

I don't understand it.

Thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-27-2008, 07:46 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
The width en the Height are 1799 and 1127
When a component does not have enough display area in its parent container it collapses to its default/minimum size.
So you'll need to mount the ImagePanel instance in a ScrollPane and add that to the center section of a BorderLayout in the applet.
Replace this line in PictureViewer
Code:
add(new ImagePanel(img));
with these
Code:
setLayout(new BorderLayout()); ScrollPane scrollPane = new ScrollPane(); scrollPane.add(new ImagePanel(img)); add(scrollPane);
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-27-2008, 08:28 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 19
keffie91 is on a distinguished road
thanks this works. I have added some code so that there is playing an audio file whem the image is loaded.

But now I want the complete applet in the map PictureViewer, In that map is the file PictureViewer.html, a map called bin with the class files in it. , a map called image with the image in it and a map called sound with the sound file in it.

To get that working I tried this

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PictureViewer Applet</title> </head> <body> <applet code= "PictureViewer.class" codebase="/bin" width="1020" height="900"> <param name="pictureName" value="image/Train.gif" /> <param name="audioName" value="sound/Train.au" /> </applet> </body> </html>
I have also tried \ in the path to the files, but it doesn't work, can somebody help me?

Oh, with all the files in one mape it works, I have tried that.

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-27-2008, 09:12 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,571
Norm is on a distinguished road
Quote:
but it doesn't work
Are there any error messages in the Java console? Copy and paste them here.
Add println() statements to your code to show what the various fetching methods are returning.
Are you testing this standalone on your PC or via fetches to a server?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-27-2008, 11:16 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
The tutorial addresses questions like this in Trail: Deployment.
Some links into this that you might find helpful:
Finding and Loading Data Files
Deploying Applets
Lesson: Packaging Programs in JAR Files
Of special interest might be:
Loading Images Using getResource
found on the page
How to Use Icons.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-28-2008, 09:01 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 19
keffie91 is on a distinguished road
Thanks fors this links, They are very useful, If my applet is finished, I give you the link to the applet.

Thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet in a GUI serfster New To Java 1 06-13-2008 12:09 AM
applet amith AWT / Swing 1 05-16-2008 04:24 AM
First Applet HELP???? nvidia New To Java 0 08-13-2007 11:11 PM
Applet kapoorje Java Applets 0 07-24-2007 05:06 PM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +3. The time now is 02:43 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org