Results 1 to 13 of 13
  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default [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:

    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

    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

    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 keffie91
    Never give up! ;)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

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

  3. #3
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    I tried
    Java Code:
    println(img.getWidth(this));
    and

    Java 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! ;)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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));
        }
    }

  5. #5
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    I have tried your solution but it doesn't work.
    Last edited by keffie91; 07-26-2008 at 07:53 PM.
    Never give up! ;)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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:
    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
    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:
    // <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));
        }
    }

  8. #8
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    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! ;)

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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
    Java Code:
            add(new ImagePanel(img));
    with these
    Java Code:
            setLayout(new BorderLayout());
            ScrollPane scrollPane = new ScrollPane();
            scrollPane.add(new ImagePanel(img));
            add(scrollPane);

  10. #10
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    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

    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>
    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! ;)

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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?

  12. #12
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

  13. #13
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    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! ;)

Similar Threads

  1. Applet in a GUI
    By serfster in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 11:09 PM
  2. applet
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2008, 03:24 AM
  3. First Applet HELP????
    By nvidia in forum New To Java
    Replies: 0
    Last Post: 08-13-2007, 10:11 PM
  4. Applet
    By kapoorje in forum Java Applets
    Replies: 0
    Last Post: 07-24-2007, 04:06 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •