[messages] [Developers] Mapping piece position in draw()

djsullivan davidsullivan317 at gmail.com
Tue Aug 27 13:39:15 MST 2013

I'm building a custom component and would like to draw on the map
"above" a selected game piece. I've implemented the Drawable interface
and have everything wired up so I know what piece is selected and I can
draw to the map, but I'm struggling to figure out how to map the piece's
location to the Graphics context. I'll need to account for zooming, etc.

So what should Center be in the following code... 

GamePiece[] gamePieces = map.getPieces();
Point center;
        for (int i = 0; i < gamePieces.length; ++i) {
            if (gamePieces[i] instanceof Stack) {
                for (Iterator<GamePiece> it = ((Stack)
gamePieces[i]).getPiecesIterator();it.hasNext();) {
                    GamePiece child = it.next();
if(child.getProperty(Properties.SELECTED).equals(Boolean.TRUE)) {
                        center = ???;
            else  {
                    center = ???;

... so when I draw my component "Center" is the center of the selected
piece in the graphics context. 

    public void draw(Graphics g, Map map) {

        if (center != null) {

            // draw a circle around the selected piece
            Graphics2D gg = (Graphics2D) g;
            gg.setStroke(new BasicStroke(3));

Thanks, David

Read this topic online here:

More information about the messages mailing list