From 508c94c519b18a4e65e0dda419f6b96c991e4356 Mon Sep 17 00:00:00 2001 From: Eli Heuer Date: Tue, 19 Sep 2017 14:49:28 -0400 Subject: [PATCH 1/4] working on GraphicsTest --- ch04/GraphicsTest.java | 43 ++++++++++++++++++++++++++++++++++++++++++ ch04/TestClass.java | 23 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 ch04/GraphicsTest.java create mode 100644 ch04/TestClass.java diff --git a/ch04/GraphicsTest.java b/ch04/GraphicsTest.java new file mode 100644 index 0000000..db42322 --- /dev/null +++ b/ch04/GraphicsTest.java @@ -0,0 +1,43 @@ +import java.awt.Canvas; +import java.awt.Graphics; +import javax.swing.JFrame; + +/** + * Graphics Test + */ + +public class GraphicsTest extends Canvas { + + public static void main(String[] args) { + JFrame frame = new JFrame("Graphics Test"); + Canvas GraphicsTest = new GraphicsTest(); + GraphicsTest.setSize(256, 256); + frame.add(GraphicsTest); + frame.pack(); + frame.setVisible(true); + } + + public void paint(Graphics g) { + for (int i = 20; i < 256; i += 10) { + g.fillOval(i, (i/2), 32, 32); + g.fillRect((i/2), i, 32, 32); + } + } +} + + +// double root = Math.sqrt(17.0); +// double angle = 1.5; +// double height = Math.sin(angle); + +// double degrees = 90; +// double angle2 = degrees / 180.0 * Math.PI; +// double radians = Math.toRadians(180.0); +// double degrees2 = Math.toDegrees(Math.PI); +// long x = Math.round(Math.PI * 20.0); + +// double x2 = Math.cos(angle + Math.PI / 2.0); +// double x3 = Math.exp(Math.log(10.0)); +// double x4 = Math.pow(2.0, 10.0); +// System.out.println(x4); + diff --git a/ch04/TestClass.java b/ch04/TestClass.java new file mode 100644 index 0000000..eec7e1d --- /dev/null +++ b/ch04/TestClass.java @@ -0,0 +1,23 @@ +/** + * Examples from Chapter 4. + */ +public class TestClass { + + public static void main(String[] args) { + double root = Math.sqrt(17.0); + double angle = 1.5; + double height = Math.sin(angle); + + double degrees = 90; + double angle2 = degrees / 180.0 * Math.PI; + double radians = Math.toRadians(180.0); + double degrees2 = Math.toDegrees(Math.PI); + long x = Math.round(Math.PI * 20.0); + + double x2 = Math.cos(angle + Math.PI / 2.0); + double x3 = Math.exp(Math.log(10.0)); + double x4 = Math.pow(2.0, 10.0); + System.out.println(x2); + } + +} From 9a529dce85508cda1eaef8401d3333b7a317bc48 Mon Sep 17 00:00:00 2001 From: Eli Heuer Date: Tue, 19 Sep 2017 15:44:09 -0400 Subject: [PATCH 2/4] adding graphics test --- ch04/GraphicsTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ch04/GraphicsTest.java b/ch04/GraphicsTest.java index db42322..ee35bd8 100644 --- a/ch04/GraphicsTest.java +++ b/ch04/GraphicsTest.java @@ -18,9 +18,12 @@ public static void main(String[] args) { } public void paint(Graphics g) { - for (int i = 20; i < 256; i += 10) { - g.fillOval(i, (i/2), 32, 32); - g.fillRect((i/2), i, 32, 32); + double angle = 0.0; + for (int x = 0; x <= 256; x += 8) { + double y = 50 + (Math.sin(angle) * 35.0); + System.out.println("y = " + y); + g.fillRect(x, (int) y, 4, 8); + angle += Math.PI/40.0; } } } From 2f2084c3fbb5e65ac090f18a949d2257842a9b4d Mon Sep 17 00:00:00 2001 From: Eli Heuer Date: Tue, 19 Sep 2017 16:25:12 -0400 Subject: [PATCH 3/4] adding rainbow color --- ch04/GraphicsTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ch04/GraphicsTest.java b/ch04/GraphicsTest.java index ee35bd8..a764db9 100644 --- a/ch04/GraphicsTest.java +++ b/ch04/GraphicsTest.java @@ -1,5 +1,6 @@ import java.awt.Canvas; import java.awt.Graphics; +import java.awt.Color; import javax.swing.JFrame; /** @@ -19,10 +20,12 @@ public static void main(String[] args) { public void paint(Graphics g) { double angle = 0.0; - for (int x = 0; x <= 256; x += 8) { - double y = 50 + (Math.sin(angle) * 35.0); + for (int x = 0; x <= 1024; x += 8) { + double y = 150 + (Math.sin(angle) * 35.0); System.out.println("y = " + y); - g.fillRect(x, (int) y, 4, 8); + Color rainbow = new Color(x/2, 200, 200); + g.setColor(rainbow); + g.fillRect(x, (int) y, 4, 64); angle += Math.PI/40.0; } } From eb7478eb80c8de67ed789967d65a4708398dcab4 Mon Sep 17 00:00:00 2001 From: Eli Heuer Date: Tue, 19 Sep 2017 18:58:30 -0400 Subject: [PATCH 4/4] update to rainbow sin wave --- ch04/GraphicsTest.java | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ch04/GraphicsTest.java b/ch04/GraphicsTest.java index a764db9..46630b8 100644 --- a/ch04/GraphicsTest.java +++ b/ch04/GraphicsTest.java @@ -2,6 +2,8 @@ import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; +import java.util.Random; +import java.text.DecimalFormat; /** * Graphics Test @@ -13,6 +15,7 @@ public static void main(String[] args) { JFrame frame = new JFrame("Graphics Test"); Canvas GraphicsTest = new GraphicsTest(); GraphicsTest.setSize(256, 256); + GraphicsTest.setBackground(Color.BLACK); frame.add(GraphicsTest); frame.pack(); frame.setVisible(true); @@ -21,12 +24,26 @@ public static void main(String[] args) { public void paint(Graphics g) { double angle = 0.0; for (int x = 0; x <= 1024; x += 8) { - double y = 150 + (Math.sin(angle) * 35.0); - System.out.println("y = " + y); - Color rainbow = new Color(x/2, 200, 200); + + // Set y-position from sin of angle. + double y = 64 + (Math.sin(angle) * 55.0); + + // generate a random color with HSB. + Random r = new Random(); + double hue = r.nextDouble(); + float h = (float) hue; // hue + float s = (float) 1.0; // saturation + float b = (float) 1.0; // brightness + + // Set color and draw rectangle. + Color rainbow = Color.getHSBColor(h, s, b); g.setColor(rainbow); - g.fillRect(x, (int) y, 4, 64); - angle += Math.PI/40.0; + g.fillRect(x, (int)y, 5, 100); + g.fillOval(x * 2, ((int)y+200), 32, 32); + + // Update angle for next ineration. + angle += Math.PI/12.0; + } } }