diff --git a/src/org/hwo/diagram/Diagram.java b/src/org/hwo/diagram/Diagram.java index 3622f82..8d26c66 100644 --- a/src/org/hwo/diagram/Diagram.java +++ b/src/org/hwo/diagram/Diagram.java @@ -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 scaleIndeces; private LinkedList plots; + private LinkedList coloredBackgrounds; + public Diagram(){ plots = new LinkedList(); + scaleIndeces = new ArrayList(); + coloredBackgrounds = new LinkedList(); + + 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 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; + } + + } diff --git a/src/org/hwo/ui/JDiagram.java b/src/org/hwo/ui/JDiagram.java new file mode 100644 index 0000000..4e51081 --- /dev/null +++ b/src/org/hwo/ui/JDiagram.java @@ -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); + } + +} diff --git a/src/org/hwo/ui/JUITest.java b/src/org/hwo/ui/JUITest.java new file mode 100644 index 0000000..8944093 --- /dev/null +++ b/src/org/hwo/ui/JUITest.java @@ -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); + } + +}