Results 1 to 11 of 11

Thread: jpg not showing in canvas

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved jpg not showing in canvas

    I have a class that adds the following class to a container. Then shows the JFrame that contains the container. ie container.add(new MovieImage()); When the JFrame is made visible the image does not show. The image only appears when the JFrame is either resized or iconified then deiconified. Any suggestions as to how i could go about getting the image to appear the first time the JFrame is drawn? Thanks.
    Code:
    class DrinkImage extends Canvas{
      public DrinkImage(){
        setSize(25,50); 
      }
      public void paint(Graphics g){
       Toolkit tk = Toolkit.getDefaultToolkit();
       Image image = tk.getImage("C:\\Beer.jpg");
       g.drawImage(image,10,10,null);
      }
     }

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Have you tried the repaint() method? It would have to be something along that line since it only shows after its resized. Not sure though...

  3. #3

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    I had this same problem in VB.NET one time and the only way I could get it to work is make a bitmap and set the image equal to the bitmap. Im not sure if there is even a way to make a bitmap in java, but thats the way I got it fixed in another language. Again not sure if that will work though...

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The canvas is added to the container c. Now when the JFrame rv is made visible the paint method should be fired off to draw the image. It's only fired off when everything needs to be drawn a second time.
    Code:
    // constructor
       c.add(new DrinkImage()); 
       rv.setSize(400,500);
       rv.setVisible(true); 
       
      }
    }
     class DrinkImage extends Canvas{
      public DrinkImage(){
        setSize(25,50); 
      }
      public void paint(Graphics g){
       Toolkit tk = Toolkit.getDefaultToolkit();
       Image image = tk.getImage("C:\\Beer.jpg");
       g.drawImage(image,10,10,null);
      }
     }

  6. #6
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222
    i've messed around with similair things, and generally i still had to call super.paint(g) and then after these do my extra bits of painting.....

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The way ive always understood it was that paint() is automatically invoked by the awt thread when the size of the component has changed or been uncovered. This sounds correct becasue if i change the size of my JFrame that holds the canvas the image appears, also when the JFrame is iconified then deiconified the image is there so paint is being called. Now i should be able to call repaint() in my application but from what i understand repaint() calls update() which eventually calls paint(); Calling repaint() is not working for me at all so is there another alternative?

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I was thinking about not displaying the JFrame until the image is drawn but i don't know if this would work and i know nothing about using an ImageObserver.

  9. #9
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    I don't know if this is any help at all.

    why don't u use media tracker for loading the image.. here is a
    sample code..

    Code:
    MediaTracker tracker = new MediaTracker(this);
    Image img = Toolkit.getDefaultToolkit().getImage(<full path of mage and file name [I used getcanonical path or file]>);
            
    tracker.addImage(img, 0);
    
            try {
                tracker.waitForAll();
            }
            catch (InterruptedException ex) {
            }
    after that u can use img for drawing... worked for me.. although I applied the image to a Jpanel... worth a try!

  10. #10

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Works great! Thanks.
    Code:
    class MovieImage extends Canvas{
       private MediaTracker mt; 
       
      public MovieImage(){
      	mt = new MediaTracker(this);  
        setSize(25,50); 
      }
      public void paint(Graphics g){
       Toolkit tk = Toolkit.getDefaultToolkit();
       Image image = tk.getImage("C:\\Beer.jpg");
       mt.addImage(image, 0);
       try {
         mt.waitForAll();
       }catch (InterruptedException ex){}
       g.drawImage(image,10,10,null);
      }
     }

  11. #11
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    Originally posted by Dilenger4
    Works great! Thanks.
    Code:
    class MovieImage extends Canvas{
       private MediaTracker mt; 
       
      public MovieImage(){
      	mt = new MediaTracker(this);  
        setSize(25,50); 
      }
      public void paint(Graphics g){
       Toolkit tk = Toolkit.getDefaultToolkit();
       Image image = tk.getImage("C:\\Beer.jpg");
       mt.addImage(image, 0);
       try {
         mt.waitForAll();
       }catch (InterruptedException ex){}
       g.drawImage(image,10,10,null);
      }
     }
    glad 2 be of any help.

Posting Permissions

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



Click Here to Expand Forum to Full Width