added resistor color codes

fixed double value parsing
This commit is contained in:
bancika
2011-01-27 22:34:41 +00:00
parent b3351ef081
commit 11b7756a59
6 changed files with 86 additions and 0 deletions
@@ -0,0 +1,11 @@
package org.diylc.common;
public enum ResistorColorCode {
_4_BAND, _5_BAND;
@Override
public String toString() {
return name().substring(1).replace("_", " ").toLowerCase();
}
}
@@ -1,9 +1,20 @@
package org.diylc.core.measures; package org.diylc.core.measures;
import java.awt.Color;
import org.diylc.common.ResistorColorCode;
public class Resistance extends AbstractMeasure<ResistanceUnit> { public class Resistance extends AbstractMeasure<ResistanceUnit> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Color[] COLOR_DIGITS = new Color[] { Color.black, Color.decode("#8B4513"),
Color.red, Color.orange, Color.yellow, Color.decode("#76EE00"), Color.blue,
Color.decode("#91219E"), Color.lightGray, Color.white };
private static final Color[] COLOR_MULTIPLIER = new Color[] { Color.lightGray.brighter(),
Color.decode("#FFB90F"), Color.black, Color.decode("#8B4513"), Color.red, Color.orange,
Color.yellow, Color.decode("#76EE00"), Color.blue };
public Resistance() { public Resistance() {
super(); super();
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
@@ -19,6 +30,44 @@ public class Resistance extends AbstractMeasure<ResistanceUnit> {
return new Resistance(value, unit); return new Resistance(value, unit);
} }
public Color[] getColorCode(ResistorColorCode resistorColorCode) {
Color[] bands;
double base = getValue() * getUnit().getFactor();
int multiplier = 0;
while (base > (resistorColorCode == ResistorColorCode._4_BAND ? 99 : 999)) {
multiplier += 1;
base /= 10;
}
while (base < (resistorColorCode == ResistorColorCode._4_BAND ? 10 : 100)) {
multiplier -= 1;
base *= 10;
}
System.out.println("base: " + base + " factor: " + multiplier);
if (multiplier > 6 || multiplier < -2) {
// Out of range.
return new Color[] {};
}
switch (resistorColorCode) {
case _4_BAND:
bands = new Color[3];
bands[0] = COLOR_DIGITS[(int) Math.round(base / 10)];
bands[1] = COLOR_DIGITS[(int) Math.round(base % 10)];
bands[2] = COLOR_MULTIPLIER[multiplier + 2];
break;
case _5_BAND:
bands = new Color[4];
bands[0] = COLOR_DIGITS[(int) Math.round(base / 100)];
bands[1] = COLOR_DIGITS[(int) Math.round(base / 10 % 10)];
bands[2] = COLOR_DIGITS[(int) Math.round(base % 10)];
bands[3] = COLOR_MULTIPLIER[multiplier + 2];
break;
default:
bands = new Color[] {};
}
return bands;
}
public static Resistance parseResistance(String value) { public static Resistance parseResistance(String value) {
for (ResistanceUnit unit : ResistanceUnit.values()) { for (ResistanceUnit unit : ResistanceUnit.values()) {
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) { if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
@@ -5,6 +5,8 @@ import java.awt.Graphics2D;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import org.diylc.common.ObjectCache;
import org.diylc.common.ResistorColorCode;
import org.diylc.components.AbstractLeadedComponent; import org.diylc.components.AbstractLeadedComponent;
import org.diylc.core.CreationMethod; import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
@@ -26,6 +28,7 @@ public class Resistor extends AbstractLeadedComponent<Resistance> {
public static Color BORDER_COLOR = BODY_COLOR.darker(); public static Color BORDER_COLOR = BODY_COLOR.darker();
private Resistance value = new Resistance(123d, ResistanceUnit.K); private Resistance value = new Resistance(123d, ResistanceUnit.K);
private ResistorColorCode colorCode = ResistorColorCode._5_BAND;
public Resistor() { public Resistor() {
super(); super();
@@ -70,9 +73,32 @@ public class Resistor extends AbstractLeadedComponent<Resistance> {
return BORDER_COLOR; return BORDER_COLOR;
} }
@EditableProperty(name = "Color code")
public ResistorColorCode getColorCode() {
return colorCode;
}
public void setColorCode(ResistorColorCode colorCode) {
this.colorCode = colorCode;
}
@Override @Override
protected Shape getBodyShape() { protected Shape getBodyShape() {
return new Rectangle2D.Double(0f, 0f, getWidth().convertToPixels(), return new Rectangle2D.Double(0f, 0f, getWidth().convertToPixels(),
getClosestOdd(getHeight().convertToPixels())); getClosestOdd(getHeight().convertToPixels()));
} }
@Override
protected void decorateComponentBody(Graphics2D g2d) {
// int width = getWidth().convertToPixels();
int height = getClosestOdd(getHeight().convertToPixels());
Color[] bands = value.getColorCode(colorCode);
int x = 4;
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(2));
for (int i = 0; i < bands.length; i++) {
g2d.setColor(bands[i]);
g2d.drawLine(x, 1, x, height - 1);
x += 5;
}
}
} }