diff --git a/src/main/java/chapter06/bmi/Bmi.java b/src/main/java/chapter06/bmi/Bmi.java index cb191ef..7746c15 100644 --- a/src/main/java/chapter06/bmi/Bmi.java +++ b/src/main/java/chapter06/bmi/Bmi.java @@ -1,65 +1,75 @@ -package chapter06.bmi;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter06.bmi; + import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; -public class Bmi extends JFrame - implements ActionListener -{ - JTextField inputLbs, inputInches, displayBmi; +/** + * A {@link JFrame} window that calculates BMI from pounds and inches. + */ +public class Bmi extends JFrame implements ActionListener { + private JTextField massInputField; + private JTextField heightInputField; + private JTextField bmiDisplayField; - public Bmi() - { - super("BMI Calculator"); - JLabel labelLbs = new JLabel("Weight (lbs):", SwingConstants.RIGHT); - inputLbs = new JTextField(5); - JLabel labelInches = new JLabel("Height (inches):", SwingConstants.RIGHT); - inputInches = new JTextField(5); - JLabel labelBmi = new JLabel("BMI = ", SwingConstants.RIGHT); - displayBmi = new JTextField(5); - displayBmi.setEditable(false); - JButton go = new JButton("Compute"); - go.addActionListener(this); + public Bmi() { + super("BMI Calculator"); + JLabel labelLbs = new JLabel("Weight (lbs.):", SwingConstants.RIGHT); + massInputField = new JTextField(5); + JLabel labelInches = new JLabel("Height (in.):", SwingConstants.RIGHT); + heightInputField = new JTextField(5); + JLabel labelBmi = new JLabel("BMI = ", SwingConstants.RIGHT); + bmiDisplayField = new JTextField(5); + bmiDisplayField.setEditable(false); + JButton go = new JButton("Compute"); + go.addActionListener(this); - Container c = getContentPane(); - c.setBackground(Color.white); - JPanel p = new JPanel(); - p.setLayout(new GridLayout(3, 2, 5, 5)); - p.add(labelLbs); - p.add(inputLbs); - p.add(labelInches); - p.add(inputInches); - p.add(labelBmi); - p.add(displayBmi); - c.add(p, BorderLayout.CENTER); - c.add(go, BorderLayout.SOUTH); - } + Container c = getContentPane(); + c.setBackground(Color.white); + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(3, 2, 5, 5)); + panel.add(labelLbs); + panel.add(massInputField); + panel.add(labelInches); + panel.add(heightInputField); + panel.add(labelBmi); + panel.add(bmiDisplayField); + c.add(panel, BorderLayout.CENTER); + c.add(go, BorderLayout.SOUTH); + } - public void actionPerformed(ActionEvent e) - { - int lbs = Integer.parseInt(inputLbs.getText()); - int inches = Integer.parseInt(inputInches.getText()); - double bmi = calculateBmi(lbs, inches); - DecimalFormat df = new DecimalFormat("00.0"); - displayBmi.setText(df.format(bmi)); - } + /** + * Called when the "Compute" button is clicked. + * @throws NumberFormatException when non-integers are entered into the height and mass input fields. Input + * verification should be used to mitigate this. + */ + @Override + public void actionPerformed(ActionEvent e) { + int pounds = Integer.parseInt(massInputField.getText()); + int inches = Integer.parseInt(heightInputField.getText()); + double bmi = calculateBmi(pounds, inches); + DecimalFormat df = new DecimalFormat("00.0"); + bmiDisplayField.setText(df.format(bmi)); + } - // Returns BMI equal to weight in kilograms divided - // over squared height in meters. - private double calculateBmi(int lbs, int inches) - { - double kg, meters; - kg = lbs * 0.454; - meters = inches * 0.0254; - return kg/(meters * meters); - } + /** + * Calculates body mass index. + * @param pounds mass in pounds. + * @param inches height in inches. + * @return BMI in kilograms per square meter. + */ + private double calculateBmi(int pounds, int inches) { + double kilograms = pounds * 0.454; + double meters = inches * 0.0254; + return kilograms / Math.pow(meters, 2); + } - public static void main(String[] args) - { - Bmi w = new Bmi(); - w.setBounds(300, 300, 300, 160); - w.setDefaultCloseOperation(EXIT_ON_CLOSE); - w.setVisible(true); - } + public static void main(String[] args) { + Bmi w = new Bmi(); + w.setBounds(300, 300, 300, 160); + w.setDefaultCloseOperation(EXIT_ON_CLOSE); + w.setVisible(true); + } } \ No newline at end of file diff --git a/src/main/java/chapter06/dogyears/DogYearsConverter.java b/src/main/java/chapter06/dogyears/DogYearsConverter.java new file mode 100644 index 0000000..402ce91 --- /dev/null +++ b/src/main/java/chapter06/dogyears/DogYearsConverter.java @@ -0,0 +1,30 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter06.dogyears; + +import java.lang.Math; + +/** + * A class that provides a method to convert dog years to human years. + */ +public class DogYearsConverter { + private static final double DOG_TO_HUMAN_YEAR_RATE = 5.3333333333; + + /** + * Converts the given age in dog years to the equivalent age in human years. + * @param dogYears an age in dog years. + * @return the age in human years. If zero or a negative integer was passed, this returns zero. + */ + public static int convertToHumanAge(int dogYears) { + double dogAge = 0; + + if (dogYears > 0) { + dogAge += 13; + dogAge += DOG_TO_HUMAN_YEAR_RATE * (dogYears - 1); + } + return (int) Math.round(dogAge); + } + + public static void main(String[] args) { + System.out.println("The dog's age is: " + convertToHumanAge(3)); + } +} \ No newline at end of file diff --git a/src/main/java/chapter06/dogyears/TheProgram.java b/src/main/java/chapter06/dogyears/TheProgram.java deleted file mode 100644 index 87fa23e..0000000 --- a/src/main/java/chapter06/dogyears/TheProgram.java +++ /dev/null @@ -1,24 +0,0 @@ -package chapter06.dogyears;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.lang.Math; - -public class TheProgram { - - public static void main(String[] args) - { - - System.out.println("The dog's age is: " + convertToHumanAge(3)); - } - - public static int convertToHumanAge (int dogYears) - { - double dogAge = 0; - final double POST = 5.3333333333; - - if (dogYears > 0) - { - dogAge += 13; - dogAge += (POST * (dogYears - 1)); - } - return (int) Math.round(dogAge); - } -} \ No newline at end of file diff --git a/src/main/java/chapter06/piechart/Poll.java b/src/main/java/chapter06/piechart/Poll.java index aca0c83..7fd9239 100644 --- a/src/main/java/chapter06/piechart/Poll.java +++ b/src/main/java/chapter06/piechart/Poll.java @@ -1,28 +1,28 @@ -package chapter06.piechart;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter06.piechart; + import java.awt.*; import javax.swing.*; -public class Poll extends JFrame -{ - public Poll() - { - - super("Vote for Tami, Brian, or Liz"); +/** + * A {@link JFrame} window that + */ +public class Poll extends JFrame { + public Poll() { + super("Vote for Tami, Brian, or Liz"); - Container c = getContentPane(); - c.setBackground(Color.WHITE); - PollDisplayPanel chart = new PollDisplayPanel("Tami", "Brian", "Liz"); - PollControlPanel controls = new PollControlPanel(chart); - c.add(chart, BorderLayout.CENTER); - c.add(controls, BorderLayout.SOUTH); - } + Container c = getContentPane(); + c.setBackground(Color.WHITE); + PollDisplayPanel chart = new PollDisplayPanel("Tami", "Brian", "Liz"); + PollControlPanel controls = new PollControlPanel(chart); + c.add(chart, BorderLayout.CENTER); + c.add(controls, BorderLayout.SOUTH); + } - public static void main(String[] args) - { - - Poll w = new Poll(); - w.setBounds(300, 300, 400, 400); - w.setDefaultCloseOperation(EXIT_ON_CLOSE); - w.setVisible(true); - } + public static void main(String[] args) { + Poll w = new Poll(); + w.setBounds(300, 300, 400, 400); + w.setDefaultCloseOperation(EXIT_ON_CLOSE); + w.setVisible(true); + } } \ No newline at end of file diff --git a/src/main/java/chapter06/piechart/PollControlPanel.java b/src/main/java/chapter06/piechart/PollControlPanel.java index 7ccd3b2..d4207db 100644 --- a/src/main/java/chapter06/piechart/PollControlPanel.java +++ b/src/main/java/chapter06/piechart/PollControlPanel.java @@ -1,56 +1,52 @@ -package chapter06.piechart;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter06.piechart; -/* - * Implements the control panel for the chapter06.piechart.Poll program - */ import java.awt.*; import java.awt.event.*; import javax.swing.*; -public class PollControlPanel extends JPanel - implements ActionListener -{ - private JButton button1, button2, button3; - private PollDisplayPanel chartPanel; - - public PollControlPanel(PollDisplayPanel chart) - { - chartPanel = chart; - - button1 = new JButton("Tami"); - button1.setPreferredSize(new Dimension(80, 30)); - button1.setToolTipText("Vote for Tami"); - button1.addActionListener(this); - - button2 = new JButton("Brian"); - button2.setPreferredSize(new Dimension(80, 30)); - button2.setToolTipText("Vote for Brian"); - button2.addActionListener(this); - - button3 = new JButton("Liz"); - button3.setPreferredSize(new Dimension(80, 30)); - button3.setToolTipText("Vote for Liz"); - button3.addActionListener(this); - - add(button1); - add(button2); - add(button3); - } - - /** - * Processes button events - */ - public void actionPerformed(ActionEvent e) - { - JButton button = (JButton)e.getSource(); - - if (button == button1) - chartPanel.vote1(); - else if (button == button2) - chartPanel.vote2(); - else if (button == button3) - chartPanel.vote3(); - chartPanel.repaint(); - } - +/** + * Implements the control panel for the {@link chapter06.piechart.Poll} program. + */ +public class PollControlPanel extends JPanel implements ActionListener { + private JButton button1; + private JButton button2; + private JButton button3; + private PollDisplayPanel chartPanel; + + public PollControlPanel(PollDisplayPanel chart) { + chartPanel = chart; + + button1 = new JButton("Tami"); + button1.setPreferredSize(new Dimension(80, 30)); + button1.setToolTipText("Vote for Tami"); + button1.addActionListener(this); + + button2 = new JButton("Brian"); + button2.setPreferredSize(new Dimension(80, 30)); + button2.setToolTipText("Vote for Brian"); + button2.addActionListener(this); + + button3 = new JButton("Liz"); + button3.setPreferredSize(new Dimension(80, 30)); + button3.setToolTipText("Vote for Liz"); + button3.addActionListener(this); + + add(button1); + add(button2); + add(button3); + } + + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); + + if (button == button1) { + chartPanel.vote1(); + } else if (button == button2) { + chartPanel.vote2(); + } else if (button == button3) { + chartPanel.vote3(); + } + chartPanel.repaint(); + } } \ No newline at end of file diff --git a/src/main/java/chapter06/piechart/PollDisplayPanel.java b/src/main/java/chapter06/piechart/PollDisplayPanel.java index c813dd7..ddd4022 100644 --- a/src/main/java/chapter06/piechart/PollDisplayPanel.java +++ b/src/main/java/chapter06/piechart/PollDisplayPanel.java @@ -1,150 +1,158 @@ -package chapter06.piechart;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter06.piechart; + +import javax.swing.*; +import java.awt.*; + /** - * A chapter06.piechart.PollDisplayPanel holds the vote counts and - * displays the numbers and the pie chart for - * the current vote counts. + * A class that holds the vote counts and displays the numbers and the pie chart for them. */ +public class PollDisplayPanel extends JPanel { + private String name1; + private String name2; + private String name3; + private int count1; + private int count2; + private int count3; + + public PollDisplayPanel(String name1, String name2, String name3) { + setBackground(Color.WHITE); + this.name1 = name1; + this.name2 = name2; + this.name3 = name3; + count1 = 0; // These are optional as Java automatically initializes class properties to reasonable defaults + count2 = 0; // (e.g., int to 0, float to 0.0f, any Object to null). However, it is considered bad style to + count3 = 0; // rely on this. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html + } + + public void vote1() { + count1++; + } + + public void vote2() { + count2++; + } + + public void vote3() { + count3++; + } -import java.awt.Color; -import java.awt.Graphics; -import javax.swing.JPanel; -import java.math.*; - -public class PollDisplayPanel extends JPanel -{ - private String name1, name2, name3; - // Declare the int fields count1, count2, count3: - private int count1, count2, count3; - - // Constructor - public PollDisplayPanel(String nm1, String nm2, String nm3) - { - setBackground(Color.WHITE); - name1 = nm1; - name2 = nm2; - name3 = nm3; - count1 = 0; // optional - count2 = 0; // optional - count3 = 0; // optional - } - - // Increments count1 - public void vote1() - { - count1++; - } - - // Increments count2 - public void vote2() - { - count2++; - } - - // Increments count3 - public void vote3() - { - count3++; - } - - // Returns a string representation of this object - public String toString() - { - return ("Tami: " + count1 + " Brian: " + count2 + " Liz: " + count3); - } - - // Redefines JPanel's paintComponent to draw this pie chart - public void paintComponent(Graphics g) - { - super.paintComponent(g); - - int w = getWidth(); - int h = getHeight(); - int x = w/2; - int y = h/2; - int r = Math.min(w, h) / 4; - drawPieChart(g, x, y, r); - drawLegend(g, x, y, r); - } - - // Draws the pie chart. - // To avoid gaps in the picture, the following algorithm is used: - // 1. set fromDegree to 0; - // 2. draw the red sector and increment fromDegree by its size - // 3. draw the green sector and increment fromDegree by its size - // 4. set the size of the blue sector to the remaining - // area, 360 - fromDegree, but not less than 0: - // degrees = Math.max(360 - fromDegree, 0); - // (Occasionally, due to rounding errors, fromDegree may become 361, - // for example, count1 = 5, count2 = 11, count3 = 0.) - private void drawPieChart(Graphics g, int x, int y, int r) - { - int total = count1 + count2 + count3; - int fromDegree = 0; - - if (total > 0) - { - int degrees; - g.setColor(Color.RED); - degrees = countToDegrees(count1, total); - drawSector(g, x, y, r, fromDegree, degrees); - fromDegree += degrees; - - g.setColor(Color.GREEN); - degrees = countToDegrees(count2, total); - drawSector(g, x, y, r, fromDegree, degrees); - fromDegree += degrees; - - g.setColor(Color.BLUE); - degrees = Math.max(360 - fromDegree, 0); - drawSector(g, x, y, r, fromDegree, degrees); + @Override + public String toString() { + return (name1 + ": " + count1 + ", " + name2 + ": " + count2 + ", " + name3 + ": " + count3); } - else - { - g.setColor(Color.LIGHT_GRAY); - drawSector(g, x, y, r, fromDegree, 360); + + /** + * Overrides the {@link JPanel#paintComponent(Graphics) paintComponent()} method in {@link JPanel} to + * draw a pie chart. + */ + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + + int width = getWidth(); + int height = getHeight(); + int x = width / 2; + int y = height / 2; + int radius = Math.min(width, height) / 4; + drawPieChart(g, x, y, radius); + drawLegend(g, x, y, radius); + } + + /** + * Draws a pie chart. + *

+ * To avoid gaps in the picture, the following algorithm is used: + *

    + *
  1. Set {@code fromDegree} to 0.
  2. + *
  3. Draw the red sector and increment {@code fromDegree} by its size.
  4. + *
  5. Draw the green sector and increment {@code fromDegree} by its size.
  6. + *
  7. Set the size of the blue sector to the remaining area, except if it is less than zero, at which + * point we set it to zero. Occasionally, due to rounding errors, {@code fromDegree} may be set to 361 if, + * for example, count1 = 5, count2 = 11, and count3 = 0.
  8. + *
+ * + * @param g the {@link Graphics} context to be used. + * @param x TODO needs description + * @param y TODO needs description + * @param radius the radius of the pie chart to be drawn. + */ + private void drawPieChart(Graphics g, int x, int y, int radius) { + int total = count1 + count2 + count3; + int fromDegree = 0; + + if (total > 0) { + int degrees; + g.setColor(Color.RED); + degrees = countToDegrees(count1, total); + drawSector(g, x, y, radius, fromDegree, degrees); + fromDegree += degrees; + + g.setColor(Color.GREEN); + degrees = countToDegrees(count2, total); + drawSector(g, x, y, radius, fromDegree, degrees); + fromDegree += degrees; + + g.setColor(Color.BLUE); + degrees = Math.max(360 - fromDegree, 0); + drawSector(g, x, y, radius, fromDegree, degrees); + } else { + g.setColor(Color.LIGHT_GRAY); + drawSector(g, x, y, radius, fromDegree, 360); + } + } + + /** + * Draws a legend containing vote counts and their corresponding colored squares. + * @param g the {@link Graphics} context to be used. + * @param x TODO + * @param y TODO + * @param r TODO + */ + private void drawLegend(Graphics g, int x, int y, int r) { + // Display the counts: + y += (r + 20); + g.setColor(Color.BLACK); + g.drawString(name1 + ": " + count1, x - r, y); + g.drawString(name2 + ": " + count2, x, y); + g.drawString(name3 + ": " + count3, x + r, y); + + // Display the color squares: + y += 5; + x -= 2; + g.setColor(Color.RED); + g.fillRect(x - r, y, 10, 10); + g.setColor(Color.GREEN); + g.fillRect(x, y, 10, 10); + g.setColor(Color.BLUE); + g.fillRect(x + r, y, 10, 10); + } + + /** + * Converts a given proportion to degrees. + * @param count the part of the total. + * @param total the total. + * @return the proportion in degrees. + */ + private int countToDegrees(int count, int total) { + return (int) ((double) count / total * 360); + } + + /** + * Draws a sector of a circle. + * + * @param g the {@link Graphics} context to be used. + * @param x the x-coordinate of the center of the circle. + * @param y the y-coordinate of the center of the circle. + * @param radius the radius of the circle. + * @param fromDegree the starting angle of the sector. + * @param degrees the size of the sector in degrees. + */ + private void drawSector(Graphics g, int x, int y, int radius, int fromDegree, int degrees) { + if (degrees > 359) { + g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); + } else { + g.fillArc(x - radius, y - radius, 2 * radius, 2 * radius, fromDegree, degrees); + } } - } - - // Draws the vote counts and the corresponding color squares. - private void drawLegend(Graphics g, int x, int y, int r) - { - // Display the counts: - y += (r + 20); - g.setColor(Color.BLACK); - - g.drawString(name1 + ": " + count1, x - r, y); - - g.drawString(name2 + ": " + count2, x, y); - - g.drawString(name3 + ": " + count3, x + r, y); - - - // Display the color squares: - y += 5; - x -= 2; - g.setColor(Color.RED); - g.fillRect(x - r, y, 10, 10); - g.setColor(Color.GREEN); - g.fillRect(x, y, 10, 10); - g.setColor(Color.BLUE); - g.fillRect(x + r, y, 10, 10); - } - - // Returns the number of degrees in a pie slice that - // corresponds to count / total, rounded to the nearest integer. - private int countToDegrees(int count, int total) - { - return (int) ((double) count / total * 360); - } - - - // Draws a sector, centered at x, y, of radius r, - // of angle measure degrees, starting at fromDegree. - private void drawSector(Graphics g, int x, int y, int r, int fromDegree, int degrees) - { - if (degrees > 359) - g.fillOval(x - r, y - r, 2 * r, 2 * r); - else - g.fillArc(x - r, y - r, 2 * r, 2 * r, fromDegree, degrees); - } } diff --git a/src/main/java/chapter06/rainbow/Rainbow.java b/src/main/java/chapter06/rainbow/Rainbow.java index bb4809a..bb395f1 100644 --- a/src/main/java/chapter06/rainbow/Rainbow.java +++ b/src/main/java/chapter06/rainbow/Rainbow.java @@ -1,64 +1,58 @@ -package chapter06.rainbow;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter06.rainbow; + import java.awt.Color; import java.awt.Graphics; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JPanel; -public class Rainbow extends JPanel -{ - private final Color skyColor = Color.CYAN; - - public Rainbow() - { - setBackground(skyColor); - } - - // Draws the rainbow. - public void paintComponent(Graphics g) - { - super.paintComponent(g); - int width = getWidth(); - int height = getHeight(); - int xCenter = width/2, yCenter = (3*height)/4; - - int largeRadius = width / 4; - int smallRadius = height / 4; - int mediumRadius = (int)Math.sqrt(smallRadius * largeRadius); - int skyRadius = largeRadius - 3 * mediumRadius + 3 * smallRadius; - - g.setColor(Color.RED); - g.fillArc(xCenter - largeRadius, yCenter - largeRadius, largeRadius * 2, largeRadius * 2, 0, 180); - - g.setColor(Color.GREEN); - g.fillArc( xCenter - mediumRadius, yCenter - mediumRadius, mediumRadius * 2, mediumRadius * 2, 0, 180 ); - - g.setColor(Color.MAGENTA); - g.fillArc( xCenter - smallRadius, yCenter - smallRadius, smallRadius * 2, smallRadius * 2, 0, 180 ); - - g.setColor(Color.CYAN); - g.fillArc( xCenter - skyRadius, yCenter - skyRadius, skyRadius * 2, skyRadius * 2, 0, 180 ); - - // Declare and initialize the radii of the small and medium - // semicircles and draw them: - // ________________________________________________ - - // Calculate the radius of the innermost (sky-color) semicircle - // so that the width of the middle (green) ring is the - // arithmetic mean of the widths of the red and magenta rings: - // ________________________________________________ - - // Draw the sky-color semicircle: - // ________________________________________________ - } - - public static void main(String[] args) - { - JFrame w = new JFrame("chapter06.rainbow.Rainbow"); - w.setBounds(300, 300, 300, 200); - w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Container c = w.getContentPane(); - c.add(new Rainbow()); - w.setVisible(true); - } +/** + * A {@link JPanel} with a rainbow on it. + */ +public class Rainbow extends JPanel { + private static final Color SKY_COLOR = Color.CYAN; + + public Rainbow() { + setBackground(SKY_COLOR); + } + + /** + * Overrides the {@link JPanel#paintComponent(Graphics) paintComponent()} method in {@link JPanel} to + * draw a rainbow. + */ + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + int width = getWidth(); + int height = getHeight(); + int xCenter = width / 2; + int yCenter = (3 * height) / 4; + + int largeRadius = width / 4; + int smallRadius = height / 4; + int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius); + int skyRadius = largeRadius - 3 * mediumRadius + 3 * smallRadius; + + g.setColor(Color.RED); + g.fillArc(xCenter - largeRadius, yCenter - largeRadius, largeRadius * 2, largeRadius * 2, 0, 180); + + g.setColor(Color.GREEN); + g.fillArc(xCenter - mediumRadius, yCenter - mediumRadius, mediumRadius * 2, mediumRadius * 2, 0, 180); + + g.setColor(Color.MAGENTA); + g.fillArc(xCenter - smallRadius, yCenter - smallRadius, smallRadius * 2, smallRadius * 2, 0, 180); + + g.setColor(Color.CYAN); + g.fillArc(xCenter - skyRadius, yCenter - skyRadius, skyRadius * 2, skyRadius * 2, 0, 180); + } + + public static void main(String[] args) { + JFrame w = new JFrame("chapter06.rainbow.Rainbow"); + w.setBounds(300, 300, 300, 200); + w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + Container c = w.getContentPane(); + c.add(new Rainbow()); + w.setVisible(true); + } } \ No newline at end of file