|
@@ -26,20 +26,24 @@ import ui.model.Model;
|
|
|
|
|
|
class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
|
|
|
|
|
|
+ private final int NUMBER = 50;
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
private Control controller;
|
|
|
private Model model;
|
|
|
private Graphics2D g2;
|
|
|
- ArrayList<Point> pointList = new ArrayList<>();
|
|
|
+ Point[] pointList = new Point[NUMBER];
|
|
|
|
|
|
private boolean pointDrag = false;
|
|
|
- private Point tempP = null;
|
|
|
+ private int tempP;
|
|
|
|
|
|
public UnitGraph(final Model model, Control control) {
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
this.controller = control;
|
|
|
this.model = model;
|
|
|
-
|
|
|
+ for (int i = 0; i < pointList.length; i++) {
|
|
|
+ pointList[i] = new Point(0,0);
|
|
|
+ }
|
|
|
this.addMouseListener(this);
|
|
|
this.addMouseMotionListener(this);
|
|
|
this.addComponentListener(this);
|
|
@@ -58,24 +62,19 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
g2.setRenderingHints(rh);
|
|
|
g2.setStroke(new BasicStroke(1));
|
|
|
|
|
|
- int[] x = new int[10000];
|
|
|
- int[] y = new int[10000];
|
|
|
- for (int i = 0; i < pointList.size(); i++) {
|
|
|
- x[i] = (int) pointList.get(i).getX();
|
|
|
- y[i] = (int) pointList.get(i).getY();
|
|
|
+ for (int i = 0; i < pointList.length-1; i++) {
|
|
|
+ g2.drawLine((int)pointList[i].getX(), (int)pointList[i].getY(), (int)pointList[i+1].getX(), (int)pointList[i+1].getY());
|
|
|
}
|
|
|
-
|
|
|
- g2.drawPolygon(x, y, pointList.size());
|
|
|
- g2.drawLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);
|
|
|
+ g2.drawLine((int)pointList[NUMBER-1].getX(), (int)pointList[NUMBER-1].getY(), this.getWidth(), this.getHeight()/2);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void mouseDragged(MouseEvent e) {
|
|
|
- //If Dragging relocate the Point
|
|
|
if (pointDrag) {
|
|
|
- tempP.setLocation(e.getX(), e.getY());
|
|
|
- repaint();
|
|
|
+ pointList[tempP].setLocation(e.getX(), e.getY());
|
|
|
+ System.out.println("drag");
|
|
|
}
|
|
|
+ repaint();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -99,42 +98,29 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void mousePressed(MouseEvent e) {
|
|
|
- try {
|
|
|
- //Look if a point is at the Mouse Position
|
|
|
- for (Point p : pointList) {
|
|
|
- if (p.getX() - 10 < e.getX() && p.getX() + 10 > e.getX() && p.getY() - 10 < e.getY()
|
|
|
- && p.getY() + 10 >= e.getY()) {
|
|
|
- if (e.getButton() == e.BUTTON1) { //Drag the found Point
|
|
|
- pointDrag = true;
|
|
|
- tempP = p;
|
|
|
- } else if (e.getButton() == e.BUTTON3) { // Rightclick Remove a Point
|
|
|
- pointList.remove(p);
|
|
|
- System.out.println("remove");
|
|
|
- }
|
|
|
- }
|
|
|
+ public void mousePressed(MouseEvent e){
|
|
|
+ for (int i = 0; i < pointList.length; i++) {
|
|
|
+ if(e.getX()-5<pointList[i].getX() && e.getX()+5>pointList[i].getX() && e.getY()-5<pointList[i].getY() && e.getY()+5>pointList[i].getY()){
|
|
|
+ pointDrag = true;
|
|
|
+ tempP = i;
|
|
|
}
|
|
|
- } catch (Exception e2) {
|
|
|
}
|
|
|
- //Add a Point
|
|
|
- if (!pointDrag && e.getButton() == e.BUTTON1) {
|
|
|
- pointList.add(new Point(e.getX(), e.getY()));
|
|
|
- }
|
|
|
-
|
|
|
- repaint();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
- //If Dragging
|
|
|
if (pointDrag) {
|
|
|
- tempP = null;
|
|
|
pointDrag = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void componentResized(ComponentEvent e) {
|
|
|
// resize listener
|
|
|
+ System.out.println("resize");
|
|
|
+ for (int i = 0; i < pointList.length; i++) {
|
|
|
+ pointList[i] = new Point((i)*this.getWidth()/NUMBER,this.getHeight()/2);
|
|
|
+ }
|
|
|
+ repaint();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -143,6 +129,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
|
|
|
@Override
|
|
|
public void componentMoved(ComponentEvent e) {
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|