diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
| commit | 89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch) | |
| tree | cdc0fd8165e65b1637fa54cac11c932acefc8a89 /projects/project1_frequencyBag/FrequencyGraphComponent.java | |
| download | coe0445-master.tar.gz coe0445-master.tar.bz2 coe0445-master.zip | |
Diffstat (limited to 'projects/project1_frequencyBag/FrequencyGraphComponent.java')
| -rw-r--r-- | projects/project1_frequencyBag/FrequencyGraphComponent.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/projects/project1_frequencyBag/FrequencyGraphComponent.java b/projects/project1_frequencyBag/FrequencyGraphComponent.java new file mode 100644 index 0000000..3d48c74 --- /dev/null +++ b/projects/project1_frequencyBag/FrequencyGraphComponent.java @@ -0,0 +1,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); + } + } +} |
