|
@@ -5,6 +5,8 @@ import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.RenderingHints;
|
|
import java.awt.RenderingHints;
|
|
|
|
+import java.awt.event.ComponentEvent;
|
|
|
|
+import java.awt.event.ComponentListener;
|
|
import java.awt.event.MouseEvent;
|
|
import java.awt.event.MouseEvent;
|
|
import java.awt.event.MouseListener;
|
|
import java.awt.event.MouseListener;
|
|
import java.awt.event.MouseMotionListener;
|
|
import java.awt.event.MouseMotionListener;
|
|
@@ -22,21 +24,25 @@ import classes.CpsObject;
|
|
import ui.controller.Control;
|
|
import ui.controller.Control;
|
|
import ui.model.Model;
|
|
import ui.model.Model;
|
|
|
|
|
|
-class UnitGraph extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
|
|
|
+class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private static final long serialVersionUID = 1L;
|
|
private Control controller;
|
|
private Control controller;
|
|
private Model model;
|
|
private Model model;
|
|
private Graphics2D g2;
|
|
private Graphics2D g2;
|
|
- private ArrayList<Point> pointList = new ArrayList<>();
|
|
|
|
|
|
+ ArrayList<Point> pointList = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ private boolean pointDrag = false;
|
|
|
|
+ private Point tempP = null;
|
|
|
|
|
|
public UnitGraph(final Model model, Control control) {
|
|
public UnitGraph(final Model model, Control control) {
|
|
-
|
|
|
|
|
|
+
|
|
this.controller = control;
|
|
this.controller = control;
|
|
this.model = model;
|
|
this.model = model;
|
|
|
|
|
|
this.addMouseListener(this);
|
|
this.addMouseListener(this);
|
|
this.addMouseMotionListener(this);
|
|
this.addMouseMotionListener(this);
|
|
|
|
+ this.addComponentListener(this);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -50,23 +56,26 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener {
|
|
g2 = (Graphics2D) g;
|
|
g2 = (Graphics2D) g;
|
|
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
g2.setRenderingHints(rh);
|
|
g2.setRenderingHints(rh);
|
|
- g2.setStroke(new BasicStroke(2));
|
|
|
|
-
|
|
|
|
|
|
+ g2.setStroke(new BasicStroke(1));
|
|
|
|
+
|
|
int[] x = new int[10000];
|
|
int[] x = new int[10000];
|
|
int[] y = new int[10000];
|
|
int[] y = new int[10000];
|
|
for (int i = 0; i < pointList.size(); i++) {
|
|
for (int i = 0; i < pointList.size(); i++) {
|
|
- x[i] = (int)pointList.get(i).getX();
|
|
|
|
- y[i] = (int)pointList.get(i).getY();
|
|
|
|
|
|
+ x[i] = (int) pointList.get(i).getX();
|
|
|
|
+ y[i] = (int) pointList.get(i).getY();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
g2.drawPolygon(x, y, pointList.size());
|
|
g2.drawPolygon(x, y, pointList.size());
|
|
- g2.drawLine(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
|
|
|
|
|
|
+ g2.drawLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void mouseDragged(MouseEvent e) {
|
|
public void mouseDragged(MouseEvent e) {
|
|
- pointList.add(new Point(e.getX(), e.getY()));
|
|
|
|
- repaint();
|
|
|
|
|
|
+ //If Dragging relocate the Point
|
|
|
|
+ if (pointDrag) {
|
|
|
|
+ tempP.setLocation(e.getX(), e.getY());
|
|
|
|
+ repaint();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -91,21 +100,53 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void mousePressed(MouseEvent e) {
|
|
public void mousePressed(MouseEvent e) {
|
|
- if(e.getButton() == e.BUTTON1){
|
|
|
|
- pointList.add(new Point(e.getX(), e.getY()));
|
|
|
|
- } else if(e.getButton() == e.BUTTON3){
|
|
|
|
- for (Point p: pointList) {
|
|
|
|
- if (p.getX()-2>=e.getX()&& p.getX()+2<=e.getX() && p.getY()-2>=e.getY()&& p.getY()+2<=e.getY()) {
|
|
|
|
- pointList.remove(p);
|
|
|
|
|
|
+ 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");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ } catch (Exception e2) {
|
|
}
|
|
}
|
|
|
|
+ //Add a Point
|
|
|
|
+ if (!pointDrag && e.getButton() == e.BUTTON1) {
|
|
|
|
+ pointList.add(new Point(e.getX(), e.getY()));
|
|
|
|
+ }
|
|
|
|
+
|
|
repaint();
|
|
repaint();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void mouseReleased(MouseEvent e) {
|
|
public void mouseReleased(MouseEvent e) {
|
|
- // TODO Auto-generated method stub
|
|
|
|
|
|
+ //If Dragging
|
|
|
|
+ if (pointDrag) {
|
|
|
|
+ tempP = null;
|
|
|
|
+ pointDrag = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void componentResized(ComponentEvent e) {
|
|
|
|
+ // resize listener
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void componentHidden(ComponentEvent e) {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void componentMoved(ComponentEvent e) {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void componentShown(ComponentEvent e) {
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|