Browse Source

Graph testing

Kevin Trometer 9 năm trước cách đây
mục cha
commit
35ccc57f57
1 tập tin đã thay đổi với 59 bổ sung18 xóa
  1. 59 18
      src/ui/view/UnitGraph.java

+ 59 - 18
src/ui/view/UnitGraph.java

@@ -5,6 +5,8 @@ import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
@@ -22,21 +24,25 @@ import classes.CpsObject;
 import ui.controller.Control;
 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 Control controller;
 	private Model model;
 	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) {
-		
+
 		this.controller = control;
 		this.model = model;
 
 		this.addMouseListener(this);
 		this.addMouseMotionListener(this);
+		this.addComponentListener(this);
 	}
 
 	/**
@@ -50,23 +56,26 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener {
 		g2 = (Graphics2D) g;
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
-		g2.setStroke(new BasicStroke(2));
-		
+		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();
+			x[i] = (int) pointList.get(i).getX();
+			y[i] = (int) pointList.get(i).getY();
 		}
-		
+
 		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
 	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
@@ -91,21 +100,53 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener {
 
 	@Override
 	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();
 	}
 
 	@Override
 	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) {
 	}
 
 }