-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphBonds.java
More file actions
103 lines (85 loc) · 2.86 KB
/
graphBonds.java
File metadata and controls
103 lines (85 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package textoSecreto;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
/**
*
* @author danielsalnikov
*/
public class graphBonds extends JPanel {
private double [] values;
private String [] names;
private String title;
Color colors []= new Color []{Color.BLUE,Color.CYAN,Color.GREEN,Color.MAGENTA,Color.YELLOW,Color.ORANGE};
public graphBonds(double[] d, String [] n, String t) {
this.values = d;
this.names = n;
this.title = t;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (values == null || values.length == 0) {
return;
}
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
if (minValue > values[i]) {
minValue = values[i];
}
if (maxValue < values[i]) {
maxValue = values[i];
}
}
Dimension dim = getSize();
int panelWidth = dim.width;
int panelHeight = dim.height;
int barWidth = panelWidth / values.length;
Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("Book Antiqua", Font.PLAIN, 14);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int stringHeight = titleFontMetrics.getAscent();
int stringWidth = (panelWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, stringWidth, stringHeight);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue) {
return;
}
double scale = (panelHeight - top - bottom) / (maxValue - minValue);
stringHeight = panelHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);
for (int j = 0; j < values.length; j++) {
int valueP = j * barWidth + 1;
int valueQ = top;
int height = (int) (values[j] * scale);
if (values[j] >= 0) {
valueQ += (int) ((maxValue - values[j]) * scale);
} else {
valueQ += (int) (maxValue * scale);
height = -height;
}
int a=(int)(Math.random()*5+1);
g.setColor(colors[a]);
g.fillRect(valueP, valueQ, barWidth - 2, height);
g.setColor(Color.black);
g.drawRect(valueP, valueQ, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[j]);
stringWidth = j * barWidth + (barWidth - labelWidth) / 2;
g.drawString(names[j],stringWidth, stringHeight);
}
}
}