Diagram Beta

thobaben_serialize
Harald Wolff 2015-02-03 03:05:17 +01:00
parent 7d03b7682f
commit de380ad1a1
8 changed files with 347 additions and 109 deletions

View File

@ -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);
}
}

View File

@ -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<Object> scaleIndeces;
private LinkedList<Plot> plots;
private LinkedList<ColoredBackground> coloredBackgrounds;
public Diagram(){
plots = new LinkedList<Plot>();
scaleIndeces = new ArrayList<Object>();
coloredBackgrounds = new LinkedList<Diagram.ColoredBackground>();
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<maxX;i++){
scaleIndeces.add(new Integer(i));
}
}
public float getScaleYMax() {
return scale_y_max;
}
public void setScaleYMax(float scale_y_max) {
this.scale_y_max = scale_y_max;
}
public float getScaleYMin() {
return scale_y_min;
}
public void setScaleYMin(float scale_y_min) {
this.scale_y_min = scale_y_min;
}
private void prepareLayout(int width,int height){
x0 = 40;
x1 = width - 40;
y0 = height - 40;
y1 = 40;
yscale = ((float)(y0 - y1)) / (scale_y_max - scale_y_min);
}
private int value2y(float v){
return y0 - (int)(v * yscale) + (int)(scale_y_min * yscale);
}
public BufferedImage plot(int width,int height){
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
@ -158,51 +151,98 @@ public class Diagram {
return image;
}
public int value2y(float value){
return y0 - (int)(yh * ((value - yoffset)/yscale));
}
public void plot(Graphics2D g,int width,int height){
prepareLayout(width, height);
int lineHeight = font.getSize() * Toolkit.getDefaultToolkit().getScreenResolution() / 72 ;
int nLabels = numLabels;
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
y0 = height - lineHeight - 10;
y1 = 10;
yh = y0 - y1;
x0 = 0;
x1 = width - 10;
for (ColoredBackground bg: coloredBackgrounds){
int ya = value2y(bg.getMin());
int yb = value2y(bg.getMax());
g.setColor(bg.getColor());
g.fillRect(x0, yb, x1, ya - yb);
if (nLabels == 0){
nLabels = ((y0 - y1) / lineHeight);
}
g.setColor(Color.BLACK);
if (this.font != null)
g.setFont(this.font);
g.drawLine(x0, y0, x0, y1 - 10);
for (int i=0;i<=nLabels;i++){
String l = String.format("%.4f", (yoffset + (yscale * i / nLabels)));
int lw = g.getFontMetrics().stringWidth(l);
if (lw > 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<this.plotProvider.getPoints();i++){
g.drawLine(x0 + (xw * i / this.plotProvider.getPoints()), y0, x0 + (xw * i / this.plotProvider.getPoints()), y0 + 5);
String xlabel = this.plotProvider.getPointLabel(i);
int lw = g.getFontMetrics().stringWidth(xlabel);
g.drawString(xlabel, x0 + (xw * i / this.plotProvider.getPoints()) - (lw/2), y0 + lineHeight);
}
for (ColoredBackground bg: coloredBackgrounds){
int ya = value2y(bg.getMin());
int yb = value2y(bg.getMax());
g.setColor(bg.getColor());
g.fillRect(x0, yb, xw, ya - yb);
}
g.setColor(Color.BLACK);
g.drawLine(x0, y0 + 5, x0, y1 - 10);
g.drawLine(x0, y1 - 10 , x0 - 5, y1 - 5);
g.drawLine(x0, y1 - 10 , x0 + 5, y1 - 5);
g.drawLine(x0, y0, x1 + 10, y0);
g.drawLine(x1 + 10 , y0, x1 + 5, y0 - 5);
g.drawLine(x1 + 10 , y0, x1 + 5, y0 + 5);
g.setColor(Color.RED);
for (Plot plot: this.plots){
int lx = 0,ly = 0;
for (int x=x0; x<x1;x++){
int index = scaleIndeces.size() * (x - x0) / (x1 - x0);
int y = value2y(plot.getValue(index));
if (x != x0){
g.drawLine(lx,ly, x , y );
}
lx = x;
ly = y;
Float[][] matrix = this.plotProvider.getMatrix();
Color[] colors = this.plotProvider.getColors();
for (int n=0;n<this.plotProvider.getPlots();n++){
plotPainter.reset();
for (int i=0;i<this.plotProvider.getPoints();i++){
int x,y;
if (matrix[n][i] != null){
x = x0 + (i * xw / this.plotProvider.getPoints() );
y = value2y(matrix[n][i]);
plotPainter.paintPoint(g, colors[n], x, y);
};
}
}
}
}

View File

@ -0,0 +1,27 @@
package org.hwo.diagram;
import java.awt.Color;
import java.awt.Graphics2D;
public class LinePlotPainter implements PlotPainter {
int lx,ly;
@Override
public void reset() {
lx = -1;
ly = -1;
}
@Override
public void paintPoint(Graphics2D g, Color color, int x, int y) {
if (lx != -1){
g.setColor(color);
g.drawLine(lx, ly, x, y);
}
lx = x;
ly = y;
}
}

View File

@ -0,0 +1,11 @@
package org.hwo.diagram;
import java.awt.Color;
import java.awt.Graphics2D;
public interface PlotPainter {
void reset();
void paintPoint(Graphics2D g,Color color,int x,int y);
}

View File

@ -0,0 +1,17 @@
package org.hwo.diagram;
import java.awt.Color;
public interface PlotProvider {
int getPoints();
int getPlots();
Float[][] getMatrix();
String getLabel(int plot);
String getPointLabel(int point);
Color[] getColors();
}

View File

@ -0,0 +1,70 @@
package org.hwo.diagram;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
public class SimplePlotProvider implements PlotProvider {
int points;
int xmin;
ArrayList<Float[]> plots;
ArrayList<String> labels;
Color[] colors;
public SimplePlotProvider(int plots,int points) {
this.points = points;
this.plots = new ArrayList<Float[]>();
this.labels = new ArrayList<String>();
this.xmin = 0;
this.colors = new Color[plots];
for (int i=0;i<plots;i++){
this.plots.add(new Float[points]);
this.labels.add(String.format("Plot %d", i));
Arrays.fill(this.plots.get(i), 0.0f);
this.colors[i] = new Color(255,0,0);
}
}
public int getXmin() {
return xmin;
}
public void setXmin(int xmin) {
this.xmin = xmin;
}
@Override
public int getPoints() {
return this.points;
}
@Override
public int getPlots() {
return this.plots.size();
}
@Override
public Float[][] getMatrix() {
return this.plots.toArray(new Float[][]{});
}
@Override
public String getLabel(int plot) {
return this.labels.get(plot);
}
@Override
public String getPointLabel(int point) {
return String.format("%d", (this.xmin + point));
}
@Override
public Color[] getColors() {
return this.colors;
}
}

View File

@ -1,5 +1,6 @@
package org.hwo.ui;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
@ -14,6 +15,7 @@ public class JDiagram extends JPanel {
public JDiagram(){
diagram = new Diagram();
diagram.setFont( getFont() );
}
@Override
@ -28,17 +30,22 @@ public class JDiagram extends JPanel {
this.diagram = diagram;
}
public Float getScaleYMax() {
return diagram.getScaleYMax();
public int getNumLabels(){
return this.diagram.getNumLabels();
}
public void setScaleYMax(Float scale_y_max) {
this.diagram.setScaleYMax(scale_y_max);
public void setNumLabels(int nLabels){
this.diagram.setNumLabels(nLabels);
}
public Float getScaleYMin() {
return diagram.getScaleYMin();
@Override
public Font getFont() {
return super.getFont();
}
@Override
public void setFont(Font font) {
if (diagram != null)
diagram.setFont(font);
super.setFont(font);
}
public void setScaleYMin(Float scale_y_min) {
this.diagram.setScaleYMin(scale_y_min);
}
}

View File

@ -6,13 +6,19 @@ import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.hwo.diagram.CirclePlotPainter;
import org.hwo.diagram.SimplePlotProvider;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Font;
public class JUITest extends JFrame {
private JPanel contentPane;
private JDiagram diagram;
/**
* Launch the application.
@ -35,7 +41,7 @@ public class JUITest extends JFrame {
*/
public JUITest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1160, 323);
setBounds(100, 100, 693, 323);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
@ -46,12 +52,43 @@ public class JUITest extends JFrame {
gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JDiagram diagram = new JDiagram();
diagram = new JDiagram();
diagram.setNumLabels(4);
diagram.setFont(new Font("Nimbus Mono L", Font.PLAIN, 14));
GridBagConstraints gbc_diagram = new GridBagConstraints();
gbc_diagram.fill = GridBagConstraints.BOTH;
gbc_diagram.gridx = 0;
gbc_diagram.gridy = 0;
contentPane.add(diagram, gbc_diagram);
this.initialize();
}
private void initialize(){
SimplePlotProvider p = new SimplePlotProvider(2, 32);
Float f = 0.0f;
Float[] values = p.getMatrix()[0];
for (int i=0;i<p.getPoints();i++){
f += (float)(Math.random()-0.5f)*0.5f;
values[i] = f;
}
p.getColors()[0] = Color.BLUE;
values = p.getMatrix()[1];
for (int i=0;i<p.getPoints();i++){
if ((i&1) == 0){
f += (float)(Math.random()-0.5f)*0.5f;
values[i] = f;
} else {
values[i] = null;
}
}
diagram.getDiagram().setPlotPainter(new CirclePlotPainter());
diagram.getDiagram().setPlotProvider(p);
//diagram.getDiagram().autoScale();
}
}