Skip to content

Commit eb7478e

Browse files
committed
update to rainbow sin wave
1 parent 2f2084c commit eb7478e

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

ch04/GraphicsTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import java.awt.Graphics;
33
import java.awt.Color;
44
import javax.swing.JFrame;
5+
import java.util.Random;
6+
import java.text.DecimalFormat;
57

68
/**
79
* Graphics Test
@@ -13,6 +15,7 @@ public static void main(String[] args) {
1315
JFrame frame = new JFrame("Graphics Test");
1416
Canvas GraphicsTest = new GraphicsTest();
1517
GraphicsTest.setSize(256, 256);
18+
GraphicsTest.setBackground(Color.BLACK);
1619
frame.add(GraphicsTest);
1720
frame.pack();
1821
frame.setVisible(true);
@@ -21,12 +24,26 @@ public static void main(String[] args) {
2124
public void paint(Graphics g) {
2225
double angle = 0.0;
2326
for (int x = 0; x <= 1024; x += 8) {
24-
double y = 150 + (Math.sin(angle) * 35.0);
25-
System.out.println("y = " + y);
26-
Color rainbow = new Color(x/2, 200, 200);
27+
28+
// Set y-position from sin of angle.
29+
double y = 64 + (Math.sin(angle) * 55.0);
30+
31+
// generate a random color with HSB.
32+
Random r = new Random();
33+
double hue = r.nextDouble();
34+
float h = (float) hue; // hue
35+
float s = (float) 1.0; // saturation
36+
float b = (float) 1.0; // brightness
37+
38+
// Set color and draw rectangle.
39+
Color rainbow = Color.getHSBColor(h, s, b);
2740
g.setColor(rainbow);
28-
g.fillRect(x, (int) y, 4, 64);
29-
angle += Math.PI/40.0;
41+
g.fillRect(x, (int)y, 5, 100);
42+
g.fillOval(x * 2, ((int)y+200), 32, 32);
43+
44+
// Update angle for next ineration.
45+
angle += Math.PI/12.0;
46+
3047
}
3148
}
3249
}

0 commit comments

Comments
 (0)