BP Forums
Drawing a bullsEye to the screen. - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: Drawing a bullsEye to the screen. (/showthread.php?tid=408)



Drawing a bullsEye to the screen. - brandonio21 - 10-04-2011

This was actually a school assignment, but I thought that I'd share my code with you:
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!


Re: Drawing a bullsEye to the screen. - luke - 06-10-2012

How does this run without a
public static void main statement?


Re: Drawing a bullsEye to the screen. - brandonio21 - 06-10-2012

luke Wrote:How does this run without a
public static void main statement?
Since this is an applet, the paint() method runs when the application starts up.


Re: Drawing a bullsEye to the screen. - baconc - 03-02-2013

this is awesome, i like sifting through the code and learning how you do this stuff so i can try to make my own shapes and what not