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

Source Code of net.sf.arianne.marboard.client.gui.component.DrawingArea

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

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

import net.sf.arianne.marboard.client.core.CurrentBoard;
import net.sf.arianne.marboard.client.core.UpdateObserver;
import net.sf.arianne.marboard.client.entity.shape.Shape;
import net.sf.arianne.marboard.client.gui.BoardState;

/**
* Manages the visible whiteboard window and draws the shapes
*
* @author hendrik
*/
public class DrawingArea extends JComponent implements UpdateObserver {
  private static final long serialVersionUID = 7881194467004549654L;

  private CurrentBoard board;

  private Shape temporaryShape;


  /**
   * creates a new DrawingArea
   *
   * @param board the whiteboard with the shape information
   * @param boardState state of the board
   */
  public DrawingArea(CurrentBoard board, BoardState boardState) {
    this.board = board;
    board.setObserver(this);
    CanvasMouseHandler mouseHandler = new CanvasMouseHandler(boardState);
    addMouseListener(mouseHandler);
    addMouseMotionListener(mouseHandler);
  }

  /**
   * draws all the shapes
   *
   * @param g the graphics element to draw on
   */
  @Override
  protected void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());

    for (Shape shape : board) {
      shape.draw((Graphics2D) g);
    }
  }

  public void update() {
    repaint();
  }

  /**
   * draws a temporary shape for preview
   *
   * @param shape
   */
  public void drawTemporaryShape(Shape shape) {
    undrawTemporaryShape();
    this.temporaryShape = shape;
    Graphics graphics = this.getGraphics();
    graphics.setXORMode(Color.WHITE);
    this.temporaryShape.draw((Graphics2D) graphics);
  }


  /**
   * removes a temporary drawn shape
   */
  public void undrawTemporaryShape() {
    if(this.temporaryShape != null) {
      Graphics graphics1 = this.getGraphics();
      graphics1.setXORMode(Color.WHITE);
      this.temporaryShape.draw((Graphics2D) graphics1);
      this.temporaryShape = null;
    }
  }


  /**
   * picks the shape at the specified coordinates
   *
   * @param x x-coordinate
   * @param y y-coordinate
   * @return Shape or <code>null</code> if there is none
   */
  public Shape pickShape(int x, int y) {
    Shape foundShape = null;
    for (Shape shape : board) {
      if (shape.isAt(x, y)) {
        foundShape = shape;
      }
    }
    return foundShape;
  }

}
TOP

Related Classes of net.sf.arianne.marboard.client.gui.component.DrawingArea

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.