-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToneDisplay.java
More file actions
84 lines (62 loc) · 2.73 KB
/
ToneDisplay.java
File metadata and controls
84 lines (62 loc) · 2.73 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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
public class ToneDisplay extends JComponent {
BufferedImage clefImage;
@Override
public void paintComponent(Graphics g) {
// Cast Graphics2D object from method argument
Graphics2D g2 = (Graphics2D) g;
// Allow G2 to render with AntiAliasing (makes things smoother)
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHints(rh);
// get the needed information from ToneGenerator
double lowestFreq = ToneGenerator.getFrequencyOfTone(ToneGenerator.lowestTone);
double highestFreq = ToneGenerator.getFrequencyOfTone(ToneGenerator.highestTone);
double frequency = ToneGenerator.currentFrequency;
double volume = ToneGenerator.currentVolume;
int heightOfDrawArea = this.getHeight() - 50;
// Paint background
GradientPaint blackToGray = new GradientPaint(0, 0, Color.WHITE, 0, 500, Color.LIGHT_GRAY);
g2.setPaint(blackToGray);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
// Paint the Tone lines
int lineWidth = this.getWidth() - 60;
Stroke stroke = new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
g2.setStroke(stroke);
g2.setColor(Color.DARK_GRAY);
// Get all 88 freqs...
for (String note : ToneBean.toneMap.keySet()) {
double freqForNote = ToneBean.toneMap.get(note);
// ...But only draw the freqs that are between the highest / lowest tones
if (freqForNote + 1 > lowestFreq && freqForNote - 1 < highestFreq) {
// Determine the correct Y position to draw each line
double percentageOfTotalHeight = ((freqForNote - lowestFreq) / (highestFreq - lowestFreq));
double yOffset = (1 - percentageOfTotalHeight);
double yPos = yOffset * heightOfDrawArea + 30;
g2.drawString(Double.toString(freqForNote), 10, (int) yPos - 8);
g2.drawLine(70, (int) yPos - 8, lineWidth, (int) yPos - 8);
g2.drawString(note, lineWidth, (int) yPos - 8);
}
}
// Draw slider frequency. Line thickness is determines by volume
g2.setColor(Color.decode("#2FB576"));
double thickness = 2 + volume * 10;
stroke = new BasicStroke((float) thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
g2.setStroke(stroke);
int locationOfBaseNote = heightOfDrawArea + 23;
double offsetMultiplier = ((frequency - lowestFreq) / (highestFreq - lowestFreq));
int yPos = locationOfBaseNote - (int) (offsetMultiplier * heightOfDrawArea);
g2.drawLine(60, yPos, lineWidth, yPos);
}
}