forked from portmobile/LAGP-Example-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrapher.java
More file actions
58 lines (52 loc) · 1.92 KB
/
Grapher.java
File metadata and controls
58 lines (52 loc) · 1.92 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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Grapher extends JPanel {
static double[] data;
int x, y, oldX, oldY;
final int PAD = 40;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xScale = (w - 2*PAD)/(data.length + 1.0d);
double maxValue = 100.0;
double yScale = (h - 2*PAD)/maxValue;
// The origin location.
int x0 = PAD;
int y0 = h-PAD;
oldX = PAD;
oldY = h-PAD;
g2.setPaint(Color.red);
for(int j = 0; j < data.length; j++) {
x = x0 + (int)(xScale * (j+1));
y = y0 - (int)(yScale * data[j]);
g2.drawLine(oldX, oldY, x, y);
oldX = x;
oldY = y;
}
}
public static float getPercentageDone(final float pSecondsElapsed, final float pDuration, final float pMinValue, final float pMaxValue) {
return (float) (pMaxValue * pSecondsElapsed / pDuration + pMinValue + 4.0f * Math.sin(Math.PI * pSecondsElapsed * 10.0f/pDuration));
}
public static void main(String[] args) {
data = new double[2000];
for (int i=0; i<2000; i++) {
data[i] = getPercentageDone(i, 2000.0f, 0.0f, 100.0f);
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new Grapher());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}