1 package ca.uhn.hl7v2.testpanel.ui.conn;
2
3 import static org.apache.commons.lang.StringUtils.*;
4
5 import java.awt.BorderLayout;
6 import java.awt.Color;
7 import java.awt.FontMetrics;
8 import java.awt.Graphics;
9 import java.awt.Graphics2D;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13
14 import javax.swing.JComponent;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18
19 public class JGraph extends JComponent {
20
21 private List<Integer> myValues;
22 private float myMax;
23 private Color myLineColor;
24 private String myText;
25 private Color myTextColor;
26
27 public JGraph() {
28 setBackground(new Color(0.2f, 0.2f, 0.7f));
29 myLineColor = new Color(0.9f, 0.9f, 1.0f);
30 myTextColor = new Color(0.3f, 0.6f, 1.0f);
31 }
32
33
34
35
36
37 public void setValues(List<Integer> theValues) {
38 myValues = theValues;
39 myMax = -1;
40 repaint();
41 }
42
43 public void setText(String theText) {
44 myText = theText;
45 repaint();
46 }
47
48
49
50
51
52
53 @Override
54 public void paintComponent(Graphics theG) {
55
56 Graphics2D g = (Graphics2D) getGraphics().create();
57
58 if (myValues == null || myValues.size() == 0) {
59 myValues = new ArrayList<Integer>();
60 myValues.add(0);
61 }
62
63 if (myMax <= 0) {
64 for (Integer next : myValues) {
65 myMax = Math.max(myMax, next);
66 }
67 }
68 myMax = Math.max(myMax, 1);
69
70 g.setColor(getBackground());
71 g.fillRect(0, 0, getWidth(), getHeight());
72
73 if (isNotBlank(myText)) {
74 FontMetrics metrics = g.getFontMetrics();
75 int hgt = metrics.getHeight();
76 int adv = metrics.stringWidth(myText);
77 g.setColor(myTextColor);
78 int x = (getWidth() / 2) - (adv / 2);
79 x = Math.max(0, x);
80 int y = (getHeight() / 2) - (hgt / 2);
81 y = Math.max(0, y);
82 y = getHeight() - y;
83 g.drawString(myText, x, y);
84 }
85
86 int nextLeft = 0;
87 int prevY = -1;
88 int width = getWidth() / myValues.size();
89 for (int i = 0; i < myValues.size(); i++) {
90 int nextValue = myValues.get(i);
91 float heightProp = ((float) nextValue / myMax);
92 int componentHeight = getHeight();
93 int top = (int) (componentHeight - (getHeight() * heightProp));
94
95 g.setColor(myLineColor);
96
97 if (prevY == -1) {
98 prevY = top;
99 }
100
101 g.drawLine(nextLeft, prevY, nextLeft + width, top);
102
103 nextLeft += width;
104 prevY = top;
105 }
106
107 g.dispose();
108 }
109
110 public void setValues(Integer... theValues) {
111 setValues(Arrays.asList(theValues));
112 }
113
114 public static void main(String[] args) {
115
116 JFrame jfr = new JFrame();
117 jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
118 jfr.setSize(200, 100);
119
120 JPanel cpanel = new JPanel(new BorderLayout());
121 jfr.add(cpanel);
122
123 cpanel.add(new JLabel("West"), BorderLayout.WEST);
124 cpanel.add(new JLabel("East"), BorderLayout.EAST);
125 cpanel.add(new JLabel("North"), BorderLayout.NORTH);
126 cpanel.add(new JLabel("South"), BorderLayout.SOUTH);
127
128 JGraph graph = new JGraph();
129 cpanel.add(graph);
130
131 graph.setValues(1, 2, 3);
132 graph.setText("some teXT");
133
134 jfr.setVisible(true);
135
136 }
137
138 }