Results 1 to 13 of 13
Thread: [SOLVED] PictureViewer applet
- 07-26-2008, 04:11 PM #1
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
[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:
ImagePanel.javaJava 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); } }
PictureViewer.htmlJava 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); } }
XML 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 keffie91Never give up! ;)
- 07-26-2008, 04:30 PM #2
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()
- 07-26-2008, 07:03 PM #3
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
I tried
andJava Code:println(img.getWidth(this));
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?Java Code:int j1 = img.getWidth(this); String output = j1.toString(); add(output);
thanks keffie91Never give up! ;)
- 07-26-2008, 07:12 PM #4
Calling setSize doesn't do any good till after realization. Override the getPreferredSize method to provide a size hint to the image components parent.
Java 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)); } }
- 07-26-2008, 07:19 PM #5
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
I have tried your solution but it doesn't work.
Last edited by keffie91; 07-26-2008 at 07:53 PM.
Never give up! ;)
- 07-26-2008, 08:10 PM #6
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.that didn't work
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.
- 07-26-2008, 10:35 PM #7
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:
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.Java 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
Java 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)); } }
- 07-27-2008, 01:26 PM #8
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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 keffie91Never give up! ;)
- 07-27-2008, 06:46 PM #9
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
with theseJava Code:add(new ImagePanel(img));
Java Code:setLayout(new BorderLayout()); ScrollPane scrollPane = new ScrollPane(); scrollPane.add(new ImagePanel(img)); add(scrollPane);
- 07-27-2008, 07:28 PM #10
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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
I have also tried \ in the path to the files, but it doesn't work, can somebody help me?XML 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>
Oh, with all the files in one mape it works, I have tried that.
thanks keffie91Never give up! ;)
- 07-27-2008, 08:12 PM #11
Are there any error messages in the Java console? Copy and paste them here.but it doesn't work
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?
- 07-27-2008, 10:16 PM #12
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.
- 07-28-2008, 08:01 PM #13
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
Similar Threads
-
Applet in a GUI
By serfster in forum New To JavaReplies: 1Last Post: 06-12-2008, 11:09 PM -
applet
By amith in forum AWT / SwingReplies: 1Last Post: 05-16-2008, 03:24 AM -
First Applet HELP????
By nvidia in forum New To JavaReplies: 0Last Post: 08-13-2007, 10:11 PM -
Applet
By kapoorje in forum Java AppletsReplies: 0Last Post: 07-24-2007, 04:06 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks