Browse Source

Improves Comments of AbstractsCanvas:paintSupplyBars

Andreas T. Meyer-Berg 6 years ago
parent
commit
3281326f74
1 changed files with 52 additions and 25 deletions
  1. 52 25
      src/ui/view/AbstractCanvas.java

+ 52 - 25
src/ui/view/AbstractCanvas.java

@@ -89,7 +89,7 @@ public abstract class AbstractCanvas extends JPanel {
 	// ------------------------------------------ METHODS
 	// ------------------------------------------
 	String paintEdge(CpsEdge con, String maxCap) {
-		if (con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
+		if (con!=null && con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
 				&& con != edgeHighlight) {
 			if (con.getConnected() == CpsEdge.CON_UPPER_NODE
 					|| con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
@@ -182,47 +182,54 @@ public abstract class AbstractCanvas extends JPanel {
 		 */
 		if (model.getShowSupplyBars() && (cps instanceof HolonObject || cps instanceof HolonBattery))
 		{
-			// set Color & Percentage	
+			// set Color & Percentage
+			/**
+			 * percentage the SupplyBar is representing
+			 */
 			float percentage = 0;
+			/**
+			 * color of the SupplyBar
+			 */
 			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.PARTIALLY_SUPPLIED || hl.getState() == HolonObject.OVER_SUPPLIED)) {
+					/**
+					 * don't show Bars for unsupplied oder fully supplied Objects
+					 */
 					return;
 				}
-				// get supplied status
+				/**
+				 * calculate Percentage & set Color
+				 */
 				percentage = hl.getSuppliedPercentage();
 				paintColor = hl.getColor();
 			}
-			else if (cps instanceof HolonBattery)
-			{
+			else if (cps instanceof HolonBattery){
 				HolonBattery hB = (HolonBattery) cps;
-				if(hB == null || hB.getCapacity() == 0)
-				{
+				if(hB == null || hB.getCapacity() == 0){
 					return;
 				}
-				// get supplied status
+				/**
+				 *  get percentage filled
+				 */
 				percentage = hB.getStateOfChargeAtTimeStep(model.getCurIteration()-1) / hB.getCapacity();
-				//Color lerping
-//				float lerp(float point1, float point2, float alpha)
-//				{
-//				    return point1 + alpha * (point2 - point1);
-//				}
+
+				/**
+				 * calculate the Color (Red->Yellow->Green)
+				 */
 				Color color1 = Color.RED;
 				Color color2 = Color.GREEN;
 				//
 				float colorPercentage;
-				if(percentage < 0.5f)
-				{
+				if(percentage < 0.5f){
 					colorPercentage = percentage * 2;
 					color1 = Color.RED;
 					color2 = Color.YELLOW;
 				}
-				else
-				{
+				else {
 					colorPercentage = (percentage - 0.5f) * 2;
 					color1 = Color.YELLOW;
 					color2 = Color.GREEN;
@@ -234,45 +241,65 @@ public abstract class AbstractCanvas extends JPanel {
 				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:
+			/**
+			 * Starting position x of the bar
+			 */
 			int barX = (int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20);
+			
+			/**
+			 * Starting position y of the bar
+			 */
 			int barY = (int) (cps.getPosition().y - controller.getScaleDiv2() + controller.getScale() + 1);
+			
 			/**
 			 * if object should be replaced -> move bar below the ReplacementIndicator
 			 */
 			if(mayBeReplaced==cps) barY += 6;
+			
+			/**
+			 * Width of the bar
+			 */
 			int barWidth = (int) (controller.getScale() + ((scalediv20) * 2) - 1);
+			
+			/**
+			 * Height of the bar 
+			 */
 			int barHeight = (int) (controller.getScale() / 5);
 
-			// draw Rectangle under the image
+			/** 
+			 * draw Rectangle below the image 
+			 */
 			g2.setStroke(new BasicStroke(1));
 			g2.drawRect(barX, barY, barWidth, barHeight);
 				
 			g2.setColor(paintColor);
 
-			// fill it accordingly if filled partially
+			/** fill it accordingly if filled partially */
 			if (percentage < 1)
 				g2.fillRect(barX + 1, barY + 1, (int) ((barWidth - 1) * percentage), barHeight - 1);
-			else //over supplied / supplied bar
+			else /** over supplied / supplied bar should be fully filled */
 				g2.fillRect(barX + 1, barY + 1, (int) (barWidth - 1), barHeight - 1);
 			
-			// write percentage
+			/** write percentage */
 			if(percentage>1)
 				g2.setColor(Color.WHITE);
 			else
 				g2.setColor(Color.BLACK);
 			
+			/** original font */
 			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);
 	
+			/** recover Font and Color */
 			g2.setFont(oldFont);
 			g2.setColor(Color.BLACK);