From f6c7014f1b67751a45d711ac95e0ac99ae0f94a3 Mon Sep 17 00:00:00 2001 From: hneemann Date: Wed, 6 Apr 2016 14:31:52 +0200 Subject: [PATCH] made DataSet a Drawable --- src/main/dig/Ampel_TJK.dig | 199 +++++++++--------- .../digital/gui/components/data/DataSet.java | 73 ++++++- .../gui/components/data/DataSetComponent.java | 67 +----- 3 files changed, 178 insertions(+), 161 deletions(-) diff --git a/src/main/dig/Ampel_TJK.dig b/src/main/dig/Ampel_TJK.dig index 0393bb55f..5b66d3e41 100644 --- a/src/main/dig/Ampel_TJK.dig +++ b/src/main/dig/Ampel_TJK.dig @@ -1,16 +1,39 @@ + 1 And - + 0 Const - + + 0 + + + Clock + + + Label + Takt + + + + 0 + + + In + + + Label + Feuer + + + 0 @@ -21,7 +44,7 @@ Rot - + 0 @@ -41,7 +64,19 @@ Gelb - + + 0 + + + Or + + + 0 + + + Not + + 0 @@ -61,172 +96,138 @@ GrĂ¼n - - 0 - - - Clock - - - Label - Clock - - - - 0 - - - Or - - - 0 - - - Not - - - 0 - - - In - - - Label - Feuer - - - + 0 T-JK.dig - + 0 T-JK.dig - + 0 - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + \ No newline at end of file diff --git a/src/main/java/de/neemann/digital/gui/components/data/DataSet.java b/src/main/java/de/neemann/digital/gui/components/data/DataSet.java index 38bd43057..33c266624 100644 --- a/src/main/java/de/neemann/digital/gui/components/data/DataSet.java +++ b/src/main/java/de/neemann/digital/gui/components/data/DataSet.java @@ -1,6 +1,11 @@ package de.neemann.digital.gui.components.data; import de.neemann.digital.core.Model; +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.draw.shapes.Drawable; import java.util.ArrayList; import java.util.Iterator; @@ -11,10 +16,11 @@ import java.util.Iterator; * * @author hneemann */ -public class DataSet implements Iterable { +public class DataSet implements Iterable, Drawable { private static final int MAX_SAMPLES = 1000; private final ArrayList signals; private final ArrayList samples; + private final int maxTextLength; private DataSample min; private DataSample max; @@ -26,6 +32,13 @@ public class DataSet implements Iterable { public DataSet(ArrayList signals) { this.signals = signals; samples = new ArrayList<>(); + int tl = 0; + for (int i = 0; i < signalSize(); i++) { + String text = getSignal(i).getName(); + int w = text.length(); + if (w > tl) tl = w; + } + maxTextLength = tl; } /** @@ -102,4 +115,62 @@ public class DataSet implements Iterable { public Model.Signal getSignal(int i) { return signals.get(i); } + + + private static final int BORDER = 10; + private static final int SIZE = 25; + private static final int SEP2 = 5; + private static final int SEP = SEP2 * 2; + + + @Override + public void drawTo(Graphic g, boolean highLight) { + int x = getTextBorder(); + + int yOffs = SIZE / 2; + int y = BORDER; + for (int i = 0; i < signalSize(); i++) { + String text = getSignal(i).getName(); + g.drawText(new Vector(x - 2, y + yOffs), new Vector(x + 1, y + yOffs), text, Orientation.RIGHTCENTER, Style.NORMAL); + g.drawLine(new Vector(x, y - SEP2), new Vector(x + SIZE * size(), y - SEP2), Style.DASH); + y += SIZE + SEP; + } + g.drawLine(new Vector(x, y - SEP2), new Vector(x + SIZE * size(), y - SEP2), Style.DASH); + + + int[] lastRy = new int[signalSize()]; + boolean first = true; + for (DataSample s : this) { + g.drawLine(new Vector(x, BORDER - SEP2), new Vector(x, (SIZE + SEP) * signalSize() + BORDER - SEP2), Style.DASH); + y = BORDER; + for (int i = 0; i < signalSize(); i++) { + + long width = getWidth(i); + if (width == 0) width = 1; + //int ry = (int) (SIZE-(SIZE*(s.getValue(i)-dataSet.getMin().getValue(i)))/ width); + int ry = (int) (SIZE - (SIZE * s.getValue(i)) / width); + g.drawLine(new Vector(x, y + ry), new Vector(x + SIZE, y + ry), Style.NORMAL); + if (!first && ry != lastRy[i]) + g.drawLine(new Vector(x, y + lastRy[i]), new Vector(x, y + ry), Style.NORMAL); + + lastRy[i] = ry; + y += SIZE + SEP; + } + first = false; + x += SIZE; + } + g.drawLine(new Vector(x, BORDER - SEP2), new Vector(x, (SIZE + SEP) * signalSize() + BORDER - SEP2), Style.DASH); + } + + private int getTextBorder() { + return maxTextLength * Style.NORMAL.getFontSize() / 2 + BORDER + SEP; + } + + public int getGraphicWidth() { + return getTextBorder() + size() * SIZE; + } + + public int getGraphicHeight() { + return signalSize() * (SIZE + SEP) + 2 * BORDER; + } } diff --git a/src/main/java/de/neemann/digital/gui/components/data/DataSetComponent.java b/src/main/java/de/neemann/digital/gui/components/data/DataSetComponent.java index 01056d959..9ba359e76 100644 --- a/src/main/java/de/neemann/digital/gui/components/data/DataSetComponent.java +++ b/src/main/java/de/neemann/digital/gui/components/data/DataSetComponent.java @@ -1,5 +1,7 @@ package de.neemann.digital.gui.components.data; +import de.neemann.digital.draw.graphics.GraphicSwing; + import javax.swing.*; import java.awt.*; @@ -10,15 +12,7 @@ import java.awt.*; * @author hneemann */ public class DataSetComponent extends JComponent { - private static final int BORDER = 10; - private static final int SIZE = 20; - private static final int SEP2 = 3; - private static final int SEP = SEP2 * 2; - private static final Stroke NORMAL = new BasicStroke(0); - private static final Stroke THICK = new BasicStroke(2); - private static final int MIN_COUNT = 20; private final DataSet dataSet; - private int textWidth; /** * Creates a new dataSet @@ -35,62 +29,13 @@ public class DataSetComponent extends JComponent { g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); - textWidth = 0; - for (int i = 0; i < dataSet.signalSize(); i++) { - String text = dataSet.getSignal(i).getName(); - int w = g.getFontMetrics().stringWidth(text); - if (w > textWidth) textWidth = w; - } - int x = textWidth + BORDER + SEP; - - int yOffs = SIZE / 2 + g.getFontMetrics().getHeight() / 2; - g.setColor(Color.BLACK); - int y = BORDER; - for (int i = 0; i < dataSet.signalSize(); i++) { - String text = dataSet.getSignal(i).getName(); - g2.setColor(Color.BLACK); - g.drawString(text, BORDER, y + yOffs); - g2.setColor(Color.LIGHT_GRAY); - g.drawLine(x, y - SEP2, x + SIZE * dataSet.size(), y - SEP2); - y += SIZE + SEP; - } - g.drawLine(x, y - SEP2, x + SIZE * dataSet.size(), y - SEP2); - - - int[] lastRy = new int[dataSet.signalSize()]; - boolean first = true; - for (DataSample s : dataSet) { - g2.setStroke(NORMAL); - g2.setColor(Color.LIGHT_GRAY); - g.drawLine(x, BORDER - SEP2, x, (SIZE + SEP) * dataSet.signalSize() + BORDER - SEP2); - g2.setStroke(THICK); - g2.setColor(Color.BLACK); - y = BORDER; - for (int i = 0; i < dataSet.signalSize(); i++) { - - long width = dataSet.getWidth(i); - if (width == 0) width = 1; - //int ry = (int) (SIZE-(SIZE*(s.getValue(i)-dataSet.getMin().getValue(i)))/ width); - int ry = (int) (SIZE - (SIZE * s.getValue(i)) / width); - g.drawLine(x, y + ry, x + SIZE, y + ry); - if (!first && ry != lastRy[i]) - g.drawLine(x, y + lastRy[i], x, y + ry); - - lastRy[i] = ry; - y += SIZE + SEP; - } - first = false; - x += SIZE; - } - g2.setStroke(NORMAL); - g2.setColor(Color.LIGHT_GRAY); - g.drawLine(x, BORDER - SEP2, x, (SIZE + SEP) * dataSet.signalSize() + BORDER - SEP2); + dataSet.drawTo(new GraphicSwing(g2), false); } @Override public Dimension getPreferredSize() { - int count = dataSet.size(); - if (count < MIN_COUNT) count = MIN_COUNT; - return new Dimension(SIZE * count + BORDER * 2 + textWidth, (SIZE + SEP) * dataSet.signalSize() + BORDER * 2); + int w = dataSet.getGraphicWidth(); + if (w < 800) w = 800; + return new Dimension(w, dataSet.getGraphicHeight()); } }