Previous Next

Tiles Sample - Classes

Tile.java

Tile is used to represent individual Tile objects. Tile implements the Serializable interface, allowing Tile objects to be read in and out of persistent storage. Each Tile is defined by:

Each Tile object also keeps track of whether it is currently selected. The Tile class provides a draw method that is called to display the tile, including selection feedback if the tile is selected.

 
public class Tile implements Serializable {
Constants defining the set of tile types
public static final int kRock   = 0;
public static final int	kPaper	= 1;
public static final int	kScissor= 2;
Constructor requires a tile type, color, position, and size
Tile(int type, Color color, Point position, Dimension size) {
    this.type	= type;
    this.color  = color;
    this.bounds = new Rectangle(position.x, position.y, size.width, size.height);
}
Methods to access color and position data
public Color getColor() {
    return color;
}
public void setColor(Color color) {
    this.color = color;
}

public Point getPosition() {
    return new Point(bounds.x, bounds.y);
}
public void setPosition(Point position) {
    bounds.setLocation(position.x, position.y);
}
public void moveBy(Point delta) {
    bounds.translate(delta.x, delta.y);
}
Methods to determine whether the tile is currently selected
public boolean isSelected() {
    return selected;
}
public void setSelected(boolean selected) {
    this.selected = selected;
}
Method used during hit-detection to determine whether the tile was clicked on
public boolean containsPoint(Point point) {
    return (bounds.contains(point.x, point.y));
}
Draw the tile based on its type, color, and position
public void draw(Graphics g) {
    Rectangle r = bounds;
    Polygon polygon = null;

    g.setColor(getColor());
    switch (type) {
        case Tile.kRock:	//ellipse
            g.fillOval(r.x, r.y, r.width, r.height);
            break;
        case Tile.kPaper:	//rectangle
            g.fillRect(r.x, r.y, r.width, r.height);
            break;
        case Tile.kScissor:	//triangle
            int array_x[] = {r.x, r.x+r.width, r.x+(r.width/2), r.x};
            int array_y[] = {r.y, r.y, r.y+r.height, r.y};
            polygon = new Polygon(array_x, array_y, 4);
            g.fillPolygon(polygon);
            break;
    }
Draw selection feedback (a black outline) if the tile is currently selected
    if (selected) {
        g.setColor (Color.black);
        switch (type) {
            case Tile.kRock:	//ellipse
                g.drawOval(r.x, r.y, r.width, r.height);
                g.drawOval(r.x+1, r.y+1, r.width-2, r.height-2);
                break;
            case Tile.kPaper:	//rectangle
                g.drawRect(r.x, r.y, r.width, r.height);
                g.drawRect(r.x-1, r.y-1, r.width+2, r.height+2);
                break;
            case Tile.kScissor:	//triangle
                g.drawPolygon(polygon);
                break;
        }
    }
}
Data describing the tile
private int	    type;
private Color	    color;
private Rectangle   bounds;
private transient boolean	    selected;
}

Classes for Tiles sample:


Previous Next

Copyright © Taligent, Inc. 1996 - 1997.
Copyright
© IBM Corporation 1996 - 1997.
All Rights Reserved.