Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA problem .. (code in Two files!) Use the following file: RainbowViewer.java

ID: 3743525 • Letter: J

Question

JAVA problem .. (code in Two files!)

Use the following file:

RainbowViewer.java

Note: for a quirky reason, the viewer class must import javax.swing. rather than javaxswingJFrame. Ask me if you want an explanation. 4A In this problem you will write a Rainbow class which wl draw a rainbow at a specified y Rainbow will have construct th lakes the x y wordiridtes uf the upper left hand comer of the rainbow as well as the height uf a color Rainbow will have a draw method which takes the graphical context as a parameter and draws the rainbow. The rainbow will be displayed as colored recangles rat s. The height of actgle is given in the construtor. The width of a egle is5 times the height of a reclangle. The colors of the irs5 shuld be made with the pre-defined colors in the Color class. Use red, orange, yellow, green, and blue in that order. The next color is indigo and has RGB values 5, 0, 130). The last rectangle is violet with RGB values of (113, 0, 255). You will need to construct Color objects using these values. here arerectangles. You will compete the RainbowComponent class to construct and draw three Rainhow ohjects given these criteria Upper left-harid corner at 0,0. Height of 20 Upper left-hand corner at 100, 180. Height of 10 Upper left hand carner at , 200. Height of 5 RainbowWiawor is provided.

Explanation / Answer

/*
* The java program RainbowComponent that generates rainbow
* colors and set the colors on the component.
* */
//RainbowComponent.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class RainbowComponent extends JComponent{

   protected void paintComponent(Graphics g) {
       Graphics2D g2=(Graphics2D)g;
      
       //Set color of red
       g2.setColor(Color.red);
       g2.fillRect(0, 0, 100, 20);
      
       //Set color of orange
       g2.setColor(Color.orange);
       g2.fillRect(0, 20, 100, 20);
      
       //Set color of yellow
       g2.setColor(Color.yellow);
       g2.fillRect(0, 40, 100, 20);
      
       //Set color of green
       g2.setColor(Color.green);
       g2.fillRect(0, 60, 100, 20);
      
       //Set color of blue
       g2.setColor(Color.blue);
       g2.fillRect(0, 100, 100, 20);
      
       //Set color of indigo
       //create an instance of Color class object with RGB values
       g2.setColor(new Color(75,0,130));
       g2.fillRect(0, 120, 100, 20);
      
       //Set color of violet
       //create an instance of Color class object with RGB values
       g2.setColor(new Color(143,0,255));
       g2.fillRect(0, 140, 100, 20);
   }
}//end of the class

-----------------------------------------------------------------------------

/*
* The java program that display a frame with rainbow
* colors.
* */
//RainbowViewer.java
import javax.swing.*;
public class RainbowViewer
{
   public static void main(String[] args)
   {
       //Create an instance of frame class
       JFrame frame = new JFrame();
       //set its size width 300 and height 400
       frame.setSize(300, 400);
       //set title
       frame.setTitle("My Rainbow");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //Create an instance of RainbowComponent
       RainbowComponent component = new RainbowComponent();
       //add the instance to the frame
       frame.add(component);
       //set its visibility true
       frame.setVisible(true);
   }
}//end of the class,RainbowViewer