In this problem you will write a Rainbow class which will draw a \"rainbow\" at
ID: 3743033 • Letter: I
Question
In this problem you will write a Rainbow class which will draw a "rainbow" at a specified (x, y).
Rainbow will have a construct that takes the x, y coordinates of the upper-left hand corner of the rainbow as well as the height of 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 rectangles rather than arcs. The height of a rectangle is given in the constructor. The width of a rectangle is 5 times the height of a rectangle. The colors of the first 5 should 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 (75, 0, 130). The last rectangle is violet with RGB values of (143, 0, 255). You will need to construct Color objects using these values. There are 7 rectangles.
You will complete the RainbowComponent class to construct and draw three Rainbow objects given these criteria
Upper left-hand corner at 0,0. Height of 20
Upper left-hand corner at 100, 180. Height of 10
Upper left-hand corner at 0, 200. Height of 5
RainbowViewer is provided.
Using Java language, thank you
Explanation / Answer
package 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 int x,y,height,width;
public Rainbow(int x, int y, int height)
{
this.x=x;
this.y=y;
this.height=height;
this.width=this.height*5;
}
// Draws the rainbow.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//setting up indigo and violet color using RGB
Color indigo = new Color(75, 0, 130);
Color violet = new Color(143, 0, 255);
//Setting color of first rectangle to RED
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
g.setColor(Color.ORANGE);
//as we know height of the rectangle will be either 20 10 or 5 we need to print next rectangle immediately after first so we will add height of first rectangle to y coordinate that will become our next y coordinate x AXIS will remain constant throughout
y=y+height;
g.fillRect(x,y,width,height);
g.setColor(Color.YELLOW);
y=y+height;
g.fillRect(x,y , width, height);
g.setColor(Color.GREEN);
y=y+height;
g.fillRect(x,y , width, height);
g.setColor(Color.BLUE);
y=y+height;
g.fillRect(x,y , width, height);
g.setColor(indigo);
y=y+height;
g.fillRect(x,y , width, height);
g.setColor(violet);
y=y+height;
g.fillRect(x,y , width, height);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Rainbow");
frame.setBounds(400, 400, 400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
c.add(new Rainbow(0,0,20));
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.