Image loading and showing problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Image loading and showing problem

Hybrid View

  1. #1
    Join Date
    Jul 2006
    Location
    Belgium
    Posts
    6

    Image loading and showing problem

    Hi,

    I'm trying to make a little java application for my website, but I have
    to load images. The compiling of the code gives no errors, but when I
    run it, it says "Applet not initialized" . I know wich row the message
    causes, but I don't know how to solve it.
    Here's the code:

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.Image.*;
    
    public class App extends Applet {
            DeskTop desktop;
    
            public void init() {
                    desktop = new DeskTop( );
            }
    
            public void paint(Graphics g) {
                    desktop.paint( g );
            }
    
    }
    
    class DeskTop extends App {
            private String desktopimagename;
            private Image desktopimage;
    
            public DeskTop( ) {
                    desktopimagename = "bg_Tiger.jpg";
                    desktopimage = super.getImage(super.getDocumentBase(), "img/" +
    desktopimagename);
            }
    
            public void paint( Graphics g ) {
                    g.drawImage( desktopimage, 0, 0, super.WIDTH, super.HEIGHT, this );
            }
    }
    The code wich causes the message is the 23rd row:

    Code:
    desktopimage = super.getImage(super.getDocumentBase(), "img/" +
    desktopimagename);
    Does anybody know something that can solve this ?

    Thanks in advance...



    PS: what I also tried was:
    add a void to my App class:
    Code:
    public Image getImg( String imagename ) {
    	Image picture;
    	picture = this.getImage( this.getDocumentBase(), "img/" + imagename );
    	return picture;
    }
    and instead of using
    Code:
    desktopimage = super.getImage(super.getDocumentBase(), "img/" +
    desktopimagename);
    i used
    Code:
    desktopimage = super.getImg( desktopimagename );
    Last edited by sachavdk; 07-12-2006 at 11:18 PM. Reason: Quick addition

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Try to bring up the java console of your browser,
    IE: Tools->Sun Java Console
    Opera: Window->Special->Java Console

    That way you can see the complete traceback and error type.

    BTW, making an applet extension (DeskTop) for use inside your applet extension (App) doesn't seem to add clarity to your solution...
    eschew obfuscation

  3. #3
    Join Date
    Apr 2006
    Posts
    28
    Try use this code
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class App extends JApplet {
    // DeskTop desktop;
    private Image desktopimage;
    public void init() {
    desktopimage = getImage(super.getDocumentBase(),"\\image/bg_Tiger.jpg" );
    }

    public void paint(Graphics g) {
    g.drawImage( desktopimage, 0,15, this );

    }

    }

  4. #4
    Join Date
    Jul 2006
    Location
    Belgium
    Posts
    6
    Yes that does work, but my problem actually is that I want to make other classes as well who will draw images (this DeskTop class is just a small test to draw such an image in a class).
    I have had one year of Java at school now an I know how to write these classes etc., but I never used images before.
    They aren't handled in my book ("En dan is er... Java" from Academic Service) and the way you told me is the same way I found on the net .

    But my way doesn't seem to work to load external images in other classes.
    Maybe you know how I should do this or know a site where I can find a clear explanation?

    Thx

  5. #5
    Join Date
    Apr 2006
    Posts
    28
    Using 2 classes and iimplement event

    import java.awt.*;
    import java.awt.event.*;

    public class App extends Frame {

    public static void main(String[] args)
    {
    DeskTop a= new DeskTop();

    a.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    a.setSize(500,300);
    a.setTitle("Tiger in Africa ");
    a.show();
    }

    }
    class DeskTop extends Frame
    {
    private String desktopimagename;
    private Image desktopimage;
    public DeskTop()
    {
    desktopimagename = "\\image/ca.gif";
    desktopimage =getToolkit().getImage(desktopimagename);
    }

    public void paint( Graphics g )
    {
    g.drawImage(desktopimage, 0, 0, this );
    }
    }

  6. #6
    Join Date
    Jul 2006
    Location
    Belgium
    Posts
    6
    Now I get this error:

    Note: F:\www\sachavdk.be\maclook\app\App.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    And when I compiled it with the -Xlint argument, I got these errors:
    F:\www\sachavdk.be\maclook\app\App.java:28: warning: [deprecation] show() in java.awt.Window has been deprecated
    a.show();
    ^
    F:\www\sachavdk.be\maclook\app\App.java:12: warning: [serial] serializable class App has no definition of serialVersionUID
    public class App extends Frame {
    ^
    F:\www\sachavdk.be\maclook\app\App.java:32: warning: [serial] serializable class DeskTop has no definition of serialVersionUID
    class DeskTop extends Frame {
    ^
    3 warnings
    I've had a lot of errors but never ones like these.
    (However I hope to get this working, then I've learned something)

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you use swing you should override the paintComponent(Graphics g)
    method of JComponent. That class is part of the base for (nearly) all visible components. This method, if used in the ImagePanel, will give you a panel that you can draw on, while at the same time you add components to it, like buttons, other panels, etc. Just remember to use absolute positioning (setBounds) for these components and a layoutmanager = null for the panel.

    You could also make your DeskTop an extension of JDesktopPane and override its paintComponent method for doing the drawing. The JDesktopPane is the base for MDI applications in java, you can add JInternalFrames to it. Check out the documentation for this.
    eschew obfuscation

  8. #8
    Join Date
    Jul 2006
    Location
    Belgium
    Posts
    6
    Made some progress .
    Writing from the DeskTop class works, but it doesn't fully support the ImagePanel.
    Code:
    class DeskTop extends JComponent {
    	
    	public ImagePanel imgPan=null;
    	private Image img;
    	
    	public DeskTop() {
    		getContentPane().setLayout(new GridLayout());
    		imgPan=new ImagePanel();
    		getContentPane().add(imgPan);
    		img = getNamedImage( "bg_Tiger.jpg" );
    		imgPan.setImage( img );
    	}
    	
    	public void paintComponent( Graphics g ) {
    		g.drawImage( img, 0, 0, App.WIDTH, App.HEIGHT, this );
    	}
    	
    	private Image getNamedImage(String name) {
    		Image img=getImage( getDocumentBase(),"img/"+name);
    		
    		if (img != null) {
    			// wait for image to arrive
    			MediaTracker mt=new MediaTracker(this);
    			mt.addImage(img, 0);
    			
    			try {
    				mt.waitForAll();
    			} catch (InterruptedException ie) {
    				img=null;
    			}      
    		}
    		return img; 
    	}
    }
    I've the feeling I'm doing something wrong with the ImagePanel class

    The errors returned:
    F:\www\sachavdk.be\look\app\App.java:52: cannot find symbol
    symbol : method getContentPane()
    location: class DeskTop
    getContentPane().setLayout(new GridLayout());
    ^
    F:\www\sachavdk.be\look\app\App.java:54: cannot find symbol
    symbol : method getContentPane()
    location: class DeskTop
    getContentPane().add(imgPan);
    ^
    F:\www\sachavdk.be\look\app\App.java:64: cannot find symbol
    symbol : method getDocumentBase()
    location: class DeskTop
    Image img=getImage( getDocumentBase(),"img/"+name);
    ^
    3 errors

  9. #9
    Join Date
    Jul 2006
    Location
    Belgium
    Posts
    6
    Euhm another thing.
    I just realised I never said it.

    My applet is used for web so it is not an executable.

    Maybe that's why there are little things that don't work?

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Too high...

    You have made your DeskTop class an extension of JComponent, don't do that, its too high in the class hierachy for your use. Make it a JApplet extension. An applet is really just a panel (component) equipped for travelling on the internet . - and its a JComponent descendant.

    So. change
    Code:
    class DeskTop extends JComponent
    to
    Code:
    class DeskTop extends JApplet
    then just rename your constructor:
    Code:
    public DeskTop()
    to
    Code:
    public void init()
    the init method is invoked by the browser when your applet page is shown.

    Btw: I have included a hybrid version (applet and application in one) here, its really no big deal..
    Code:
    /**
     * A hybrid applet, runs as an application and as applet
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.Image;
    
    public class HybridApplet extends JApplet {
      private ImagePanel imgPan=null;
      private boolean isStandalone=false; // true when run from main()
      private static String localPath="c:\\tmp"; // image file location on local computer
      /**
       * Initialize the applet
       */
      public void init() {
        getContentPane().setLayout(new GridLayout());
        imgPan=new ImagePanel();
        getContentPane().add(imgPan);
        Image img = getNamedImage("\\imagerolls/rd_59_2_1055.gif");// relative to document root (web)
        imgPan.setImage(img);
      }
      /**
       * Tell the applet where (what) it is...
       * @param isStandalone
       */
      public void setStandalone (boolean isStandalone) {
        this.isStandalone=isStandalone;
      }
      /**
       * This metod retrieves image from local disk or over the net depending on
       * the isStandalone flag.
       * @param name
       * @return Image
       */
      private Image getNamedImage(String name) {
        Image img=null;
        if (isStandalone) {
          try {
            /* application */
            FileInputStream in = new FileInputStream(localPath+name);// add on local path
            int n = in.available();
            byte[] b = new byte[n];
            in.read(b);
            in.close();
            img=Toolkit.getDefaultToolkit().createImage(b);
          }
          catch (IOException ex) {
            ex.printStackTrace(); // bummer
            img=null;
          }
        } else {
          /* applet */
          img = getImage(getDocumentBase(), name);
          if (img != null) {
            // wait for image to arrive
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(img, 0);
            try {
              mt.waitForAll();
            }
            catch (InterruptedException ie) {
              ie.printStackTrace();
              img = null;
            }
          }
        }
        return img;
      }
      /**
       * This main() method is only executed when the applet is
       * run standalone like an application.  If this code is published
       * in a web page the main() will have no effect, as the browser will
       * do the initialization and embed the applet in its browser window.
       * Note: the applet is in effect just a JPanel when used like this, it
       * is quite handy for quick testing.
       */
      public static void main(String[] args) {
        JFrame f=new JFrame("Standalone applet");
        HybridApplet theApplet=new HybridApplet();
        f.getContentPane().setLayout(new GridLayout());
        f.getContentPane().add(theApplet);
        theApplet.setStandalone(true); // important
        theApplet.init(); // must do it yourself here, no browser
        f.addWindowListener(new WindowAdapter() { // ensure proper terminaton
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        f.setBounds(20,20, 500,400); // whatever...
        f.setVisible(true);
      }
    }
    /* a very basic image display panel */
    class ImagePanel extends JPanel {
      private Image img=null;
    
      public void setImage(Image img) {
        this.img=img;
        repaint();
      }
      public void paintComponent(Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.white);
        g.fillRect(0,0,getWidth(), getHeight());
        if (img==null) {
          g.setColor(Color.black);
          g.drawString("No image loaded yet", 20, 20);
        } else {
          g.drawImage(img, 0,0,this);
        }
        g.setColor(c);
      }
    }
    Last edited by sjalle; 07-16-2006 at 11:07 AM. Reason: typos
    eschew obfuscation

Similar Threads

  1. Showing something when loading data
    By THL in forum ASP.NET
    Replies: 2
    Last Post: 03-08-2006, 08:02 AM
  2. Replies: 1
    Last Post: 03-03-2002, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center