summaryrefslogtreecommitdiff
path: root/projects/project1_frequencyBag/FrequencyGraphComponent.java
blob: 3d48c746281e119ed0d88a445e0e8f28eaaccfd3 (plain)
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JComponent;

@SuppressWarnings("serial")
public class FrequencyGraphComponent extends JComponent
{
	private FrequencyBag<Integer> fb;
	private int min;
	private int max;
	private int delta;
	private int width;
	private int height;
	private int leftMargin = 10;
	private int rightMargin = 10;
	private int topMargin = 10;
	private int bottomMargin = 10;
	
	public FrequencyGraphComponent(FrequencyBag<Integer> afb, int aMin, int aMax, int aDelta)
	{
		fb = afb;
		min = aMin;
		max = aMax;
		delta = aDelta;
	}

	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		
		g2.setColor(Color.BLACK);
		
		width = this.getWidth();
		height = this.getHeight();

		int maxFrequency = fb.getMaxFrequency();
		double deltaWidth = (double) (width - (leftMargin + rightMargin)) / ((max - min) / delta);
		double deltaHeight = (double) (height - (topMargin + bottomMargin)) / maxFrequency;
		
		Line2D.Double line = new Line2D.Double(0,0,0,0);
		
		for(int i = min; i < max; i = i + delta)
		{
			double sx = ((i - min) * deltaWidth) + leftMargin;
			double ex = (((i + delta) - min) * deltaWidth) + leftMargin;
			double sy = (height - bottomMargin) - (fb.getFrequencyOf(i) * deltaHeight);
			double ey = (height - bottomMargin) - (fb.getFrequencyOf(i + delta) * deltaHeight);
			line.setLine(sx,sy,ex,ey);
			g2.draw(line);
		}
		
		g2.setColor(Color.GREEN);
		double accProp = 0.0;
			
		for(int i = min; i < max; i = i + delta)
		{
			accProp = accProp + fb.getProbabilityOf(i);
			double nextProp = fb.getProbabilityOf(i + delta);
				
			double sx = ((i - min) * deltaWidth) + leftMargin;
			double ex = (((i + delta) - min) * deltaWidth) + leftMargin;
			double sy = (height - bottomMargin) * (1 - accProp);
			double ey = (height - bottomMargin) * (1 - (accProp + nextProp));
			line.setLine(sx,sy,ex,ey);
			g2.draw(line);
		}
	}
}