added object cache

This commit is contained in:
bancika
2011-01-21 18:57:05 +00:00
parent d67a0e7c87
commit c51357f58c
15 changed files with 80 additions and 35 deletions
@@ -0,0 +1,32 @@
package org.diylc.common;
import java.awt.BasicStroke;
import java.awt.Stroke;
import java.util.HashMap;
import java.util.Map;
public class ObjectCache {
private static ObjectCache instance;
public static ObjectCache getInstance() {
if (instance == null) {
instance = new ObjectCache();
}
return instance;
}
private ObjectCache() {
}
private Map<Integer, Stroke> basicStrokeMap = new HashMap<Integer, Stroke>();
public Stroke fetchBasicStroke(int width) {
if (basicStrokeMap.containsKey(width)) {
return basicStrokeMap.get(width);
}
Stroke stroke = new BasicStroke(width);
basicStrokeMap.put(width, stroke);
return stroke;
}
}
@@ -8,6 +8,7 @@ import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.CubicCurve2D;
import org.diylc.common.ObjectCache;
import org.diylc.core.ComponentState;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
@@ -50,12 +51,12 @@ public abstract class AbstractCurvedComponent<T> extends AbstractTransparentComp
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.setColor(getDefaultColor().darker());
g2d.setStroke(new BasicStroke(3));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(3));
CubicCurve2D curve = new CubicCurve2D.Double(1, height - 1, width / 4, height / 3,
3 * width / 4, 2 * height / 3, width - 1, 1);
g2d.draw(curve);
g2d.setColor(getDefaultColor());
g2d.setStroke(new BasicStroke(1));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
g2d.draw(curve);
}
@@ -12,6 +12,7 @@ import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.diylc.common.Display;
import org.diylc.common.ObjectCache;
import org.diylc.core.ComponentState;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
@@ -65,12 +66,14 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
double distance = points[0].distance(points[1]);
Shape shape = getBodyShape();
if (shape == null) {
g2d.setStroke(new BasicStroke(getLeadThickness().convertToPixels()));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(
getLeadThickness().convertToPixels()));
g2d.setColor(shouldShadeLeads() ? getLeadColor(componentState).darker()
: getLeadColor(componentState));
g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
if (shouldShadeLeads()) {
g2d.setStroke(new BasicStroke(getLeadThickness().convertToPixels() - 2));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(
getLeadThickness().convertToPixels() - 2));
g2d.setColor(getLeadColor(componentState));
g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
}
@@ -99,7 +102,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
g2d.draw(shape);
// Draw leads.
int leadThickness = getClosestOdd(getLeadThickness().convertToPixels());
g2d.setStroke(new BasicStroke(leadThickness));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
g2d.setColor(shouldShadeLeads() ? getLeadColor(componentState).darker()
: getLeadColor(componentState));
g2d.drawLine((int) (shapeRect.width - distance) / 2, (int) shapeRect.height / 2,
@@ -109,7 +112,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
(int) ((shapeRect.width + distance) / 2 - leadLenght) + leadThickness / 2,
(int) shapeRect.height / 2);
if (shouldShadeLeads()) {
g2d.setStroke(new BasicStroke(leadThickness - 2));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness - 2));
g2d.setColor(getLeadColor(componentState));
g2d.drawLine((int) (shapeRect.width - distance) / 2, (int) shapeRect.height / 2,
(int) ((shapeRect.width - distance) / 2 + leadLenght) - leadThickness / 2,
@@ -23,6 +23,7 @@ import org.diylc.common.DrawOption;
import org.diylc.common.EventType;
import org.diylc.common.GridType;
import org.diylc.common.IComponentFiler;
import org.diylc.common.ObjectCache;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
@@ -249,7 +250,7 @@ public class DrawingManager {
// Draw component area for test
if (DEBUG_COMPONENT_AREAS) {
g2d.setStroke(new BasicStroke());
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
g2d.setColor(Color.red);
for (Area area : componentAreaMap.values()) {
g2d.draw(area);