Sfoglia il codice sorgente

Reset Button and Maximum label

Kevin Trometer 8 anni fa
parent
commit
392ec50cad
1 ha cambiato i file con 66 aggiunte e 28 eliminazioni
  1. 66 28
      src/ui/view/UnitGraph.java

+ 66 - 28
src/ui/view/UnitGraph.java

@@ -17,6 +17,13 @@ import javax.swing.JPanel;
 
 import ui.controller.Control;
 import ui.model.Model;
+import java.awt.BorderLayout;
+import javax.swing.JButton;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import java.awt.FlowLayout;
+import javax.swing.SwingConstants;
+import javax.swing.JLabel;
 
 class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
 
@@ -26,23 +33,50 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private Graphics2D g2;
 	private CubicCurve2D c = new CubicCurve2D.Double();
 	private Point[] pointList;
-	
+
 	private boolean pointDrag = false;
 	private int tempP;
 	private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
+	private final JButton resetButton = new JButton("Reset Graph");
+	private final JLabel lblMaximum = new JLabel("Maximum");
 
 	public UnitGraph(final Model model, Control control) {
 		this.controller = control;
 		this.model = model;
-		
+
 		this.pointList = new Point[model.getIterations()];
 		for (int i = 0; i < pointList.length; i++) {
 			pointList[i] = new Point(0, 0);
 		}
-		
+
 		this.addMouseListener(this);
 		this.addMouseMotionListener(this);
 		this.addComponentListener(this);
+
+		lblMaximum.setText(lblMaximum.getText() + ": 100");
+		resetButton.setToolTipText("Resets the Graph");
+		resetButton.setBackground(Color.WHITE);
+
+		resetButton.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				reset();
+			}
+		});
+		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
+
+		add(lblMaximum);
+
+		add(resetButton);
+	}
+
+	/*
+	 * Resets the Graph
+	 */
+	public void reset() {
+		for (int i = 0; i < pointList.length; i++) {
+			pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
+		}
+		repaint();
 	}
 
 	/**
@@ -53,13 +87,13 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 */
 	public void paintComponent(Graphics g) {
 		super.paintComponent(g);
-		
+
 		g2 = (Graphics2D) g;
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 		g2.setStroke(new BasicStroke(1));
 
-		g2.setColor(Color.LIGHT_GRAY);
+		g2.setColor(new Color(240, 240, 240));
 		for (int i = 0; i < pointList.length; i++) {
 			g2.drawLine((int) pointList[i].getX(), 0, (int) pointList[i].getX(), this.getHeight());
 		}
@@ -71,20 +105,21 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			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;
+			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);
-			
-			
-			//draw the curve
+
+			// draw the curve
 			g2.draw(c);
 		}
 	}
@@ -93,14 +128,18 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	public void mouseDragged(MouseEvent e) {
 		if (pointDrag && e.getY() >= this.getHeight() / 3 && tempP != -1) {
 			pointList[tempP].setLocation(pointList[tempP].getX(), e.getY());
-			if(tempP!=0){
-				pointList[tempP-1].setLocation(pointList[tempP-1].getX(), pointList[tempP-1].getY()+(pointList[tempP].getY()-e.getY())/2);
+			if (tempP != 0 && pointList[tempP - 1].getY() < pointList[tempP].getY()
+					- (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
+				pointList[tempP - 1].setLocation(pointList[tempP - 1].getX(),
+						pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
 			}
-			if(tempP!=model.getIterations()-1){
-				pointList[tempP+1].setLocation(pointList[tempP+1].getX(), pointList[tempP+1].getY()+(pointList[tempP].getY()-e.getY())/2);
+			if (tempP != model.getIterations() - 1 && pointList[tempP + 1].getY() < pointList[tempP].getY()
+					- (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
+				pointList[tempP + 1].setLocation(pointList[tempP + 1].getX(),
+						pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
 			}
-		} else if(tempP != -1){
-			pointList[tempP].setLocation(pointList[tempP].getX(), this.getHeight()/3);
+		} else if (tempP != -1) {
+			pointList[tempP].setLocation(pointList[tempP].getX(), this.getHeight() / 3);
 		}
 		repaint();
 	}
@@ -128,7 +167,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	@Override
 	public void mousePressed(MouseEvent e) {
 		for (int i = 0; i < pointList.length; i++) {
-			if (e.getX() - this.getWidth() / model.getIterations()/ 2 <= pointList[i].getX()
+			if (e.getX() - this.getWidth() / model.getIterations() / 2 <= pointList[i].getX()
 					&& e.getX() + this.getWidth() / model.getIterations() / 2 >= pointList[i].getX()
 					&& e.getY() - 10 < pointList[i].getY() && e.getY() + 10 > pointList[i].getY()) {
 				tempP = i;
@@ -147,10 +186,9 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 	// resize listener
 	public void componentResized(ComponentEvent e) {
-		if(pointList[0].getY() != 0)
-		for (int i = 0; i < pointList.length; i++) {
-			pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), (int) pointList[i].getY());
-		} else {
+		if (pointList[0].getY() != 0)
+			reset();
+		else {
 			for (int i = 0; i < pointList.length; i++) {
 				pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
 			}