10-04-2011, 07:39 PM
This was actually a school assignment, but I thought that I'd share my code with you:
This code draws a bullseye to the screen at the location of 400,400, and grows outward. The number of rings can be edited.
[attachment=0]<!-- ia0 -->javaBullseye.png<!-- ia0 -->[/attachment]
Have fun!
Code:
//Exercise P4.6 - by Brandon Milton
/*
* This bullseye code is made by Brandon Milton, known as brandonio21 on BP Forums
* This exercise is directly from Cay Horstman's: Computing Concepts with Java Essentials
*
* Commenting is for better understanding
*
* Feel free to use
* http://brandonsoft.com
* http://bpforums.info
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.applet.Applet;
import java.awt.geom.Ellipse2D;
public class bullsEye extends Applet { //Tell Java that this is indeed an applet
public void paint(Graphics g){ //This is what java needs to actually draw something
Graphics2D g2 = (Graphics2D)g; //Tell java to use the newer graphics2d
int rings = 8; //THIS CAN BE EDITED - This is the number of rings that the bullseye itself has. default is '8'
int i = rings*50; //multiply the number of rings by 50, to get the largest rings width
while (i > 0){ //As long as the width is not 0,
Ellipse2D.Double elip = new Ellipse2D.Double(400 - (i/2),400 - (i/2),i,i); //Create a new circle, it's position is in the center of the previous ring, and it's size is the previous rings - 50
if ((i/50 %2) == 0){ //If it is an even ring
g2.setColor(Color.red);
g2.draw(elip);
g2.fill(elip);
} else{ //It is an odd ring
g2.setColor(Color.white);
g2.draw(elip);
g2.fill(elip);
}
i = i -50; //Decrease the size, and draw another ring
}
}
}
This code draws a bullseye to the screen at the location of 400,400, and grows outward. The number of rings can be edited.
[attachment=0]<!-- ia0 -->javaBullseye.png<!-- ia0 -->[/attachment]
Have fun!