Render resistor color bands properly when zommed in

This commit is contained in:
Branislav Stojkovic
2019-01-09 15:52:30 +01:00
parent c7a04075f4
commit af2195798f
8 changed files with 75 additions and 3 deletions
@@ -43,6 +43,7 @@ public class ObjectCache {
private ObjectCache() {} private ObjectCache() {}
private Map<Float, Stroke> basicStrokeMap = new HashMap<Float, Stroke>(); private Map<Float, Stroke> basicStrokeMap = new HashMap<Float, Stroke>();
private Map<Float, Stroke> zoomableStrokeMap = new HashMap<Float, Stroke>();
private Map<String, Stroke> dashStrokeMap = new HashMap<String, Stroke>(); private Map<String, Stroke> dashStrokeMap = new HashMap<String, Stroke>();
public Stroke fetchBasicStroke(float width) { public Stroke fetchBasicStroke(float width) {
@@ -53,6 +54,15 @@ public class ObjectCache {
basicStrokeMap.put(width, stroke); basicStrokeMap.put(width, stroke);
return stroke; return stroke;
} }
public Stroke fetchZoomableStroke(float width) {
if (zoomableStrokeMap.containsKey(width)) {
return zoomableStrokeMap.get(width);
}
Stroke stroke = new ZoomableStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
zoomableStrokeMap.put(width, stroke);
return stroke;
}
public Stroke fetchStroke(float width, float[] dash, float phase) { public Stroke fetchStroke(float width, float[] dash, float phase) {
String key = width + "|" + Arrays.toString(dash) + "|" + phase; String key = width + "|" + Arrays.toString(dash) + "|" + phase;
@@ -0,0 +1,57 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.common;
import java.awt.BasicStroke;
/***
* {@link BasicStroke} that will always be zoomed in when rendered.
*
* @author Branislav Stojkovic
*/
public class ZoomableStroke extends BasicStroke {
public ZoomableStroke() {
// TODO Auto-generated constructor stub
}
public ZoomableStroke(float width) {
super(width);
// TODO Auto-generated constructor stub
}
public ZoomableStroke(float width, int cap, int join) {
super(width, cap, join);
// TODO Auto-generated constructor stub
}
public ZoomableStroke(float width, int cap, int join, float miterlimit) {
super(width, cap, join, miterlimit);
// TODO Auto-generated constructor stub
}
public ZoomableStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) {
super(width, cap, join, miterlimit, dash, dash_phase);
// TODO Auto-generated constructor stub
}
}
@@ -55,6 +55,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.diylc.common.ObjectCache; import org.diylc.common.ObjectCache;
import org.diylc.common.ZoomableStroke;
import org.diylc.core.IDrawingObserver; import org.diylc.core.IDrawingObserver;
/** /**
@@ -406,7 +407,7 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
if (this.zoom > 1 && s instanceof BasicStroke) { if (this.zoom > 1 && s instanceof BasicStroke) {
BasicStroke bs = (BasicStroke) s; BasicStroke bs = (BasicStroke) s;
// make thin lines even thinner to compensate for zoom factor // make thin lines even thinner to compensate for zoom factor
if (bs.getLineWidth() <= 2) if (bs.getLineWidth() <= 2 && !(s instanceof ZoomableStroke))
s = ObjectCache.getInstance().fetchStroke((float) (bs.getLineWidth() / zoom), bs.getDashArray(), bs.getDashPhase()); s = ObjectCache.getInstance().fetchStroke((float) (bs.getLineWidth() / zoom), bs.getDashArray(), bs.getDashPhase());
} }
canvasGraphics.setStroke(s); canvasGraphics.setStroke(s);
Binary file not shown.
@@ -179,7 +179,7 @@ public class Resistor extends AbstractLeadedComponent<Resistance> {
if (getShape() == ResistorShape.Standard) { if (getShape() == ResistorShape.Standard) {
int x = width + FIRST_BAND; int x = width + FIRST_BAND;
Color[] bands = value.getColorCode(colorCode); Color[] bands = value.getColorCode(colorCode);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(2)); g2d.setStroke(ObjectCache.getInstance().fetchZoomableStroke(2));
for (int i = 0; i < bands.length; i++) { for (int i = 0; i < bands.length; i++) {
g2d.setColor(bands[i]); g2d.setColor(bands[i]);
g2d.drawLine(x, width / 10 + 2, x, width * 8 / 10 + 1); g2d.drawLine(x, width / 10 + 2, x, width * 8 / 10 + 1);
@@ -189,7 +189,7 @@ public class Resistor extends AbstractLeadedComponent<Resistance> {
int x = -FIRST_BAND; int x = -FIRST_BAND;
int height = getClosestOdd(getWidth().convertToPixels()); int height = getClosestOdd(getWidth().convertToPixels());
Color[] bands = value.getColorCode(colorCode); Color[] bands = value.getColorCode(colorCode);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(2)); g2d.setStroke(ObjectCache.getInstance().fetchZoomableStroke(2));
for (int i = 0; i < bands.length; i++) { for (int i = 0; i < bands.length; i++) {
g2d.setColor(bands[i]); g2d.setColor(bands[i]);
g2d.drawLine(x, 1, x, height - 1); g2d.drawLine(x, 1, x, height - 1);
Binary file not shown.
Binary file not shown.
+4
View File
@@ -2146,6 +2146,10 @@
<changeType>NEW_FEATURE</changeType> <changeType>NEW_FEATURE</changeType>
<description>"P-Bass Pickup" added</description> <description>"P-Bass Pickup" added</description>
</org.diylc.appframework.update.Change> </org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Render resistor color bands properly when zommed in</description>
</org.diylc.appframework.update.Change>
</changes> </changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url> <url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version> </org.diylc.appframework.update.Version>