Results 1 to 3 of 3
  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Question Bulls Eye drawing

    Hi All! Writing code to draw Bulls Eye picture and did everything correct except don't know how to fix it to the frame size. Tried few variants but no luck so far. We have:


    BullsEye class:
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    
    /** A circle shape that can be placed anywhere on the screen.
     * 
     * @author FOX
     *
     */
    public class BullsEye 
    {
    	public BullsEye(int x, int y)
    	{
    		xLeft = x;
    		yTop = y;
    	}
    	public void draw(Graphics2D g2)
    	{
    		Ellipse2D.Double circle = new Ellipse2D.Double(xLeft, yTop, 300,300);
    		g2.draw(circle);
    		g2.setColor(Color.BLACK);
    		g2.fill(circle);
    		
    		Ellipse2D.Double circle2 = new Ellipse2D.Double(xLeft+25, yTop+25, 250, 250);
    		g2.draw(circle2);
    		g2.setColor(Color.WHITE);
    		g2.fill(circle2);
    		
    		Ellipse2D.Double circle3 = new Ellipse2D.Double(xLeft+50, yTop+50, 200, 200);
    		g2.draw(circle3);
    		g2.setColor(Color.BLACK);
    		g2.fill(circle3);
    		
    		Ellipse2D.Double circle4 = new Ellipse2D.Double(xLeft+75, yTop+75, 150, 150);
    		g2.draw(circle4);
    		g2.setColor(Color.WHITE);
    		g2.fill(circle4);
    		
    		Ellipse2D.Double circle5 = new Ellipse2D.Double(xLeft+100, yTop+100, 100, 100);
    		g2.draw(circle5);
    		g2.setColor(Color.BLACK);
    		g2.fill(circle5);
    		
    	}
    	private int xLeft;
    	private int yTop;
    }
    BUllsEyeComponent:
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    /** This component draws five circle shapes.
     * *
     * @author FOX
     *
     */
    
    public class BullsEyeComponent extends JComponent {
    	public void paintComponent (Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		
    	
    		
    		BullsEye eye = new BullsEye(0,0);
    	
    		
    		eye.draw(g2);
    		
    	
    	}
    	
    
    }
    BullsEyeViewer:
    
    import javax.swing.JFrame;
    
    public class BullsEyeViewer {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		JFrame frame = new JFrame();
    		
    		frame.setSize(350, 350);
    		frame.setTitle("Bulls Eye");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		BullsEyeComponent component = new BullsEyeComponent();
    		frame.add(component);
    		
    		frame.setVisible(true);
    
    	}
    
    }
    In the BullsEyeComponent class when I set explicit parameters of BullsEye class to 0(new BullsEye(0,0)) in the bottom right side of picture is not changing when you change the frame size, if I set it to getWidth() and getHeight() then top left side is not changing....So I have bad trade
    here...I just want picture to change its size in both sides according to the change of frame size when you do it with your mouse. How I can do it? Thank you guys for help!
    Last edited by Fubarable; 04-01-2012 at 05:17 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Bulls Eye drawing

    Consider:
    • Don't create a new BullsEye class in the paintComponent class. There's no need to create more than one of these objects, and in particular, you don't want to create objects unnecessarily in paintComponent, a method that must be as fast as possible.
    • Create one BullsEye class in your gui class's constructor.
    • Either give Bullseye a constructor that takes a JComponent as a parameter and pass this into it, and then in its draw(...) method have BullsEye check the size of the component, or
    • instead you could change draw to accept a Graphics object and a Dimension object, the latter holding the JComponent's current size.
    • Please consider using [code] [/code] tags when adding code to the forum.


    Good luck.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Bulls Eye drawing

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. help in drawing tex
    By Rampletero in forum Advanced Java
    Replies: 8
    Last Post: 03-10-2012, 05:17 PM
  2. need help for drawing...
    By nice7 in forum New To Java
    Replies: 4
    Last Post: 12-06-2011, 03:34 PM
  3. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 01:32 AM
  4. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •