Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Load Images in Multi-Class Application

Posted on 1997-05-07
4
Medium Priority
?
194 Views
Last Modified: 2010-04-16
*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
       

0
Comment
Question by:wawa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220226
Are you writting this on an Applet or an aplication?
0
 

Author Comment

by:wawa
ID: 1220227
This is a stand-alone application.
0
 

Author Comment

by:wawa
ID: 1220228
Edited text of question
0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 300 total points
ID: 1220229
From the title of your question I assume that the File dialog call is in one class and the display in another.

If this is the case, then the problem you are having stems from the fact that you do not have a common context for the image you read in and the image you want to display. I.e.: Java does not allow global variables, so the Image beeing loaded is local to the class that loads it and not available to the class that wants to display it.

Any particular reason for this (Simple yes/no will do)?

Is calling the dialog from within the class that displays it acceptable? If thats the case, you could simply move the code that reads the file into the class that displays it.

If not, have you considered defining a class that contains a static Image and instantiating it in both classes so that you have a common memory address?

Such a class would simulate global memory.

Example:

public void imageXfer(){
   static Image Img;

    imageXfr(){
    // Do nothing just instantiate
    }
}

And then instantiate and use in your classes, as in

...
   imageXfr MyImg = new(imageXfr);
...
   MyImg = eScreen.Image;

and using a similar technique in the display section yo could do:


...

imageXfr MyImg = new(imageXfr); // the Image contained is static so it will remain unchanged in both classes

image = imageXfr.image;


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question