From 2426ee78f0823062e2cf015d6c076fec10189f02 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 28 Feb 2019 18:58:45 -0500 Subject: [PATCH] Created a program that will draw a rainbow in a popup window --- DrawRainbow.java | 45 ++++++++++++++++++++++++++++++++++++++++++++ DrawRainbowTest.java | 21 +++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 DrawRainbow.java create mode 100644 DrawRainbowTest.java diff --git a/DrawRainbow.java b/DrawRainbow.java new file mode 100644 index 0000000..085c1cb --- /dev/null +++ b/DrawRainbow.java @@ -0,0 +1,45 @@ +//Java/JavaTutorials/DrawRainbow.java +//Matthew Ellison +// Created: 02-28-19 +//Modified: 02-28-19 +//This program draws a rainbow on a panel + + +import java.awt.Color; +import java.awt.Graphics; +import javax.swing.JPanel; + + +public class DrawRainbow extends JPanel{ + //Define some extra colors + private static final Color VIOLET = new Color(128, 0, 128); + private static final Color INDIGO = new Color(75, 0, 130); + private static final Color BACKGROUND_COLOR = Color.WHITE; + + //For the colors used in the rainbow. The two background colors represent a blank space + private static final Color[] colors = {BACKGROUND_COLOR, BACKGROUND_COLOR, VIOLET, INDIGO, Color.BLUE, Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED}; + + public DrawRainbow(){ + setBackground(BACKGROUND_COLOR); + } + + //Draws a rainbow using concentric arcs + public void paintComponent(Graphics g){ + super.paintComponent(g); + + int radius = 100; //The radius of an arc + + //Draw the rainbow near the bottom-center + int centerX = getWidth() / 2; + int centerY = getHeight() - 10; + + //Draws filled arcs starting with the outermost + for(int cnt = colors.length;cnt > 0;--cnt){ + //Set the color for the current arc + g.setColor(colors[cnt - 1]); + + //Fill the arc from 0 to 180 degrees + g.fillArc(centerX - cnt * radius, centerY - cnt * radius, cnt * radius * 2, cnt * radius * 2, 0, 180); + } + } +} \ No newline at end of file diff --git a/DrawRainbowTest.java b/DrawRainbowTest.java new file mode 100644 index 0000000..de7e07b --- /dev/null +++ b/DrawRainbowTest.java @@ -0,0 +1,21 @@ +//Java/JavaTutorials/DrawRainbowTest.java +//Matthew Ellison +// Created: 02-28-19 +//Modified: 02-28-19 +//This program runs a test of DrawRainbow + + +import javax.swing.JFrame; + + +public class DrawRainbowTest{ + public static void main(String[] argv){ + DrawRainbow panel = new DrawRainbow(); + JFrame application = new JFrame(); + + application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + application.add(panel); + application.setSize(1920, 1080); + application.setVisible(true); + } +} \ No newline at end of file