Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drawing a bullsEye to the screen.
#1
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!


Attached Files Thumbnail(s)
   
My Blog | My Setup | My Videos | Have a wonderful day.
#2
How does this run without a
public static void main statement?
#3
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.
My Blog | My Setup | My Videos | Have a wonderful day.
#4
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Drawing 3 simple houses. brandonio21 4 16,949 11-02-2011, 03:42 PM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)