Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jdstar/Display.java


1   /*
2    * Copyright (C) 1999-2001 Max Gilead <gilead@linart.pl>
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  package jdstar;
19  
20  import xmage.raster.RasterImage;
21  import xmage.raster.ImageAdapter;
22  import xmage.raster.filters.ImageOps;
23  import javax.swing.JFrame;
24  import javax.swing.JPanel;
25  import java.awt.Component;
26  import java.awt.Dimension;
27  import java.awt.Graphics;
28  import java.awt.Image;
29  
30  public class Display
31    extends JPanel
32    {
33    private static final int TSIZE = Tileset.TILE_SIZE;
34    private Board board = null;
35    private JFrame frame = null;
36  
37    public Display(Board board, JFrame frame)
38      {
39      this.board = board;
40      this.frame = frame;
41      board.display = this;
42      }
43    int[][] b = null;
44    int w = 0;
45    int h = 0;
46    Tileset t = null;
47    
48    RasterImage boraster = null;
49    Image boimage = null;
50    Image plimage = null;
51    Image buimage = null;
52    
53    /**
54     * Called whenever whole board must be refreshed, ie. when new board is loaded or tileset changes.
55     * All necessary data is taken directly from the board.
56     */
57    public void refreshBoard()
58      {
59      b = board.board;
60      w = b[0].length;
61      h = b.length;
62      t = board.tileset;
63      
64      boraster = new RasterImage(w * Tileset.TILE_SIZE, h * Tileset.TILE_SIZE);
65      for (int y = 0; y < h; y++)
66        for (int x = 0; x < w; x++)
67          {
68          switch(b[y][x])
69            {
70            case Board.BUDDY:
71            case Board.PLAYER:
72            case Board.EMPTY:  ImageOps.paste(t.empty, 0, 0, boraster, x * TSIZE, y * TSIZE,
73                        TSIZE, TSIZE); break;
74            case Board.WALL:   ImageOps.paste(t.wall, 0, 0, boraster, x * TSIZE, y * TSIZE,
75                        TSIZE, TSIZE); break;
76            case Board.ITEM:   ImageOps.paste(t.item, 0, 0, boraster, x * TSIZE, y * TSIZE,
77                        TSIZE, TSIZE); break;
78            }
79          }
80      plimage = new ImageAdapter(t.player).getImage();
81      buimage = new ImageAdapter(t.buddy).getImage();
82      boimage = new ImageAdapter(boraster).getImage();
83      setMinimumSize(new Dimension(boraster.getWidth(), boraster.getHeight()));
84      setPreferredSize(new Dimension(boraster.getWidth(), boraster.getHeight()));
85      setMaximumSize(new Dimension(boraster.getWidth(), boraster.getHeight()));
86      frame.pack();
87      }
88  
89    public void tileUpdated(int x, int y)
90      {
91      switch(b[y][x])
92        {
93        case Board.BUDDY:
94        case Board.PLAYER:
95        case Board.EMPTY:  ImageOps.paste(t.empty, 0, 0, boraster, x * TSIZE, y * TSIZE,
96                    TSIZE, TSIZE); break;
97        case Board.WALL:   ImageOps.paste(t.wall, 0, 0, boraster, x * TSIZE, y * TSIZE,
98                    TSIZE, TSIZE); break;
99        case Board.ITEM:   ImageOps.paste(t.item, 0, 0, boraster, x * TSIZE, y * TSIZE,
100                   TSIZE, TSIZE); break;
101       }
102     boimage = new ImageAdapter(boraster).getImage();
103     }
104 
105   public void paint(Graphics g)
106     {
107     super.paint(g);
108     if (b == null)
109       return;
110     g.drawImage(boimage, 0, 0, null);
111     g.drawImage(plimage, (int)((board.player.X - 0.5) * TSIZE), (int)((board.player.Y - 0.5) * TSIZE),
112       null);
113     g.drawImage(buimage, (int)((board.buddy.X - 0.5) * TSIZE), (int)((board.buddy.Y - 0.5) * TSIZE),
114       null);
115     }
116   }
117