Package net.sf.arianne.marboard.client.gui.drawingtool

Source Code of net.sf.arianne.marboard.client.gui.drawingtool.RectangleDrawingTool

package net.sf.arianne.marboard.client.gui.drawingtool;

import java.awt.event.MouseEvent;

import marauroa.common.game.RPAction;
import marauroa.common.game.RPObject;
import net.sf.arianne.marboard.client.core.MarboardClientFramework;
import net.sf.arianne.marboard.client.entity.shape.Rectangle;
import net.sf.arianne.marboard.client.entity.shape.Shape;
import net.sf.arianne.marboard.client.gui.BoardState;
import net.sf.arianne.marboard.client.gui.DrawingTool;
import net.sf.arianne.marboard.client.gui.component.DrawingArea;

/**
* draws an rectangle
*
* @author hendrik
*/
public class RectangleDrawingTool implements DrawingTool {
 
  private boolean firstClick = true;
  private int startX;
  private int startY;
  private DrawingArea board;

  /**
   * creates a new RectangleDrawingTool
   *
   * @param board
   */
  public RectangleDrawingTool(DrawingArea board) {
    super();
    this.board = board;
  }

  /**
   * handles mouse events. On the first click the position is remembered
   * and on the second click the rectangle is drawn
   *
   * @param boardState state of the board
   * @param event MouseEvent
   */
  public void handleMouseEvent(BoardState boardState, MouseEvent event) {
    if (firstClick) {
      startX = event.getX();
      startY = event.getY();
      firstClick = false;
    } else {
      handleSecondEvent(boardState, event);
    }
  }

  /**
   * tells the server to draw the rectangle. The selected color,
   * fill color and thickness are used.
   *
   * @param boardState state of the board
   * @param event MouseEvent
   */
  private void handleSecondEvent(BoardState boardState, MouseEvent event) {
    RPAction action = new RPAction();
    action.put("type", "create_rectangle_action");
    action.put("x", startX);
    action.put("y", startY);
    action.put("x2", event.getX());
    action.put("y2", event.getY());
    action.put("color", boardState.getColor().getRGB());
    action.put("fill_color", boardState.getFillColor().getRGB());
    action.put("thickness", boardState.getThickness());
    MarboardClientFramework.executeAction(action);
    board.undrawTemporaryShape();
    reset();
  }

  /**
   * resets the tool
   */
  public void reset() {
    firstClick = true;
  }

  /**
   * draws a preview version of the rectangle when the mouse is moved
   * after the first click. The preview shape is drawn using xor so that
   * simply drawing it again will delte it.
   *
   * @param boardState state of the board
   * @param event MouseEvent
   */
  public void handleMouseMovement(BoardState boardState, MouseEvent event) {
    if(!firstClick) {
      RPObject rpobject = new RPObject();
      rpobject.put("color", boardState.getColor().getRGB());
      rpobject.put("fill_color", boardState.getFillColor().getRGB());
      rpobject.put("thickness", boardState.getThickness());
      rpobject.put("x", Math.min(startX, event.getX()));
      rpobject.put("y", Math.min(startY, event.getY()));
      rpobject.put("x2", Math.max(startX, event.getX()));
      rpobject.put("y2", Math.max(startY, event.getY()));
      Shape shape = new Rectangle(rpobject);
      board.drawTemporaryShape(shape);
    }
  }

}
TOP

Related Classes of net.sf.arianne.marboard.client.gui.drawingtool.RectangleDrawingTool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.