|
@@ -10,6 +10,7 @@ import java.awt.event.ComponentListener;
|
|
|
import java.awt.event.MouseEvent;
|
|
|
import java.awt.event.MouseListener;
|
|
|
import java.awt.event.MouseMotionListener;
|
|
|
+import java.awt.geom.CubicCurve2D;
|
|
|
import java.awt.geom.QuadCurve2D;
|
|
|
import java.util.ArrayList;
|
|
|
import java.awt.Point;
|
|
@@ -32,20 +33,19 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
private Control controller;
|
|
|
private Model model;
|
|
|
private Graphics2D g2;
|
|
|
- private QuadCurve2D q = new QuadCurve2D.Float();
|
|
|
+ private CubicCurve2D c = new CubicCurve2D.Double();
|
|
|
Point[] pointList = new Point[NUMBER];
|
|
|
|
|
|
private boolean pointDrag = false;
|
|
|
private int tempP;
|
|
|
- private int x;
|
|
|
- private int y;
|
|
|
+ private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
|
|
|
|
|
|
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);
|
|
|
+ pointList[i] = new Point(0, 0);
|
|
|
}
|
|
|
this.addMouseListener(this);
|
|
|
this.addMouseMotionListener(this);
|
|
@@ -64,26 +64,47 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
g2.setRenderingHints(rh);
|
|
|
g2.setStroke(new BasicStroke(1));
|
|
|
-
|
|
|
+
|
|
|
g2.setColor(Color.LIGHT_GRAY);
|
|
|
for (int i = 0; i < pointList.length; i++) {
|
|
|
- g2.drawLine((int)pointList[i].getX(), 0, (int)pointList[i].getX(), this.getHeight());
|
|
|
+ g2.drawLine((int) pointList[i].getX(), 0, (int) pointList[i].getX(), this.getHeight());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
g2.setColor(Color.BLACK);
|
|
|
g2.setStroke(new BasicStroke(2));
|
|
|
- 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());
|
|
|
+ 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());
|
|
|
+
|
|
|
+ //Set up the Curve
|
|
|
+ x1 = (int) pointList[i].getX();
|
|
|
+ y1 = (int) pointList[i].getY();
|
|
|
+ x2 = (int) pointList[i + 1].getX();
|
|
|
+ y2 = (int) pointList[i + 1].getY();
|
|
|
+ ctrlx1 = (int) pointList[i].getX()+((int) pointList[i+1].getX()-(int) pointList[i].getX())/2;
|
|
|
+ ctrlx2 = (int) pointList[i+1].getX()-((int) pointList[i+1].getX()-(int) pointList[i].getX())/2;
|
|
|
+ if(y1<y2){
|
|
|
+ ctrly1 = (int) pointList[i].getY()+((int) pointList[i+1].getY()-(int) pointList[i].getY())/10;
|
|
|
+ ctrly2 = (int) pointList[i+1].getY()-((int) pointList[i+1].getY()-(int) pointList[i].getY())/10;
|
|
|
+ } else{
|
|
|
+ ctrly1 = (int) pointList[i].getY()-((int) pointList[i].getY()-(int) pointList[i+1].getY())/10;
|
|
|
+ ctrly2 = (int) pointList[i+1].getY()+((int) pointList[i].getY()-(int) pointList[i+1].getY())/10;
|
|
|
+ }
|
|
|
+
|
|
|
+ c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
|
|
|
|
|
|
- //q.setCurve((int)pointList[i].getX(), (int)pointList[i].getY(),(int)pointList[i].getX(), (int)pointList[i].getY(),(int)pointList[i+1].getX(), (int)pointList[i+1].getY());
|
|
|
- //g2.draw(q);
|
|
|
+
|
|
|
+ //draw the curve
|
|
|
+ g2.draw(c);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void mouseDragged(MouseEvent e) {
|
|
|
- if (pointDrag && e.getY()>= this.getHeight()/3) {
|
|
|
+ if (pointDrag && e.getY() >= this.getHeight() / 3) {
|
|
|
pointList[tempP].setLocation(pointList[tempP].getX(), e.getY());
|
|
|
+ } else {
|
|
|
+ pointList[tempP].setLocation(pointList[tempP].getX(), this.getHeight()/3);
|
|
|
}
|
|
|
repaint();
|
|
|
}
|
|
@@ -109,9 +130,11 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void mousePressed(MouseEvent e){
|
|
|
+ public void mousePressed(MouseEvent e) {
|
|
|
for (int i = 0; i < pointList.length; i++) {
|
|
|
- if(e.getX()-this.getWidth()/NUMBER/2<=pointList[i].getX() && e.getX()+this.getWidth()/NUMBER/2>=pointList[i].getX() && e.getY()-10<pointList[i].getY() && e.getY()+10>pointList[i].getY()){
|
|
|
+ if (e.getX() - this.getWidth() / NUMBER / 2 <= pointList[i].getX()
|
|
|
+ && e.getX() + this.getWidth() / NUMBER / 2 >= pointList[i].getX()
|
|
|
+ && e.getY() - 10 < pointList[i].getY() && e.getY() + 10 > pointList[i].getY()) {
|
|
|
pointDrag = true;
|
|
|
tempP = i;
|
|
|
}
|
|
@@ -127,9 +150,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
|
|
|
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-1),this.getHeight()/3);
|
|
|
+ pointList[i] = new Point((i) * this.getWidth() / (NUMBER - 1), this.getHeight() / 3);
|
|
|
}
|
|
|
repaint();
|
|
|
}
|
|
@@ -140,7 +162,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
|
|
|
@Override
|
|
|
public void componentMoved(ComponentEvent e) {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|