Previous | Next |
TileModel.java
TileModel extends the framework model class Model, and maintains the Tiles component data by storing a set of Tile objects. It uses a Vector object, essentially a growable array, to store the Tile objects. A Vector can contain zero or more objects.
In addition to standard data serialization methods, TileModel provides data access methods for:
TileModel also provides a helper method, findTile, that the component uses to determine when the user has clicked on a tile.
public class TileModel extends Model { |
|
Default constructor builds a model with no Tile objects | public TileModel() { } |
Specify the file extension | public String getFileExtension() { return "tile"; } |
Methods for accessing a specific tile or all tiles, and for getting a count of the tiles | public Vector getTiles() { return tiles; }public void setTiles(Vector tiles) { this.tiles = tiles; setModelChanged(true); notifyOfModelChange(null); }public Tile getTile(int index) { return (Tile)tiles.elementAt(index); }public int getTileCount() { return tiles.size(); } |
Methods for adding one or more tiles | public void addTile(Tile tile) { tiles.addElement(tile); tile.setSelected(true); setModelChanged(true); notifyOfModelChange(null); }public void addTiles(Vector v) { for (int i = 0; i < v.size(); i++) { Tile tile = (Tile)v.elementAt(i); tiles.addElement(tile); tile.setSelected(true); } setModelChanged(true); notifyOfModelChange(null); } |
Methods for removing one or more tiles | public void removeTile(Tile tile) { tiles.removeElement(tile); setModelChanged(true); notifyOfModelChange(null); }public void removeTiles(Vector v) { for (int i = 0; i < v.size(); i++) { Tile tile = (Tile)v.elementAt(i); tiles.removeElement(tile); } setModelChanged(true); notifyOfModelChange(null); } |
Determine whether a tile exists at the specified position and, if so, return it | public Tile findTile(Point p) { for (int i = tiles.size(); i > 0; i--) { Tile tile = (Tile)tiles.elementAt(i-1); if (tile.containsPoint(p)) { return tile; } } return null; } |
Methods for resetting the color or position data in specified tiles | public void setColor(Vector selectedTiles, Color color) { if (selectedTiles.isEmpty()) return; for (int i = 0; i < selectedTiles.size(); i++) ((Tile)selectedTiles.elementAt(i)).setColor(color); setModelChanged(true); notifyOfModelChange(null); }public void moveBy(Vector selectedTiles, Point delta) { if (selectedTiles.isEmpty()) return; for (int i = 0; i < selectedTiles.size(); i++) ((Tile)selectedTiles.elementAt(i)).moveBy(delta); setModelChanged(true); notifyOfModelChange(null); } |
Storage for the Tile objects | private Vector tiles = new Vector(); } |
Classes for Tiles sample:
Previous | Next |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 - 1997.
All Rights Reserved.