From de380ad1a14f3f27ae96869c863dc1f7351e25e7 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 3 Feb 2015 03:05:17 +0100 Subject: [PATCH] Diagram Beta --- src/org/hwo/diagram/CirclePlotPainter.java | 29 +++ src/org/hwo/diagram/Diagram.java | 236 ++++++++++++-------- src/org/hwo/diagram/LinePlotPainter.java | 27 +++ src/org/hwo/diagram/PlotPainter.java | 11 + src/org/hwo/diagram/PlotProvider.java | 17 ++ src/org/hwo/diagram/SimplePlotProvider.java | 70 ++++++ src/org/hwo/ui/JDiagram.java | 25 ++- src/org/hwo/ui/JUITest.java | 41 +++- 8 files changed, 347 insertions(+), 109 deletions(-) create mode 100644 src/org/hwo/diagram/CirclePlotPainter.java create mode 100644 src/org/hwo/diagram/LinePlotPainter.java create mode 100644 src/org/hwo/diagram/PlotPainter.java create mode 100644 src/org/hwo/diagram/PlotProvider.java create mode 100644 src/org/hwo/diagram/SimplePlotProvider.java diff --git a/src/org/hwo/diagram/CirclePlotPainter.java b/src/org/hwo/diagram/CirclePlotPainter.java new file mode 100644 index 0000000..5110f13 --- /dev/null +++ b/src/org/hwo/diagram/CirclePlotPainter.java @@ -0,0 +1,29 @@ +package org.hwo.diagram; + +import java.awt.Color; +import java.awt.Graphics2D; + +public class CirclePlotPainter implements PlotPainter { + + int radius; + + public CirclePlotPainter() { + this.radius = 3; + } + + public CirclePlotPainter(int radius){ + this.radius = radius; + } + + @Override + public void reset() { + } + + @Override + public void paintPoint(Graphics2D g, Color color, int x, int y) { + g.setColor(color); + g.drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360); + g.fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360); + } + +} diff --git a/src/org/hwo/diagram/Diagram.java b/src/org/hwo/diagram/Diagram.java index 8d26c66..1d76bf8 100644 --- a/src/org/hwo/diagram/Diagram.java +++ b/src/org/hwo/diagram/Diagram.java @@ -1,8 +1,12 @@ package org.hwo.diagram; import java.awt.Color; +import java.awt.Desktop; +import java.awt.Font; import java.awt.Graphics2D; +import java.awt.Toolkit; import java.awt.image.BufferedImage; +import java.io.ObjectInputStream.GetField; import java.util.ArrayList; import java.util.LinkedList; @@ -42,114 +46,103 @@ public class Diagram { } - - private Float scale_x_min = 0.0f, - scale_x_max = 1.0f, - scale_y_min = 0.0f, - scale_y_max = 1.0f; + private PlotProvider plotProvider; + private PlotPainter plotPainter; private int x0, x1, y0, y1; + int yh,xw; + private float yscale; + private float yoffset; + private Font font; + private int numLabels; - private ArrayList scaleIndeces; - - private LinkedList plots; private LinkedList coloredBackgrounds; - - public Diagram(){ - plots = new LinkedList(); - scaleIndeces = new ArrayList(); coloredBackgrounds = new LinkedList(); + yoffset = -1.0f; + yscale = 2.0f; + plotProvider = new SimplePlotProvider(2, 256); + plotPainter = new LinePlotPainter(); + font = new Font("Arial", Font.PLAIN, 24); + numLabels = 0; coloredBackgrounds.add(new ColoredBackground(new Color(0.7f, 1.0f, 0.7f),-0.25f,0.25f)); coloredBackgrounds.add(new ColoredBackground(new Color(1.0f, 1.0f, 0.5f),-0.5f,-0.25f)); coloredBackgrounds.add(new ColoredBackground(new Color(1.0f, 1.0f, 0.5f),0.25f,0.5f)); - scalarScale(0, 128); - Plot p = newPlot(); - for (int i=0;i<128;i++){ - p.setValue(i, 1.0f - (2.0f * (float)Math.random())); - } - + } - setScaleYMin(-1.0f); - setScaleYMax(1.0f); + public float getYoffset() { + return yoffset; + } + public void setYoffset(float yoffset) { + this.yoffset = yoffset; + } + public float getYscale() { + return yscale; + } + public void setYscale(float yscale) { + this.yscale = yscale; } - public Plot newPlot(){ - Plot p = new Plot(this); - p.resize(this.scaleIndeces.size()); - this.plots.add(p); - return p; + public Font getFont() { + return font; } + public void setFont(Font font) { + this.font = font; + } + + public int getNumLabels() { + return numLabels; + } + public void setNumLabels(int numLabels) { + this.numLabels = numLabels; + } + + public PlotPainter getPlotPainter() { + return plotPainter; + } + public void setPlotPainter(PlotPainter plotPainter) { + this.plotPainter = plotPainter; + } + + public PlotProvider getPlotProvider() { + return plotProvider; + } + public void setPlotProvider(PlotProvider plotProvider) { + this.plotProvider = plotProvider; + } + public void autoScale(){ Float max = Float.MIN_VALUE,min = Float.MAX_VALUE; + Float[][] matrix = this.plotProvider.getMatrix(); - for (Plot p:plots){ - Float mi,ma; - - mi = p.getMin(); - ma = p.getMax(); - - if (mi < min) - min = mi; - if (ma > max) - max = ma; - + + for (Float[] p:matrix){ + for (Float value: p){ + if (value < min) + min = value; + if (value > max) + max = value; + } } - scale_y_min = min; - scale_y_max = max; + yoffset = min; + yscale = max - min; - System.err.println(String.format("AutoScale: %f : %f", scale_y_min, scale_y_max)); + System.err.println(String.format("AutoScale: %f x %f", yoffset, yscale)); } - public void scalarScale(int minX,int maxX){ - scaleIndeces.clear(); - for (int i=minX;i x0) + x0 = lw; + } + for (int i=0;i<=nLabels;i++){ + String l = String.format("%.4f", (yoffset + (yscale * i / nLabels))); + int lw = g.getFontMetrics().stringWidth(l); + + g.drawString(l, 2 + (x0 - lw), y0 - (yh * i / nLabels) + (lineHeight/4)); + } + x0 += 10; + xw = x1 - x0; + + for (int i=0;i<=nLabels;i++){ + g.drawLine(x0 - 5, y0 - (yh * i / nLabels) , x0, y0 - (yh * i / nLabels)); + } + + for (int i=0;i plots; + ArrayList labels; + Color[] colors; + + public SimplePlotProvider(int plots,int points) { + this.points = points; + this.plots = new ArrayList(); + this.labels = new ArrayList(); + this.xmin = 0; + this.colors = new Color[plots]; + + for (int i=0;i