CS 121 Lecture 36: Drawing With Loops & Conditionals
Document Summary
Conditionals & loops greatly enhance our ability to generate interesting graphics: ex: the bullseye program draws a target. The drawing actually occurs in the bullseyepanel class uses an if statement to alternate the colors between black & white. Each ring is actually drawn as a filled circle (an oval of equal width & length). B/c we draw the circles on top of each other, the inner circles cover the inner part of the larger circles, creating a ring effect. At the end, a final red circle is drawn for the bull"s-eye. // creates and displays the program frame. import javax. swing. jframe; public class bullseye public static void main(string[] args) Jframe frame = new jframe(bullseye); frame. getcontentpane(). add(new bullseyepanel()); frame. setdefaultcloseoperation(jframe. exit_on_close); frame. pack(); frame. setvisible(true); import javax. swing. jpanel; import java. awt. public class bullseyepanel extends jpanel { private final int max_width = 300, num_rings = 5, // sets up the bullseye panel. public bullseyepanel () setbackground (color. cyan); setpreferredsize (new dimension(300,300));