added some documentation

This commit is contained in:
hneemann 2016-04-17 11:30:51 +02:00
parent abcb49b2c7
commit 4c75bfd745
6 changed files with 117 additions and 69 deletions

View File

@ -158,9 +158,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
// draw circle around element // draw circle around element
if (highLight) { if (highLight) {
GraphicMinMax mm = getMinMax(); GraphicMinMax mm = getMinMax();
Vector delta = mm.getMax().sub(mm.getMin()); Vector delta = mm.getMax().sub(mm.getMin()).mul(3).div(5);
int rad = (int) Math.sqrt(delta.x * delta.x + delta.y * delta.y) / 2;
delta = new Vector(rad, rad);
Vector pos = mm.getMax().add(mm.getMin()).div(2); Vector pos = mm.getMax().add(mm.getMin()).div(2);
graphic.drawCircle(pos.sub(delta), pos.add(delta), Style.HIGHLIGHT); graphic.drawCircle(pos.sub(delta), pos.add(delta), Style.HIGHLIGHT);
} }

View File

@ -15,9 +15,9 @@ public class GraphicSVG implements Graphic, Closeable {
/** /**
* Creates a new instance. * Creates a new instance.
* *
* @param out the stream * @param out the stream
* @param min upper left corner * @param min upper left corner
* @param max lower right corner * @param max lower right corner
* @throws IOException IOException * @throws IOException IOException
*/ */
public GraphicSVG(OutputStream out, Vector min, Vector max) throws IOException { public GraphicSVG(OutputStream out, Vector min, Vector max) throws IOException {
@ -79,38 +79,36 @@ public class GraphicSVG implements Graphic, Closeable {
@Override @Override
public void drawLine(Vector p1, Vector p2, Style style) { public void drawLine(Vector p1, Vector p2, Style style) {
if (style != Style.INVISIBLE) try {
try { w.write("<line x1=\"" + p1.x + "\" y1=\"" + p1.y + "\" x2=\"" + p2.x + "\" y2=\"" + p2.y + "\" stroke=\"" + getColor(style) + "\" stroke-linecap=\"square\" stroke-width=\"" + getStrokeWidth(style) + "\"");
w.write("<line x1=\"" + p1.x + "\" y1=\"" + p1.y + "\" x2=\"" + p2.x + "\" y2=\"" + p2.y + "\" stroke=\"" + getColor(style) + "\" stroke-linecap=\"square\" stroke-width=\"" + getStrokeWidth(style) + "\"");
// if (style.isDashed()) // if (style.isDashed())
// addStrokeDash(w, style.getDashArray()); // addStrokeDash(w, style.getDashArray());
w.write(" />\n"); w.write(" />\n");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public void drawPolygon(Polygon p, Style style) { public void drawPolygon(Polygon p, Style style) {
if (style != Style.INVISIBLE) try {
try { w.write("<path d=\"M " + str(p.get(0)));
w.write("<path d=\"M " + str(p.get(0))); for (int i = 1; i < p.size(); i++)
for (int i = 1; i < p.size(); i++) w.write(" L " + str(p.get(i)));
w.write(" L " + str(p.get(i)));
if (p.isClosed()) if (p.isClosed())
w.write(" Z"); w.write(" Z");
w.write("\""); w.write("\"");
// if (style.isDashed()) // if (style.isDashed())
// addStrokeDash(w, style.getDashArray()); // addStrokeDash(w, style.getDashArray());
if (style.isFilled() && p.isClosed()) if (style.isFilled() && p.isClosed())
w.write(" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"" + getColor(style) + "\"/>\n"); w.write(" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"" + getColor(style) + "\"/>\n");
else else
w.write(" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"none\"/>\n"); w.write(" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"none\"/>\n");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private static double getStrokeWidth(Style style) { private static double getStrokeWidth(Style style) {
@ -119,21 +117,20 @@ public class GraphicSVG implements Graphic, Closeable {
@Override @Override
public void drawCircle(Vector p1, Vector p2, Style style) { public void drawCircle(Vector p1, Vector p2, Style style) {
if (style != Style.INVISIBLE) try {
try { Vector c = p1.add(p2).div(2);
Vector c = p1.add(p2).div(2); double r = Math.abs(p2.sub(p1).x) / 2.0;
double r = Math.abs(p2.sub(p1).x) / 2.0; if (style.isFilled())
if (style.isFilled()) w.write("<circle cx=\"" + c.x + "\" cy=\"" + c.y + "\" r=\"" + r + "\" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"" + getColor(style) + "\" />\n");
w.write("<circle cx=\"" + c.x + "\" cy=\"" + c.y + "\" r=\"" + r + "\" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"" + getColor(style) + "\" />\n"); else {
else { w.write("<circle cx=\"" + c.x + "\" cy=\"" + c.y + "\" r=\"" + r + "\" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"none\"");
w.write("<circle cx=\"" + c.x + "\" cy=\"" + c.y + "\" r=\"" + r + "\" stroke=\"" + getColor(style) + "\" stroke-width=\"" + getStrokeWidth(style) + "\" fill=\"none\"");
// if (style.isDashed()) // if (style.isDashed())
// addStrokeDash(w, style.getDashArray()); // addStrokeDash(w, style.getDashArray());
w.write(" />\n"); w.write(" />\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
} }
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
@Override @Override

View File

@ -27,32 +27,28 @@ public class GraphicSwing implements Graphic {
@Override @Override
public void drawLine(Vector p1, Vector p2, Style style) { public void drawLine(Vector p1, Vector p2, Style style) {
if (style != Style.INVISIBLE) { applyStyle(style);
applyStyle(style); gr.drawLine(p1.x, p1.y, p2.x, p2.y);
gr.drawLine(p1.x, p1.y, p2.x, p2.y);
}
} }
@Override @Override
public void drawPolygon(Polygon p, Style style) { public void drawPolygon(Polygon p, Style style) {
if (style != Style.INVISIBLE) { applyStyle(style);
applyStyle(style); Path2D path = new GeneralPath();
Path2D path = new GeneralPath(); boolean first = true;
boolean first = true; for (Vector v : p)
for (Vector v : p) if (first) {
if (first) { first = false;
first = false; path.moveTo(v.x, v.y);
path.moveTo(v.x, v.y); } else
} else path.lineTo(v.x, v.y);
path.lineTo(v.x, v.y);
if (p.isClosed()) if (p.isClosed())
path.closePath(); path.closePath();
if (style.isFilled() && p.isClosed()) if (style.isFilled() && p.isClosed())
gr.fill(path); gr.fill(path);
gr.draw(path); gr.draw(path);
}
} }
@Override @Override

View File

@ -1,17 +1,46 @@
package de.neemann.digital.draw.graphics; package de.neemann.digital.draw.graphics;
/** /**
* The text orientation
*
* @author hneemann * @author hneemann
*/ */
public enum Orientation { public enum Orientation {
/**
* the anchor point is at the left side of the text at the bottom line
*/
LEFTBOTTOM(0, 0), LEFTBOTTOM(0, 0),
/**
* the anchor point is at the center of the text at the bottom line
*/
CENTERBOTTOM(1, 0), CENTERBOTTOM(1, 0),
/**
* the anchor point is at the right side of the text at the bottom line
*/
RIGHTBOTTOM(2, 0), RIGHTBOTTOM(2, 0),
/**
* the anchor point is at the right side of the text in middle height
*/
RIGHTCENTER(2, 1), RIGHTCENTER(2, 1),
/**
* the anchor point is at the right side of the text at the top of the text
*/
RIGHTTOP(2, 2), RIGHTTOP(2, 2),
/**
* the anchor point is at the center of the text at the top of the text
*/
CENTERTOP(1, 2), CENTERTOP(1, 2),
/**
* the anchor point is at the left side of the text at the top of the text
*/
LEFTTOP(0, 2), LEFTTOP(0, 2),
/**
* the anchor point is at the left side of the text in middle height
*/
LEFTCENTER(0, 1), LEFTCENTER(0, 1),
/**
* the anchor point is in the center of the text
*/
CENTERCENTER(1, 1); CENTERCENTER(1, 1);

View File

@ -8,18 +8,50 @@ import java.awt.*;
* @author hneemann * @author hneemann
*/ */
public class Style { public class Style {
/**
* used for all lines to draw the shapes itself
*/
public static final Style NORMAL = new Style(4, false, Color.BLACK); public static final Style NORMAL = new Style(4, false, Color.BLACK);
/**
* thin line used for the graphic in the clock or delay shape
*/
public static final Style THIN = new Style(2, false, Color.BLACK); public static final Style THIN = new Style(2, false, Color.BLACK);
/**
* Used for wires in editing mode
*/
public static final Style WIRE = new Style(4, true, Color.BLUE.darker()); public static final Style WIRE = new Style(4, true, Color.BLUE.darker());
/**
* Used for low wires in running mode
*/
public static final Style WIRE_LOW = new Style(4, true, new Color(0, 112, 0)); public static final Style WIRE_LOW = new Style(4, true, new Color(0, 112, 0));
/**
* Used for high wires in running mode
*/
public static final Style WIRE_HIGH = new Style(4, true, new Color(102, 255, 102)); public static final Style WIRE_HIGH = new Style(4, true, new Color(102, 255, 102));
/**
* Used for wires in high Z state
*/
public static final Style WIRE_HIGHZ = new Style(4, true, Color.GRAY); public static final Style WIRE_HIGHZ = new Style(4, true, Color.GRAY);
/**
* used to draw the output dots
*/
public static final Style WIRE_OUT = new Style(4, true, Color.RED.darker()); public static final Style WIRE_OUT = new Style(4, true, Color.RED.darker());
/**
* filld style used to fill the splitter or the dark LEDs
*/
public static final Style FILLED = new Style(4, true, Color.BLACK); public static final Style FILLED = new Style(4, true, Color.BLACK);
/**
* Used to draw the grid in the graph
*/
public static final Style DASH = new Style(1, false, Color.BLACK, new float[]{4, 4}); public static final Style DASH = new Style(1, false, Color.BLACK, new float[]{4, 4});
/**
* Used todraw the pin description text
*/
public static final Style SHAPE_PIN = new Style(4, false, Color.GRAY, 18, null); public static final Style SHAPE_PIN = new Style(4, false, Color.GRAY, 18, null);
/**
* highlight color used for the circles to mark an element
*/
public static final Style HIGHLIGHT = new Style(4, false, Color.CYAN); public static final Style HIGHLIGHT = new Style(4, false, Color.CYAN);
public static final Style INVISIBLE = new Style(0, false, Color.WHITE);
private final int thickness; private final int thickness;
private final boolean filled; private final boolean filled;

View File

@ -5,7 +5,10 @@ import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.PinDescription; import de.neemann.digital.core.element.PinDescription;
import de.neemann.digital.draw.elements.IOState; import de.neemann.digital.draw.elements.IOState;
import de.neemann.digital.draw.elements.Pins; import de.neemann.digital.draw.elements.Pins;
import de.neemann.digital.draw.graphics.*; import de.neemann.digital.draw.graphics.Graphic;
import de.neemann.digital.draw.graphics.Orientation;
import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.graphics.Vector;
import de.neemann.digital.lang.Lang; import de.neemann.digital.lang.Lang;
/** /**
@ -41,13 +44,6 @@ public class TextShape implements Shape {
@Override @Override
public void drawTo(Graphic graphic, boolean highLight) { public void drawTo(Graphic graphic, boolean highLight) {
int size = Style.NORMAL.getFontSize();
graphic.drawPolygon(
new Polygon(true)
.add(0, 0)
.add(size * 2, 0)
.add(size * 2, size)
.add(0, size), Style.INVISIBLE);
graphic.drawText(new Vector(0, 0), new Vector(1, 0), label, Orientation.LEFTTOP, Style.NORMAL); graphic.drawText(new Vector(0, 0), new Vector(1, 0), label, Orientation.LEFTTOP, Style.NORMAL);
} }
} }