diff --git a/diylc/diylc-library/src/org/diylc/components/AbstractLeadedComponent.java b/diylc/diylc-library/src/org/diylc/components/AbstractLeadedComponent.java index e36e3699..d2f61b01 100644 --- a/diylc/diylc-library/src/org/diylc/components/AbstractLeadedComponent.java +++ b/diylc/diylc-library/src/org/diylc/components/AbstractLeadedComponent.java @@ -260,7 +260,7 @@ public abstract class AbstractLeadedComponent extends AbstractTransparentComp } Rectangle2D textRect = fontMetrics.getStringBounds(label, g2d); // Don't offset in outline mode. - int offset = outlineMode ? 0 : getLabelOffset((int) length, (int) textRect.getWidth()); + int offset = outlineMode ? 0 : getLabelOffset((int) length, (int) width, (int) textRect.getWidth()); // Adjust label angle if needed to make sure that it's readable. if ((theta >= Math.PI / 2 && theta <= Math.PI) || (theta < -Math.PI / 2 && theta > -Math.PI)) { g2d.rotate(Math.PI, length / 2, width / 2); @@ -348,7 +348,7 @@ public abstract class AbstractLeadedComponent extends AbstractTransparentComp return false; } - protected int getLabelOffset(int bodyWidth, int labelWidth) { + protected int getLabelOffset(int bodyLength, int bodyWidth, int labelLength) { return 0; } diff --git a/diylc/diylc-library/src/org/diylc/components/passive/Resistor.java b/diylc/diylc-library/src/org/diylc/components/passive/Resistor.java index 8a1261c0..16a9e473 100644 --- a/diylc/diylc-library/src/org/diylc/components/passive/Resistor.java +++ b/diylc/diylc-library/src/org/diylc/components/passive/Resistor.java @@ -24,10 +24,12 @@ package org.diylc.components.passive; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Shape; +import java.awt.Stroke; import java.awt.geom.Area; import java.awt.geom.Ellipse2D; +import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; - + import org.diylc.common.ObjectCache; import org.diylc.common.ResistorColorCode; import org.diylc.common.SimpleComponentTransformer; @@ -171,43 +173,41 @@ public class Resistor extends AbstractLeadedComponent { @Override protected void decorateComponentBody(Graphics2D g2d, boolean outlineMode) { - // int width = getWidth().convertToPixels(); if (colorCode == ResistorColorCode.NONE || outlineMode || value == null) { return; - } + } + Area body = new Area(getBodyShape()); + Stroke stroke = ObjectCache.getInstance().fetchZoomableStroke(2); int width = getClosestOdd(getWidth().convertToPixels()); - if (getShape() == ResistorShape.Standard) { - int x = width + FIRST_BAND; - Color[] bands = value.getColorCode(colorCode); - g2d.setStroke(ObjectCache.getInstance().fetchZoomableStroke(2)); - for (int i = 0; i < bands.length; i++) { - g2d.setColor(bands[i]); - g2d.drawLine(x, width / 10 + 2, x, width * 8 / 10 + 1); - x += BAND_SPACING; - } - } else { - int x = -FIRST_BAND; - int height = getClosestOdd(getWidth().convertToPixels()); - Color[] bands = value.getColorCode(colorCode); - g2d.setStroke(ObjectCache.getInstance().fetchZoomableStroke(2)); - for (int i = 0; i < bands.length; i++) { - g2d.setColor(bands[i]); - g2d.drawLine(x, 1, x, height - 1); - x += BAND_SPACING; - } - } + int x = getShape() == ResistorShape.Standard ? width + FIRST_BAND : -FIRST_BAND; + Color[] bands = value.getColorCode(colorCode); + for (int i = 0; i < bands.length; i++) { + g2d.setColor(bands[i]); + Area line = new Area(stroke.createStrokedShape(new Line2D.Double(x, 0, x, width))); + line.intersect(body); + g2d.fill(line); + x += BAND_SPACING; + } } @Override - protected int getLabelOffset(int bodyWidth, int labelWidth) { - if (value == null || getColorCode() == ResistorColorCode.NONE || getLabelOriantation() != AbstractLeadedComponent.LabelOriantation.Directional) - return 0; - int width = getClosestOdd(getWidth().convertToPixels()); - Color[] bands = value.getColorCode(colorCode); - int bandArea = - getShape() == ResistorShape.Standard ? width + FIRST_BAND + BAND_SPACING * (bands.length - 1) : -FIRST_BAND - + BAND_SPACING * (bands.length - 1); - + protected int getLabelOffset(int bodyLength, int bodyWidth, int labelLength) { + if (value == null || getColorCode() == ResistorColorCode.NONE || getLabelOriantation() != AbstractLeadedComponent.LabelOriantation.Directional) + return 0; + Color[] bands = value.getColorCode(colorCode); + + int bandLenght = FIRST_BAND + BAND_SPACING * (bands.length - 1); + + if (getShape() == ResistorShape.Standard) { + if (labelLength < bodyLength - 2 * bodyWidth - bandLenght) + return bandLenght; + } else { + if (labelLength < bodyLength - bandLenght) + return bandLenght; + } + + int bandArea = getShape() == ResistorShape.Standard ? bodyWidth + bandLenght : -bandLenght; + return bandArea / 2; } diff --git a/diylc/diylc-swing/library/main.jar b/diylc/diylc-swing/library/main.jar index 4b662540..9484ecd7 100644 Binary files a/diylc/diylc-swing/library/main.jar and b/diylc/diylc-swing/library/main.jar differ diff --git a/diylc/diylc-swing/update.xml b/diylc/diylc-swing/update.xml index dda26e6b..5605237c 100644 --- a/diylc/diylc-swing/update.xml +++ b/diylc/diylc-swing/update.xml @@ -2203,7 +2203,7 @@ 49 0 - 2018-12-26 00:00:00.000 CET + 2019-01-16 00:00:00.000 CET @@ -2236,7 +2236,7 @@ IMPROVEMENT - Use hardware acceleration for rendering to improve performance + Use hardware acceleration for rendering to improve performance and reduce memory usage IMPROVEMENT @@ -2248,6 +2248,22 @@ https://github.com/bancika/diy-layout-creator/releases + + + + 3 + 50 + 0 + + 2019-01-16 00:00:00.000 CET + + + + IMPROVEMENT + Better color band rendering on resistors and smarter label positioning + + + https://github.com/bancika/diy-layout-creator/releases