JDiagram Gamma Commit
parent
de380ad1a1
commit
367816f400
|
@ -0,0 +1,37 @@
|
|||
package org.hwo.diagram;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -9,43 +9,10 @@ import java.awt.image.BufferedImage;
|
|||
import java.io.ObjectInputStream.GetField;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Diagram {
|
||||
|
||||
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 PlotProvider plotProvider;
|
||||
private PlotPainter plotPainter;
|
||||
|
||||
|
@ -63,16 +30,20 @@ public class Diagram {
|
|||
|
||||
private int numLabels;
|
||||
|
||||
private String labelFormatSpec;
|
||||
|
||||
|
||||
private LinkedList<ColoredBackground> coloredBackgrounds;
|
||||
|
||||
public Diagram(){
|
||||
coloredBackgrounds = new LinkedList<Diagram.ColoredBackground>();
|
||||
coloredBackgrounds = new LinkedList<ColoredBackground>();
|
||||
yoffset = -1.0f;
|
||||
yscale = 2.0f;
|
||||
plotProvider = new SimplePlotProvider(2, 256);
|
||||
plotProvider = new SimplePlotProvider(2, 2);
|
||||
plotPainter = new LinePlotPainter();
|
||||
font = new Font("Arial", Font.PLAIN, 24);
|
||||
numLabels = 0;
|
||||
labelFormatSpec = "%.4f";
|
||||
|
||||
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));
|
||||
|
@ -122,6 +93,17 @@ public class Diagram {
|
|||
this.plotProvider = plotProvider;
|
||||
}
|
||||
|
||||
public String getLabelFormatSpec() {
|
||||
return labelFormatSpec;
|
||||
}
|
||||
public void setLabelFormatSpec(String labelFormatSpec) {
|
||||
this.labelFormatSpec = labelFormatSpec;
|
||||
}
|
||||
|
||||
public List<ColoredBackground> getColoredBackgrounds() {
|
||||
return coloredBackgrounds;
|
||||
}
|
||||
|
||||
|
||||
public void autoScale(){
|
||||
Float max = Float.MIN_VALUE,min = Float.MAX_VALUE;
|
||||
|
@ -151,10 +133,12 @@ public class Diagram {
|
|||
return image;
|
||||
}
|
||||
|
||||
public int value2y(float value){
|
||||
private int value2y(float value){
|
||||
return y0 - (int)(yh * ((value - yoffset)/yscale));
|
||||
}
|
||||
|
||||
private int point2x(int point){
|
||||
return x0 + (xw * point / (this.plotProvider.getPoints()-1));
|
||||
}
|
||||
|
||||
public void plot(Graphics2D g,int width,int height){
|
||||
int lineHeight = font.getSize() * Toolkit.getDefaultToolkit().getScreenResolution() / 72 ;
|
||||
|
@ -172,20 +156,30 @@ public class Diagram {
|
|||
if (nLabels == 0){
|
||||
nLabels = ((y0 - y1) / lineHeight);
|
||||
}
|
||||
if (nLabels == 0)
|
||||
nLabels = 1;
|
||||
|
||||
for (ColoredBackground bg: coloredBackgrounds){
|
||||
int ya = value2y(bg.getMin());
|
||||
int yb = value2y(bg.getMax());
|
||||
|
||||
g.setColor(bg.getColor());
|
||||
g.fillRect(0, yb, width, ya - yb);
|
||||
}
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
if (this.font != null)
|
||||
g.setFont(this.font);
|
||||
|
||||
|
||||
for (int i=0;i<=nLabels;i++){
|
||||
String l = String.format("%.4f", (yoffset + (yscale * i / nLabels)));
|
||||
String l = String.format(labelFormatSpec, (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)));
|
||||
String l = String.format(labelFormatSpec, (yoffset + (yscale * i / nLabels)));
|
||||
int lw = g.getFontMetrics().stringWidth(l);
|
||||
|
||||
g.drawString(l, 2 + (x0 - lw), y0 - (yh * i / nLabels) + (lineHeight/4));
|
||||
|
@ -198,22 +192,14 @@ public class Diagram {
|
|||
}
|
||||
|
||||
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);
|
||||
g.drawLine(point2x(i), y0, point2x(i), 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);
|
||||
g.drawString(xlabel, point2x(i) - (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);
|
||||
|
@ -235,7 +221,7 @@ public class Diagram {
|
|||
int x,y;
|
||||
|
||||
if (matrix[n][i] != null){
|
||||
x = x0 + (i * xw / this.plotProvider.getPoints() );
|
||||
x = point2x(i);
|
||||
y = value2y(matrix[n][i]);
|
||||
|
||||
plotPainter.paintPoint(g, colors[n], x, y);
|
||||
|
|
|
@ -66,5 +66,9 @@ public class SimplePlotProvider implements PlotProvider {
|
|||
public Color[] getColors() {
|
||||
return this.colors;
|
||||
}
|
||||
|
||||
public void setPlot(int plot,Float[] values){
|
||||
this.plots.set(plot, values);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.hwo.ui;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
|
@ -14,6 +15,7 @@ public class JDiagram extends JPanel {
|
|||
private Diagram diagram;
|
||||
|
||||
public JDiagram(){
|
||||
setMinimumSize(new Dimension(80, 80));
|
||||
diagram = new Diagram();
|
||||
diagram.setFont( getFont() );
|
||||
}
|
||||
|
@ -37,6 +39,13 @@ public class JDiagram extends JPanel {
|
|||
this.diagram.setNumLabels(nLabels);
|
||||
}
|
||||
|
||||
public String getLabelFormatSpec(){
|
||||
return this.diagram.getLabelFormatSpec();
|
||||
}
|
||||
public void setLabelFormatSpec(String spec){
|
||||
this.diagram.setLabelFormatSpec(spec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Font getFont() {
|
||||
return super.getFont();
|
||||
|
|
|
@ -41,7 +41,7 @@ public class JUITest extends JFrame {
|
|||
*/
|
||||
public JUITest() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 693, 323);
|
||||
setBounds(100, 100, 1111, 342);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
|
@ -53,7 +53,8 @@ public class JUITest extends JFrame {
|
|||
contentPane.setLayout(gbl_contentPane);
|
||||
|
||||
diagram = new JDiagram();
|
||||
diagram.setNumLabels(4);
|
||||
diagram.setLabelFormatSpec("%.2f");
|
||||
diagram.setNumLabels(6);
|
||||
diagram.setFont(new Font("Nimbus Mono L", Font.PLAIN, 14));
|
||||
GridBagConstraints gbc_diagram = new GridBagConstraints();
|
||||
gbc_diagram.fill = GridBagConstraints.BOTH;
|
||||
|
@ -88,6 +89,9 @@ public class JUITest extends JFrame {
|
|||
diagram.getDiagram().setPlotPainter(new CirclePlotPainter());
|
||||
diagram.getDiagram().setPlotProvider(p);
|
||||
|
||||
diagram.getDiagram().setYoffset(-3.0f);
|
||||
diagram.getDiagram().setYscale(6.0f);
|
||||
|
||||
//diagram.getDiagram().autoScale();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue