Package shapes

Source Code of shapes.Poly

package shapes;

import gui.JMFrame;
import gui.JMImagePanel;

import java.awt.Point;
import java.awt.Polygon;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class Poly extends Shape {
    private int numPoints;

    private Polygon shape;

    public boolean[] pointSelected;

    public Poly(int[] x, int[] y, int num) {
  this.shape = new Polygon(x, y, num);
  this.numPoints = num;
  this.pointSelected = new boolean[1];
  this.pointSelected[0] = false;
    }

    public void move(int moveX, int moveY) {
  int[] xPoints = ((Polygon) (this.shape)).xpoints;
  int[] yPoints = ((Polygon) (this.shape)).ypoints;
  for (int i = 0; i < this.numPoints; i++) {
      this.shape.xpoints[i] += moveX;
      this.shape.ypoints[i] += moveY;
  }
  this.shape = new Polygon(xPoints, yPoints, this.shape.npoints);
    }

    public void addPoint(int x, int y) {
  int[] xPoints = new int[((Polygon) this.shape).npoints + 1];
  int[] yPoints = new int[((Polygon) this.shape).npoints + 1];
  for (int i = 0; i < ((Polygon) this.shape).npoints; i++) {
      xPoints[i] = ((Polygon) this.shape).xpoints[i];
      yPoints[i] = ((Polygon) this.shape).ypoints[i];

  }
  xPoints[xPoints.length - 1] = x;
  yPoints[xPoints.length - 1] = y;
  ((Polygon) this.shape).npoints++;
  this.numPoints++;
  this.pointSelected = new boolean[numPoints];
  for (int i = 0; i < numPoints; i++) {
      pointSelected[i] = false;
  }
  ((Polygon) this.shape).xpoints = xPoints;
  ((Polygon) this.shape).ypoints = yPoints;
    }

    public Polygon getShape() {
  return this.shape;
    }

    public int closest(Point point, int tolerence) {
  int distance = tolerence;
  int closest = -1;
  for (int i = 0; i < this.shape.npoints; i++) {
      if ((int) (point.distance(new Point(this.shape.xpoints[i],
        this.shape.ypoints[i]))) < distance) {
    closest = i;
    distance = (int) (point.distance(new Point(
      this.shape.xpoints[i], this.shape.ypoints[i])));
      }
  }
  return closest;
    }

    public void movePoint(int[] i, int moveX, int moveY) {
  int[] xPoints = ((Polygon) (this.shape)).xpoints;
  int[] yPoints = ((Polygon) (this.shape)).ypoints;
  for (int j = 0; j < i.length; j++) {
      xPoints[i[j]] += moveX;
      yPoints[i[j]] += moveY;
  }

  this.shape = new Polygon(xPoints, yPoints, this.shape.npoints);
    }

    public void selectPoint(int i) {
  if (!(JMImagePanel.shiftPressed || (i >= 0 && pointSelected[i]))) {
      for (int j = 0; j < pointSelected.length; j++) {
    pointSelected[j] = false;
      }
  }
  if (i >= 0) {
      pointSelected[i] = (JMImagePanel.shiftPressed && pointSelected[i] ? false
        : true);
  } else {
      if (!JMImagePanel.shiftPressed) {
    for (int j = 0; j < pointSelected.length; j++) {
        pointSelected[j] = false;
    }
      }
  }
    }

    public int[] selected() {
  int numSelected = 0;
  for (int i = 0; i < pointSelected.length; i++) {
      if (pointSelected[i])
    numSelected++;
  }
  int[] selected = new int[numSelected];
  int selectedIndex = 0;
  for (int i = 0; i < pointSelected.length; i++) {
      if (pointSelected[i]) {
    selected[selectedIndex] = i;
    selectedIndex++;
      }

  }
  return selected;
    }

    public void deselectAll() {
  for (int i = 0; i < this.pointSelected.length; i++) {
      pointSelected[i] = false;
  }
    }

    public void deleteSelected(JMFrame parent) {
  int numSelected = 0;
  for (int i = 0; i < pointSelected.length; i++) {
      if (pointSelected[i])
    numSelected++;
  }
  numPoints -= numSelected;
  if (numPoints > 2) {
      int[] xPoints = new int[numPoints];
      int[] yPoints = new int[numPoints];
      int index = 0;
      for (int i = 0; i < shape.npoints; i++) {
    if (!pointSelected[i]) {
        xPoints[index] = shape.xpoints[i];
        yPoints[index] = shape.ypoints[i];
        index++;
    }
      }
      pointSelected = new boolean[numPoints];
      for (int i = 0; i < numPoints; i++) {
    pointSelected[i] = false;
      }
      this.shape = new Polygon(xPoints, yPoints, numPoints);
  } else {
      JOptionPane.showMessageDialog(parent,
        "Polygons Must Have At Least Three Points");
      numPoints += numSelected;
  }
    }

    public void divide(int[] selected, JMFrame parent) {
  if (selected.length == 2) {
      if (((int) Math.abs(selected[0] - selected[1])) == 1) {
    int[] xPoints = new int[numPoints + 1];
    int[] yPoints = new int[numPoints + 1];
    for (int i = 0; i <= Math.min(selected[0], selected[1]); i++) {
        xPoints[i] = this.shape.xpoints[i];
        yPoints[i] = this.shape.ypoints[i];
    }
    xPoints[Math.max(selected[0], selected[1])] = (this.shape.xpoints[selected[0]] + this.shape.xpoints[selected[1]]) / 2;
    yPoints[Math.max(selected[0], selected[1])] = (this.shape.ypoints[selected[0]] + this.shape.ypoints[selected[1]]) / 2;
    for (int i = Math.max(selected[0], selected[1]); i < this.shape.npoints; i++) {
        xPoints[i + 1] = this.shape.xpoints[i];
        yPoints[i + 1] = this.shape.ypoints[i];
    }
    this.shape = new Polygon(xPoints, yPoints, ++numPoints);
    this.pointSelected = new boolean[numPoints];
    for (int i = 0; i < numPoints; i++) {
        this.pointSelected[i] = false;
    }
    this.pointSelected[Math.max(selected[0], selected[1])] = true;
      } else {
    int min = Math.min(selected[0], selected[1]);
    int max = Math.max(selected[0], selected[1]);
    if (min == 0 && max == this.numPoints - 1) {
        this.addPoint(
            (this.shape.xpoints[0] + this.shape.xpoints[this.shape.npoints - 1]) / 2,
            (this.shape.ypoints[0] + this.shape.ypoints[this.shape.npoints - 1]) / 2);
        this.pointSelected[this.numPoints-1] = true;
    } else {
        errorMessage("Please Select Exactly Two Adjacent Vertices",
          parent);
    }
      }
  } else {
      errorMessage("Please Select Exactly Two Adjacent Vertices", parent);
  }
    }

    public int[] getSelectedPoints() {
  int numSelected = 0;
  for (int i = 0; i < pointSelected.length; i++) {
      if (pointSelected[i])
    numSelected++;
  }
  int[] selected = new int[numSelected];
  int index = 0;
  for (int i = 0; i < pointSelected.length; i++) {
      if (pointSelected[i]) {
    selected[index] = i;
    index++;
      }

  }
  return selected;
    }

    public void errorMessage(String message, JMFrame parent) {
  JOptionPane.showMessageDialog(parent, message);
    }
}
TOP

Related Classes of shapes.Poly

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.