JDiagram alpha commit

thobaben_serialize
Harald Wolff 2015-02-03 00:19:56 +01:00
parent c331dd61b3
commit 7d03b7682f
4 changed files with 329 additions and 7 deletions

View File

@ -1,36 +1,208 @@
package org.hwo.diagram;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;
public class Diagram {
private Double scale_x_min,
scale_x_max,
scale_y_min,
scale_y_max;
class ColoredBackground {
Float min,max;
Color color;
public ColoredBackground(Color color,float min,float max){
this.min = min;
this.max = max;
this.color = color;
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
public Float getMax() {
return max;
}
public void setMax(Float max) {
this.max = max;
}
public Float getMin() {
return min;
}
public void setMin(Float min) {
this.min = min;
}
}
private Float scale_x_min = 0.0f,
scale_x_max = 1.0f,
scale_y_min = 0.0f,
scale_y_max = 1.0f;
private int x0,
x1,
y0,
y1;
private float yscale;
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>();
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 Plot newPlot(){
Plot p = new Plot(this);
p.resize(this.scaleIndeces.size());
this.plots.add(p);
return p;
}
public void autoScale(){
Float max = Float.MIN_VALUE,min = Float.MAX_VALUE;
for (Plot p:plots){
Float mi,ma;
mi = p.getMin();
ma = p.getMax();
if (mi < min)
min = mi;
if (ma > max)
max = ma;
}
scale_y_min = min;
scale_y_max = max;
System.err.println(String.format("AutoScale: %f : %f", scale_y_min, scale_y_max));
}
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);
}
public BufferedImage plot(){
return null;
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);
plot(image.createGraphics(),width,height);
return image;
}
public void plot(Graphics2D g,int width,int height){
prepareLayout(width, height);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
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);
}
g.setColor(Color.BLACK);
g.drawLine(x0, y0, 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;
}
}
}
}

View File

@ -1,13 +1,18 @@
package org.hwo.diagram;
import java.util.Arrays;
public class Plot {
private Diagram diagram;
private String label;
private Float[] values;
protected Plot(Diagram diagram){
this.diagram = diagram;
this.values = new Float[0];
}
public Diagram getDiagram() {
@ -21,5 +26,49 @@ public class Plot {
this.label = label;
}
public Float[] getValues() {
return this.values;
};
public void setValues(Float[] values) {
this.values = values;
}
public Float getValue(int index){
return this.values[index];
}
public void setValue(int index,Float value){
this.values[index] = value;
}
public Float getMin(){
Float min = Float.MAX_VALUE;
for (Float f: values)
if (f < min)
min = f;
return min;
};
public Float getMax(){
Float max = Float.MIN_VALUE;
for (Float f: values)
if (f > max)
max = f;
return max;
};
void resize(int points){
int l = Math.min(this.values.length,points);
Float[] nv = new Float[points];
System.arraycopy(this.values, 0, nv, 0, l);
this.values = nv;
}
}

View File

@ -0,0 +1,44 @@
package org.hwo.ui;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.hwo.diagram.Diagram;
public class JDiagram extends JPanel {
private Diagram diagram;
public JDiagram(){
diagram = new Diagram();
}
@Override
public void paint(Graphics g) {
this.diagram.plot((Graphics2D)g, this.getWidth(), this.getHeight());
}
public Diagram getDiagram() {
return diagram;
}
public void setDiagram(Diagram diagram) {
this.diagram = diagram;
}
public Float getScaleYMax() {
return diagram.getScaleYMax();
}
public void setScaleYMax(Float scale_y_max) {
this.diagram.setScaleYMax(scale_y_max);
}
public Float getScaleYMin() {
return diagram.getScaleYMin();
}
public void setScaleYMin(Float scale_y_min) {
this.diagram.setScaleYMin(scale_y_min);
}
}

View File

@ -0,0 +1,57 @@
package org.hwo.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
public class JUITest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JUITest frame = new JUITest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JUITest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1160, 323);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{186, 0};
gbl_contentPane.rowHeights = new int[]{0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JDiagram diagram = new JDiagram();
GridBagConstraints gbc_diagram = new GridBagConstraints();
gbc_diagram.fill = GridBagConstraints.BOTH;
gbc_diagram.gridx = 0;
gbc_diagram.gridy = 0;
contentPane.add(diagram, gbc_diagram);
}
}