- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphPanel.java
More file actions
Latest commit
109 lines (90 loc) · 3.33 KB
/
Copy pathGraphPanel.java
File metadata and controls
109 lines (90 loc) · 3.33 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
102
103
104
105
106
107
108
109
/*
Graph Panel object for Parkrun data collector
Daniel Mesham
23 January 2017
*/
importjavax.swing.*;
importjava.awt.*;
importjava.lang.Math;
classGraphPanelextendsJPanel {
protectedGraphPointorigin;
privatedoubleMAX_X = 0.0;
privatedoubleMAX_Y = 0.0;
privateintoriginX = 50;
privateintoriginY = 0;
privateintwidth = 0;
privateintheight = 0;
protectedGraphPanel(intwidthIn, intheightIn, doublemaxX, doublemaxY) {
super();
width = widthIn;
height = heightIn;
MAX_X = maxX;
MAX_Y = maxY;
setBorder(BorderFactory.createLineBorder(Color.RED, 1));
setPreferredSize(newDimension(width, height));
setBackground(Color.WHITE);
}
privatevoiddrawAxes(Graphicsg) {
originX = 50;
originY = height - 50;
intendX = width - 50;
intendY = 50;
g.drawLine(originX, originY, endX, originY); // x-axis
g.drawLine(originX, originY, originX, endY); // y-axis
// Choose maximum x & y values
MAX_X = (int) 10*(Math.ceil(MAX_X/10) + 1);
MAX_Y = (int) 10*(Math.ceil(MAX_Y/10) + 1);
// Choose tick mark intervals
intxInterval = tickInterval(MAX_X);
intyInterval = tickInterval(MAX_Y);
// Check marks & axis values
for (intx = 0; x <= MAX_X; x += xInterval) {
// x axis
g.drawLine(valToX(x), valToY(0.0), valToX(x), valToY(0.0)+5);
g.drawString(x + "", valToX(x)-4, valToY(0.0) + 20);
}
for (inty = 0; y <= MAX_Y; y += yInterval) {
// y axis
g.drawLine(valToX(0), valToY(y), valToX(0)-5, valToY(y));
g.drawString(y + "", valToX(0.0)-35, valToY(y) + 5);
}
}
@Override
publicvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
g.setColor(Color.BLACK);
drawAxes(g);
}
protectedintxCoord(GraphPointp) {
returnvalToX(p.getX());
}
protectedintyCoord(GraphPointp) {
returnvalToY(p.getY());
}
protectedintvalToX(doublexVal) {
returnoriginX + lengthToX(xVal);
}
protectedintvalToY(doubleyVal) {
returnoriginY - lengthToY(yVal);
}
/** Converts a raw data value interval size to the equivalent width on the graph */
protectedintlengthToX(doublexLength) {
return (int) Math.round((width-100)*xLength/MAX_X);
}
protectedintlengthToY(doubleyLength) {
return (int) Math.round((height-100)*yLength/MAX_Y);
}
/** Generates the interval of tick marks best suited to the given maximum axis vale */
privateinttickInterval(doublemaxVal) {
intpowInt = (int) Math.ceil(Math.log10(maxVal) - 1); // The approximate power of the tick (maxVal/10)
intinterval = (int) Math.pow(10, powInt); // The first guess at an interval
doublenumTicks = maxVal/Math.pow(10.0, powInt); // The number of ticks this interval would give
if (numTicks <= 2) { // If there are <= 2 ticks, divide interval by 5
interval = (int) interval/5;
}
elseif (numTicks <= 5) { // If there are <= 5 ticks, divide interval by 2
interval = (int) interval/2;
}
returninterval;
}
}