Explorar el Código

adding slider logic, except the min/max values for the slider itself

tarfeef101 hace 6 años
padre
commit
d987c8e886

+ 1 - 1
assignments/a2/src/main/java/Controls.java

@@ -25,7 +25,7 @@ public class Controls extends JPanel implements Observer
       {
         if (!playback.getSettingLen() && playback.getValue() != model.getThingies().size())
         {
-          if (playback.getValue() >= 1) model.changeThingy(playback.getValue() - 1);
+          model.changeThingy(playback.getValue());
         }
       }
     });

+ 50 - 7
assignments/a2/src/main/java/Model.java

@@ -12,6 +12,7 @@ public class Model extends Observable
   private Thingy curThingy;
   private Color curColour;
   private int curThiccness;
+  private int totalLen;
   public Color sideColour;
   public Color controlColour;
 
@@ -22,6 +23,7 @@ public class Model extends Observable
     this.thingies = new ArrayList();
     this.curColour = Color.black;
     this.curThiccness = 5;
+    this.totalLen = 0;
     setChanged();
   }
   
@@ -30,6 +32,7 @@ public class Model extends Observable
     curThingy = new Thingy(curColour, curThiccness);
     thingies.add(curThingy);
     curThingy.addPoint(x, y);
+    totalLen++;
     setChanged();
     notifyObservers();
   }
@@ -37,24 +40,46 @@ public class Model extends Observable
   public void addPoint(int x, int y)
   {
     curThingy.addPoint(x, y);
+    totalLen++;
     setChanged();
     notifyObservers();
   }
   
+  public int getTotalLen()
+  {
+    return totalLen;
+  }
+  
   public ArrayList<Thingy> getThingies()
   {
     return thingies;
   }
   
-  public void changeThingy(int x)
+  public void changeThingy(int x) // int x is the position in the slider we are at
   {
-    curThingy = thingies.get(x);
-    if (thingies.size() >= 2)
+    int tempLen = 0;
+    int index;
+    
+    for (int i = 0; i < thingies.size() && tempLen <= x; i++)
+    {
+      tempLen += thingies.get(i).getVisisbleLen();
+      index = i;
+    }
+    
+    Thingy tempThingy = thingies.get(index);
+    // off by one adjustments if we're at the end of a thingy
+    if (tempThingy.getVisisbleLen() == 0 && tempLen > x) tempThingy = thingies.get(index - 1);
+    if (tempThingy.getVisisbleLen() == tempThingy.getPoints().size() && tempLen < x) tempThingy = thingies.get(index + 1);
+    
+    // now modify tempThingy
+    // if we want to go back in time
+    if (tempLen > x)
+    {
+      tempThingy.reduceLen();
+    }
+    else // go forwards
     {
-      for (int i = (x + 1); i < thingies.size(); i++)
-      {
-        thingies.remove(i);
-      }
+      tempThingy.increaseLen();
     }
     setChanged();
     notifyObservers();
@@ -125,17 +150,20 @@ class Thingy
   private ArrayList<Point> points;
   private Color colour;
   private int thiccness;
+  private int visibleLen;
   
   public Thingy(Color c, int x)
   {
     points = new ArrayList();
     this.colour = c;
     this.thiccness = x;
+    this.visibleLen = 0;
   }
   
   public void addPoint(int x, int y)
   {
     points.add(new Point(x, y));
+    visibleLen++;
   }
   
   public ArrayList<Point> getPoints()
@@ -152,4 +180,19 @@ class Thingy
   {
     return thiccness;
   }
+  
+  public int getVisisbleLen()
+  {
+    return visibleLen;
+  }
+  
+  public void reduceLen()
+  {
+    visibleLen--;
+  }
+  
+  public void increaseLen()
+  {
+    visibleLen++;
+  }
 }

+ 2 - 2
assignments/a2/src/main/java/View.java

@@ -39,13 +39,13 @@ public class View extends JPanel implements Observer
       gtemp.setColor(t.getColour());
       gtemp.setStroke(new BasicStroke(t.getThiccness(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
       
-      if ((points.size() ==  1))
+      if (t.getVisisbleLen() ==  1))
       {
         gtemp.drawLine(points.get(0).x, points.get(0).y, points.get(0).x, points.get(0).y);
       }
       else
       {
-        for (int i = 1; i < points.size(); i++)
+        for (int i = 1; i < t.getVisisbleLen(); i++)
         {
           gtemp.drawLine(points.get(i - 1).x, points.get(i - 1).y, points.get(i).x, points.get(i).y);
         }