Bladeren bron

supplyBars ColorRamp

tolatesry 6 jaren geleden
bovenliggende
commit
60da65798e

+ 17 - 17
src/classes/HolonBattery.java

@@ -4,7 +4,7 @@ public class HolonBattery extends AbstractCpsObject{
 	
 	private float inRatio;
 	private float outRatio;
-	private float capasity;
+	private float capacity;
 	private float stateOfCharge;
 	private float newStateOfCharge;
 	public enum State{STANDBY, COLLECT, EMIT};
@@ -17,7 +17,7 @@ public class HolonBattery extends AbstractCpsObject{
 		super(ObjName);
 		inRatio = 0;
 		outRatio = 0;
-		capasity = 0;
+		capacity = 0;
 		stateOfCharge = 0;
 		newStateOfCharge = 0;
 		setState(State.STANDBY);
@@ -31,7 +31,7 @@ public class HolonBattery extends AbstractCpsObject{
 		super.setName(obj.getName());
 		setInRatio(((HolonBattery) obj).getInRatio());
 		setOutRatio(((HolonBattery) obj).getOutRatio());
-		setCapasity(((HolonBattery) obj).getCapasity());
+		setCapacity(((HolonBattery) obj).getCapacity());
 		setStateOfCharge(((HolonBattery) obj).getStateOfCharge());
 		setNewStateOfCharge(((HolonBattery) obj).getStateOfCharge());
 		setState(State.STANDBY);
@@ -40,9 +40,9 @@ public class HolonBattery extends AbstractCpsObject{
 		return stateOfCharge;
 	}
 	public void setStateOfCharge(float stateOfCharge) {
-		if(stateOfCharge > capasity) //state of Charege can not more than the capacity
+		if(stateOfCharge > capacity) //state of Charege can not more than the capacity
 		{
-			stateOfCharge = capasity;
+			stateOfCharge = capacity;
 		}
 		else if(stateOfCharge < 0) // state of charge can not be a negativ value
 		{
@@ -50,16 +50,16 @@ public class HolonBattery extends AbstractCpsObject{
 		}
 		this.stateOfCharge = stateOfCharge;
 	}
-	public float getCapasity() {
+	public float getCapacity() {
 	
-		return capasity;
+		return capacity;
 	}
-	public void setCapasity(float capasity) {	
-		if(capasity < 0) //capasity can not be negative
+	public void setCapacity(float capacity) {	
+		if(capacity < 0) //capasity can not be negative
 		{
-			capasity = 0;
+			capacity = 0;
 		}
-		this.capasity = capasity;
+		this.capacity = capacity;
 	}
 	public float getOutRatio() {
 		return outRatio;
@@ -77,8 +77,8 @@ public class HolonBattery extends AbstractCpsObject{
 	//For Calculations
 	public float getIN()
 	{
-		if(getCapasity() - getStateOfCharge() < inRatio)
-			return getCapasity() - getStateOfCharge();
+		if(getCapacity() - getStateOfCharge() < inRatio)
+			return getCapacity() - getStateOfCharge();
 		else
 			return inRatio;
 	}
@@ -109,7 +109,7 @@ public class HolonBattery extends AbstractCpsObject{
 	 */
 	public String getCanvasBatteryString()
 	{
-		
+		/*
 		switch(getState())
 		{
 		case COLLECT:
@@ -120,8 +120,8 @@ public class HolonBattery extends AbstractCpsObject{
 		default:
 			return "0";
 		}
+		*/
 		
-		/*
 		if(newStateOfCharge > stateOfCharge)
 		{
 			return "+" + Float.toString(newStateOfCharge - stateOfCharge);
@@ -133,13 +133,13 @@ public class HolonBattery extends AbstractCpsObject{
 		{
 			return "0";
 		}
-		*/
+		
 	}
 	public String toString()
 	{
 		return "HolonBattery ID:" + this.getId() + " State:" + getState().name() 
 				+ " InRatio:" + getInRatio() + " OutRatio:" + getOutRatio() 
-				+ " Akku: " + getStateOfCharge() + "/" + getCapasity()
+				+ " Akku: " + getStateOfCharge() + "/" + getCapacity()
 				+ " NewSOC: " + getNewStateOfCharge();
 	}
 	public float getNewStateOfCharge() {

+ 2 - 2
src/classes/comparator/WeakestBattery.java

@@ -10,8 +10,8 @@ public class WeakestBattery implements Comparator<HolonBattery>{
 	@Override
 	public int compare(HolonBattery o1, HolonBattery o2) {
 		//Sort Battery by the value of StateOfCharge/Capasity
-		float O1capasity = o1.getCapasity();		
-		float O2capasity = o2.getCapasity();
+		float O1capasity = o1.getCapacity();		
+		float O2capasity = o2.getCapacity();
 		if(O1capasity == 0)
 		{
 			return 1; 

+ 1 - 1
src/ui/controller/UpdateController.java

@@ -339,7 +339,7 @@ public class UpdateController {
 				deleteRows(model.getMultiTable());
 				Object[] numInRatio = {"InRatio:", ((HolonBattery)obj).getInRatio() };
 				Object[] numOutRatio = {"OutRatio:", ((HolonBattery)obj).getOutRatio() };
-				Object[] numSOC_Capasity = {"State of charge:", Float.toString(((HolonBattery)obj).getStateOfCharge()) + "/" + Float.toString(((HolonBattery)obj).getCapasity()) };
+				Object[] numSOC_Capasity = {"State of charge:", Float.toString(((HolonBattery)obj).getStateOfCharge()) + "/" + Float.toString(((HolonBattery)obj).getCapacity()) };
 				model.getPropertyTable().addRow(numInRatio);
 				model.getPropertyTable().addRow(numOutRatio);
 				model.getPropertyTable().addRow(numSOC_Capasity);

+ 95 - 26
src/ui/view/AbstractCanvas.java

@@ -168,27 +168,85 @@ public abstract class AbstractCanvas extends JPanel {
 		/**
 		 * draw and fill the supply Bar
 		 */
-		if (model.getShowSupplyBars() && cps instanceof HolonObject) {
-			HolonObject hl = (HolonObject) cps;
-			if (hl != null && (hl.getState() == HolonObject.NOT_SUPPLIED
-					|| hl.getState() == HolonObject.PARTIALLY_SUPPLIED || hl.getState() == HolonObject.OVER_SUPPLIED
-			/* || hl.getState() == HolonObject.SUPPLIED */)) {
-				// calculate Positons:
-				int barX = (int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20);
-				int barY = (int) (cps.getPosition().y - controller.getScaleDiv2() + controller.getScale() + 1);
-				int barWidth = (int) (controller.getScale() + ((scalediv20) * 2) - 1);
-				int barHeight = (int) (controller.getScale() / 5);
-
-				// draw Rectangle under the image
-				g2.setStroke(new BasicStroke(1));
-				g2.drawRect(barX, barY, barWidth, barHeight);
-
+		if (model.getShowSupplyBars() && (cps instanceof HolonObject || cps instanceof HolonBattery))
+		{
+			
+			
+		
+			// set Color & Percentage	
+			float percentage = 0;
+			Color paintColor = Color.WHITE;
+			if(cps instanceof HolonObject)
+			{
+				HolonObject hl = (HolonObject) cps;
+				if (hl == null || !(hl.getState() == HolonObject.NOT_SUPPLIED
+						|| hl.getState() == HolonObject.PARTIALLY_SUPPLIED || hl.getState() == HolonObject.OVER_SUPPLIED
+				/* || hl.getState() == HolonObject.SUPPLIED */)) 
+				{
+					return;
+				}
 				// get supplied status
-				float percentage = hl.getSuppliedPercentage();
-
-				// set Color
-				g2.setColor(hl.getColor());
-
+				percentage = hl.getSuppliedPercentage();
+				paintColor = hl.getColor();
+			}
+			else if (cps instanceof HolonBattery)
+			{
+				HolonBattery hB = (HolonBattery) cps;
+				if(hB == null || hB.getCapacity() == 0)
+				{
+					return;
+				}
+				// get supplied status
+				percentage = hB.getNewStateOfCharge() / hB.getCapacity();
+				//Color lerping
+//				float lerp(float point1, float point2, float alpha)
+//				{
+//				    return point1 + alpha * (point2 - point1);
+//				}
+				Color color1 = Color.RED;
+				Color color2 = Color.GREEN;
+				//
+				float colorPercentage;
+				if(percentage < 0.5f)
+				{
+					colorPercentage = percentage * 2;
+					color1 = Color.RED;
+					color2 = Color.YELLOW;
+				}
+				else
+				{
+					colorPercentage = (percentage - 0.5f) * 2;
+					color1 = Color.YELLOW;
+					color2 = Color.GREEN;
+				}
+				final int dRed = color2.getRed() - color1.getRed();
+				final int dGreen = color2.getGreen() - color1.getGreen();
+				final int dBlue = color2.getBlue() - color1.getBlue();	
+				
+				int resultRed = color1.getRed() +  (int)(colorPercentage * dRed);
+				int resultGreen = color1.getGreen() +  (int)(colorPercentage * dGreen);
+				int resultBlue = color1.getBlue() + (int)( colorPercentage * dBlue);
+				//System.out.println("[r="+ resultRed + ",g="+ resultGreen + ",b=" + resultBlue+"]");
+				paintColor = new Color(	resultRed,resultGreen,resultBlue);
+			}
+			
+			// calculate Positons:
+			int barX = (int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20);
+			int barY = (int) (cps.getPosition().y - controller.getScaleDiv2() + controller.getScale() + 1);
+			int barWidth = (int) (controller.getScale() + ((scalediv20) * 2) - 1);
+			int barHeight = (int) (controller.getScale() / 5);
+
+			// draw Rectangle under the image
+			g2.setStroke(new BasicStroke(1));
+			g2.drawRect(barX, barY, barWidth, barHeight);
+				
+				
+	
+			g2.setColor(paintColor);
+				
+	
+				
+	
 				// fill it accordingly if filled partially
 				if (percentage < 1)
 					g2.fillRect(barX + 1, barY + 1, (int) ((barWidth - 1) * percentage), barHeight - 1);
@@ -203,15 +261,15 @@ public abstract class AbstractCanvas extends JPanel {
 				
 				Font oldFont = g2.getFont();
 				g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
-
+	
 				String percentageString = (Math.round((percentage * 100))) + "%";
-
+	
 				int stringWidth = (int) g2.getFontMetrics().getStringBounds(percentageString, g2).getWidth();
 				g2.drawString(percentageString, barX + barWidth / 2 + 1 - stringWidth / 2, barY + barHeight);
-
+	
 				g2.setFont(oldFont);
 				g2.setColor(Color.BLACK);
-			}
+				
 		}
 	}
 
@@ -241,7 +299,12 @@ public abstract class AbstractCanvas extends JPanel {
 					float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
 					g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
 							cps.getPosition().y - controller.getScaleDiv2() - 10);
-				}
+				}else if (showedInformation[1] && cps instanceof HolonBattery)
+                {
+                	g2.setColor(Color.BLACK);
+                    g2.drawString(((HolonBattery) cps).getCanvasBatteryString(), cps.getPosition().x - controller.getScaleDiv2(),
+                            cps.getPosition().y - controller.getScaleDiv2() - 10);
+                }
 			} else if (cps instanceof HolonObject) {
 				g2.setColor(((HolonObject) cps).getColor());
 
@@ -256,7 +319,13 @@ public abstract class AbstractCanvas extends JPanel {
 					g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
 							cps.getPosition().y - controller.getScaleDiv2() - 10);
 				}
-			}
+			}else if (cps instanceof HolonBattery) {
+                if (showedInformation[1]) {
+                	g2.setColor(Color.BLACK);
+                    g2.drawString(((HolonBattery) cps).getCanvasBatteryString(), cps.getPosition().x - controller.getScaleDiv2(),
+                            cps.getPosition().y - controller.getScaleDiv2() - 10);
+                }
+            }
 			// draw image
 			File checkPath = new File(cps.getImage());
 			if (checkPath.exists()) {

+ 3 - 3
src/ui/view/AddObjectPopUp.java

@@ -307,9 +307,9 @@ public class AddObjectPopUp extends JDialog {
         JLabel batteryOutRateLabel = new JLabel("Out ratio:");
         JTextField batteryOutRateBox = new JTextField(10);
         batteryOutRateBox.setText(Float.toString(editBat.getOutRatio()));
-        JLabel batteryCapasityLabel = new JLabel("Capasity:");
+        JLabel batteryCapasityLabel = new JLabel("Capacity:");
         JTextField batteryCapasityBox = new JTextField(10);
-        batteryCapasityBox.setText(Float.toString(editBat.getCapasity()));
+        batteryCapasityBox.setText(Float.toString(editBat.getCapacity()));
         JLabel batterySOCLabel = new JLabel("State of charge:"); 
         JTextField batterySOCBox = new JTextField(10);
         batterySOCBox.setText(Float.toString(editBat.getStateOfCharge()));
@@ -365,7 +365,7 @@ public class AddObjectPopUp extends JDialog {
         			//Saving:
         			editBat.setInRatio(changedInRatio);
         			editBat.setOutRatio(changedOutRatio);
-        			editBat.setCapasity(changedCapasity);
+        			editBat.setCapacity(changedCapasity);
         			editBat.setStateOfCharge(changedSOC);
 					try {
 						controller.saveCategory();

+ 1 - 1
src/ui/view/MyCanvas.java

@@ -493,7 +493,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 					controller.getScale(), controller.getScale(), null);
 
 			paintSupplyBar(g, cps);
-
+			
 		}
 
 		// Dragged marker Highlighting