to create a Rainbow using java Graphics (fillArc)


Program: 

import java.awt.*; import java.applet.*; public class Rainbow extends Applet { public void paint(Graphics g) { int x = 10; int y = 10; int w = getWidth() - 20; int h = getHeight() - 20; g.setColor(Color.RED); g.fillArc(x, y, w, h, 0, 180); y += h / 7; g.setColor(Color.ORANGE); g.fillArc(x, y, w, h, 0, 180); y += h / 7; g.setColor(Color.YELLOW); g.fillArc(x, y, w, h, 0, 180); y += h / 7; g.setColor(Color.GREEN); g.fillArc(x, y, w, h, 0, 180); y += h / 7; g.setColor(Color.BLUE); g.fillArc(x, y, w, h, 0, 180); y += h / 7; g.setColor(new Color(75, 0, 130)); g.fillArc(x, y, w, h, 0, 180); y += h / 7; g.setColor(new Color(128, 0, 128)); g.fillArc(x, y, w, h, 0, 180); } }

output:-






This code defines an applet named "Rainbow" that draws a rainbow with seven colors using arcs. The colors used are red, orange, yellow, green, blue, indigo, and violet, in that order. The applet window size is determined by the "getWidth()" and "getHeight()" methods, which return the width and height of the applet window, respectively.

The "paint()" method is where the actual drawing of the rainbow occurs. The method starts by setting the initial values of x, y, w, and h, which are used to draw the first arc. The value of y is then incremented by h / 7 to produce the y-coordinate for the next arc, and so on.

Each arc is drawn using the "fillArc()" method of the Graphics object, which takes as parameters the x and y coordinates of the upper-left corner of the arc's bounding rectangle, the width and height of the bounding rectangle, and the starting and ending angles of the arc.

Finally, each arc is filled with a different color using the "setColor()" method of the Graphics object, which takes as a parameter an instance of the Color class representing the desired color.

Overall, this code produces a colorful applet that demonstrates the use of colors, arcs, and arithmetic in Java graphics programming.

Comments