Link to home
Start Free TrialLog in
Avatar of wawa
wawa

asked on

Load Images in Multi-Class Application

*Note: This is a stand-alone GUI application

I am programming a "Load" button in my application to load a GIF image onto a canvas.  The Load event looks like:
(Note that eScreen is the name of the Screen container canvas class I created so I can draw on an image)

      else if ("Load".equals(o)){
        FileDialog fd = new FileDialog(this, "Load Image", FileDialog.LOAD);
        fd.setDirectory(".");
        fd.show();
       
        filename = fd.getFile();
        eScreen.image = Toolkit.getDefaultToolkit().getImage(filename);

        eScreen.imageUpdate(eScreen.image, ALLBITS , 0, 0, 256, 256);
      }
The following is in the ScreenContainer class.  
I've followed an image loading example from Core Java by Sunsoft Press, but it does not
paint my image when loaded.  The relevant code is as follows:

  Image image = null;
  int buffer_width = 0;
  int buffer_height = 0;
  int image_width = 0;
  int image_height = 0;
  Image buffered_image;


  public void paint(Graphics g){
    if(image != null){
      if (image_width <=0 || image_height <= 0){
        buffered_image = createImage(1,1);
        Graphics bg = buffered_image.getGraphics();
        bg.drawImage(image, 0, 0, this);
        bg.dispose();
        return;
      }
     
      Dimension d = size();
      Insets in = insets();
     
      int client_width = d.width - in.right - in.left;
      int client_height = d.height - in.bottom - in.top;
     
      if(client_width > buffer_width || client_height > buffer_height){
        //size has increased
        buffer_width = client_width;
        buffer_height = client_height;
       
        buffered_image = createImage(buffer_width, buffer_height);
        Graphics bg = buffered_image.getGraphics();
        bg.drawImage(image, 0, 0, null);
        for (int i=0; i< buffer_width/image_width; i++){
          for (int j=0; j< buffer_height/image_height; j++){
            if(i + j > 0) bg.copyArea(0, 0, image_width, image_height,
i*image_width,
                                      j*image_height);
          }
        }
        bg.dispose();
      }
     
      g.drawImage(buffered_image, 0, 0, this);

    }
    else{
      g.drawRect(0,0,255,255);
      g.setColor(Color.orange);
      g.fillRect(0,0,255,255);
    }
  }
 
  public void update(Graphics g)
  { //overrides the update function so just paints image --Don't use it though!
    paint(g);
  }

  public boolean imageUpdate(Image img, int infoflags, int x, int y, int width,
                             int height){
    if((infoflags & ImageObserver.ALLBITS) != 0){    //image is complete
      image_width = image.getWidth(null);
      image_height = image.getHeight(null);
      repaint();
      return false;
    }
    return true;  //want more info
  }

  public Insets insets(){
    return new Insets(10,10,10,10);
  }
}


Thanks for any help,
Linda
       

Avatar of jpk041897
jpk041897

Are you writting this on an Applet or an aplication?
Avatar of wawa

ASKER

This is a stand-alone application.
Avatar of wawa

ASKER

Edited text of question
ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial