improved potentiometer symbol

This commit is contained in:
bancika
2016-04-25 15:00:25 +02:00
parent 6f19985c73
commit 146634f75d
10 changed files with 105 additions and 22 deletions
@@ -1,18 +1,26 @@
package org.diylc.components.passive;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.components.semiconductors.AbstractTransistorSymbol;
@ComponentDescriptor(name = "Trimmer Symbol", author = "MCbx", category = "Schematics", instanceNamePrefix = " VR", description = "Trimmer (potentiometer) symbol", stretchable = false, zOrder = IDIYComponent.COMPONENT, rotatable = true)
@ComponentDescriptor(name = "Potentiometer Symbol", author = "MCbx", category = "Schematics", instanceNamePrefix = " VR", description = "Potentiometer symbol", stretchable = false, zOrder = IDIYComponent.COMPONENT, rotatable = true)
public class PotentiometerSymbol extends AbstractTransistorSymbol {
private static final long serialVersionUID = 1L;
public PotentiometerSymbol() {
this.color = Color.blue;
}
public Shape[] getBody() {
if (body == null) {
body = new Shape[3];
@@ -41,30 +49,65 @@ public class PotentiometerSymbol extends AbstractTransistorSymbol {
polyline.lineTo(x + pinSpacing * 2, (y - pinSpacing * 2) + 7 * pinSpacing / 2);
polyline.lineTo(x + pinSpacing * 2, y + pinSpacing * 2);
polyline.moveTo(x, y);
polyline.lineTo(x + pinSpacing * 2 - pinSpacing / 4, y);
polyline.lineTo(x + pinSpacing * 2 - 2 * pinSpacing / 3, y - pinSpacing / 2);
polyline.moveTo(x + pinSpacing * 2 - pinSpacing / 4, y);
polyline.lineTo(x + pinSpacing * 2 - 2 * pinSpacing / 3, y + pinSpacing / 2);
body[0] = polyline;
polyline = new GeneralPath();
polyline.lineTo(x + pinSpacing * 2 - pinSpacing / 2, y);
body[1] = polyline;
polyline = new GeneralPath();
body[0] = polyline;
polyline = new GeneralPath();
polyline.moveTo(x + pinSpacing * 2 - pinSpacing / 3, y);
polyline.lineTo(x + pinSpacing * 2 - pinSpacing * 2 / 3, y - pinSpacing / 3);
polyline.lineTo(x + pinSpacing * 2 - pinSpacing * 2 / 3, y + pinSpacing / 3);
polyline.lineTo(x + pinSpacing * 2 - pinSpacing / 3, y);
body[2] = polyline;
}
return body;
}
@Override
protected int getLabelX(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) {
int pinSpacing = (int) PIN_SPACING.convertToPixels();
if (controlPoints[0].x < controlPoints[1].x && controlPoints[0].x < controlPoints[2].x)
return controlPoints[1].x + pinSpacing / 3;
if (controlPoints[0].y < controlPoints[1].y && controlPoints[0].y < controlPoints[2].y)
return controlPoints[0].x - (int) (textRect.getWidth() / 2);
if (controlPoints[0].x > controlPoints[1].x && controlPoints[0].x > controlPoints[2].x)
return controlPoints[1].x - pinSpacing / 2 - (int)textRect.getWidth();
if (controlPoints[0].y > controlPoints[1].y && controlPoints[0].y > controlPoints[2].y)
return controlPoints[0].x - (int) (textRect.getWidth() / 2);
return 2 * ((int) (shapeRect.getWidth() - textRect.getWidth()) / 2 + (int) shapeRect.getX());
}
@Override
protected int getLabelY(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) {
int pinSpacing = (int) PIN_SPACING.convertToPixels();
if (controlPoints[0].x < controlPoints[1].x && controlPoints[0].x < controlPoints[2].x)
return controlPoints[0].y;
if (controlPoints[0].y < controlPoints[1].y && controlPoints[0].y < controlPoints[2].y)
return controlPoints[1].y + pinSpacing * 2 / 3;
if (controlPoints[0].x > controlPoints[1].x && controlPoints[0].x > controlPoints[2].x)
return controlPoints[0].y;
if (controlPoints[0].y > controlPoints[1].y && controlPoints[0].y > controlPoints[2].y)
return controlPoints[1].y - pinSpacing * 2 / 3;
return 2 * ((int) (shapeRect.getHeight() - textRect.getHeight()) / 2 + fontMetrics.getAscent()
+ (int) shapeRect.getY());
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.rotate(-Math.PI / 4, width / 2, height / 2);
g2d.setColor(COLOR);
g2d.setColor(Color.blue);
g2d.drawLine(0, height / 2, 4, height / 2);
g2d.drawLine(width - 4, height / 2, width, height / 2);
g2d.drawPolyline(new int[] { 4, 6, 10, 14, 18, 22, 26, 28 }, new int[] { height / 2, height / 2 + 2,
height / 2 - 2, height / 2 + 2, height / 2 - 2, height / 2 + 2, height / 2 - 2, height / 2 }, 8);
g2d.drawPolyline(new int[] { width / 2, width / 2 - 2, width / 2 - 4, width / 2 + 2, width / 2 - 2 },
new int[] { height, height / 2 + 2, height / 2 + 6, height / 2 + 6, height / 2 + 2 }, 5);
g2d.drawPolyline(new int[] { width / 2, width / 2 - 2 }, new int[] { height, height / 2 + 2 }, 2);
g2d.fill(new Polygon(new int[] { width / 2 - 4, width / 2 + 2, width / 2 - 2 },
new int[] { height / 2 + 6, height / 2 + 6, height / 2 + 2 }, 3));
}
}
@@ -1,10 +1,13 @@
package org.diylc.components.semiconductors;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.Display;
@@ -35,7 +38,7 @@ public abstract class AbstractTransistorSymbol extends AbstractComponent<String>
protected Point[] controlPoints = new Point[] { new Point(0, 0), new Point(0, 0),
new Point(0, 0) };
protected Color color = COLOR;
protected SymbolFlipping flipping = FLIPPING;
protected SymbolFlipping flip = FLIPPING;
protected Display display = Display.NAME;
transient protected Shape[] body;
@@ -90,7 +93,7 @@ public abstract class AbstractTransistorSymbol extends AbstractComponent<String>
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(rotation),controlPoints[0].x,controlPoints[0].y);
if (this.flipping==SymbolFlipping.Y){
if (this.flip==SymbolFlipping.Y){
g2d.translate(0,controlPoints[0].y);
g2d.scale(1, -1);
g2d.translate(0, -1*controlPoints[0].y);
@@ -129,8 +132,13 @@ public abstract class AbstractTransistorSymbol extends AbstractComponent<String>
if (display==Display.BOTH) {
label=getName()+" "+(getValue() == null ? "" : getValue().toString());
}
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D textRect = fontMetrics.getStringBounds(label, g2d);
Rectangle shapeRect = body[0].getBounds().union(body[1].getBounds()).union(body[2].getBounds());
drawCenteredText(g2d, label,
x + pinSpacing * 2, y, HorizontalAlignment.LEFT, VerticalAlignment.CENTER);
getLabelX(shapeRect, textRect, fontMetrics, outlineMode), getLabelY(shapeRect, textRect, fontMetrics, outlineMode), HorizontalAlignment.LEFT, VerticalAlignment.CENTER);
}
@Override
@@ -142,6 +150,14 @@ public abstract class AbstractTransistorSymbol extends AbstractComponent<String>
public int getControlPointCount() {
return controlPoints.length;
}
protected int getLabelX(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) {
return controlPoints[0].x + (int) PIN_SPACING.convertToPixels() * 2;
}
protected int getLabelY(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) {
return controlPoints[0].y;
}
private void updateControlPoints() {
int pinSpacing = (int) PIN_SPACING.convertToPixels();
@@ -185,12 +201,12 @@ public abstract class AbstractTransistorSymbol extends AbstractComponent<String>
}
@EditableProperty
public SymbolFlipping getFlipping() {
return flipping;
public SymbolFlipping getFlip() {
return flip;
}
public void setFlipping(SymbolFlipping flipping) {
this.flipping = flipping;
public void setFlip(SymbolFlipping flip) {
this.flip = flip;
}
@EditableProperty
@@ -6,6 +6,13 @@ public enum SymbolFlipping {
@Override
public String toString() {
return name();
switch (this) {
case Y:
return "Y-axis";
case NONE:
return "None";
default:
return name();
}
}
}