View Javadoc

1   package erland.game.tileadventure;
2   /*
3    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *
19   */
20  
21  import erland.util.SubImageHandler;
22  import erland.util.ParameterValueStorageExInterface;
23  import erland.util.StringUtil;
24  
25  import java.awt.*;
26  
27  public class BitmapObject extends GameObject {
28      private int subImage;
29      private Image image;
30      private SubImageHandler subImages;
31      private String imageName;
32      public static int drawCount;
33      /***
34       * Set the background image
35       * @param image The main image
36       * @param subImage The sub image number of the sub image to use
37       */
38      public void setImage(Image image, int subImage) {
39          this.subImage = subImage;
40          this.image = image;
41          this.subImages = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).createSubImageHandler(this.image);
42      }
43  
44      /***
45       * Set the background image
46       * @param image The name of the main image
47       * @param subImage The sub image number of the sub image to use
48       */
49      public void setImage(String image, int subImage)
50      {
51          if(getEnvironment().getImageHandler()!=null) {
52              setImage(getEnvironment().getImageHandler().getImage(image),subImage);
53          }
54          this.imageName = image;
55      }
56  
57      public void draw(Graphics g) {
58          if(subImages!=null) {
59              if(getContainer().getVisible(getPosX(),getPosY(),getPosZ())) {
60                  //System.out.println("Draw at "+getDrawingPosX()+","+getDrawingPosY()+"="+getPosX()+","+getPosY()+","+getPosZ());
61                  if(subImage>=0) {
62                      drawCount++;
63                      subImages.drawImage(g,subImage,getDrawingPosX(0f,0f,0f),getDrawingPosY(0f,0f,0f));
64                  }
65              }
66          }
67      }
68  
69      public void write(ParameterValueStorageExInterface out) {
70          super.write(out);
71          if(StringUtil.asNull(imageName)!=null) {
72              out.setParameter("image",imageName);
73          }
74          out.setParameter("subimage",Integer.toString(subImage));
75      }
76  
77      public void read(ParameterValueStorageExInterface in) {
78          super.read(in);
79          int subImage = Integer.valueOf(in.getParameter("subimage")).intValue();
80          setImage(image,subImage);
81      }
82  }