Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java Graphics help

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Java Graphics help

    Can anybody help me with this? As part of a worksheet, I have to draw a bullseye using graphics. I can draw the bullseye perfectly but its just full of repetitive code and my heads been puzzled trying to put it all into a loop. Heres the code ( remember im only learning so dont be too harsh ):

     
    import graphics.*;
    public class Q2
    {
    	public static void main(String[] args)
    	{
    		Window w = new Window(500,500);
    		Circle c = new Circle(250, 250, 200);
    		Circle c2 = new Circle(250, 250, 180);
    		w.draw(c);
    		w.draw(c2);
    		w.fill(c);
    		w.clear(c2);
    		Circle d = new Circle(250, 250, 160);
    		Circle d2 = new Circle(250, 250, 140);
    		w.draw(d);
    		w.draw(d2);
    		w.fill(d);
    		w.clear(d2);
    		Circle e = new Circle(250, 250, 120);
    		Circle e2 = new Circle(250, 250, 100);
    		w.draw(e);
    		w.draw(e2);
    		w.fill(e);
    		w.clear(e2);
    		Circle f = new Circle(250, 250, 80);
    		Circle f2 = new Circle(250, 250, 60);
    		w.draw(f);
    		w.draw(f2);
    		w.fill(f);
    		w.clear(f2);
    		Circle g = new Circle(250, 250, 40);
    		Circle g2 = new Circle(250, 250, 20);
    		w.draw(g);
    		w.draw(g2);
    		w.fill(g);
    		w.clear(g2);
    	}
    }

    basically, you have to draw a larger ring, then a smaller one, fill the large one & finally clear the small one. It HAS to be in that order. Thats why im so confused as to how to put all this into a loop. Any help is greatly appreciated, thanks


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Java Graphics help

    Well, the part that is repeated is
            Circle c = new Circle(250, 250, 200);
            Circle c2 = new Circle(250, 250, 180);
            w.draw(c);
            w.draw(c2);
            w.fill(c);
            w.clear(c2);
    and the only thing that changes throughout is the 3rd parameter so make it a variable, and then loop through decrementing it.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Java Graphics help

    Quote Originally Posted by Zula View Post
    Well, the part that is repeated is
            Circle c = new Circle(250, 250, 200);
            Circle c2 = new Circle(250, 250, 180);
            w.draw(c);
            w.draw(c2);
            w.fill(c);
            w.clear(c2);
    and the only thing that changes throughout is the 3rd parameter so make it a variable, and then loop through decrementing it.
    Window w = new Window(500, 500);
    		for(int i=200; i>=20; i-=20)
    		{
    			Circle c = new Circle(250, 250, i);
    			w.draw(c);
    			w.fill(c);
    			w.clear(c);
    		}

    If I make a loop only decrementing the 3rd parameter it creates all the rings ok but it wont fill & clear them properly. I think i need two sets of circles, with both being drawn, n every 1st circle out of the pair to be filled & and every 2nd to be cleared. Thats the only way the bullseye can be created. Will this need nested loops maybe?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Java Graphics help

    What are these draw and fill methods? Why don't you override paintComponent of a JPanel?

    If you want help, you should probably provide an SSCCE that demonstrates what you're doing.

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Java Graphics help

    Quote Originally Posted by KevinWorkman View Post
    What are these draw and fill methods? Why don't you override paintComponent of a JPanel?

    If you want help, you should probably provide an SSCCE that demonstrates what you're doing.
    Theyre part of a .jar file we received along with this worksheet. Would it help if i attached it?
    w.draw(c); draws whatever shape you have initialized to variable c.
    w.fill(c); fills this shape(default colour black)
    w.clear(c); will clear the colour

    and apologies for my ignorance but how do i provide a SSCCE?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Java Graphics help

    This is actually a link that explains what an SSCCE is, I don't know why the forum doesn't make that more obvious: SSCCE

    No, attaching the JAR probably wouldn't help. I was just curious what those methods were actually doing.

    But just looking at your code, you're not doing the equivalent work. Hint: in your original, you had two circles that are were working with each time. In the loop version, you only have one.

  7. #7
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Java Graphics help

    Still use two circles, since the repeated part has two circles.

        for(int i = 200;i>=20;i-=40) {
            Circle c = new Circle(250, 250, i);
            Circle c2 = new Circle(250, 250, i-20);
            w.draw(c);
            w.draw(c2);
            w.fill(c);
            w.clear(c2);
        }

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Java Graphics help

    Hooray spoon feeding

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Java Graphics help

    Thanks for your help with this guys.

    I have moved thread to correct forum - AWT / Java Swing - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Java Graphics help

    Quote Originally Posted by KevinWorkman View Post
    Hooray spoon feeding
    Im honestly not looking to be spoon fed i'm just new to learning java n i've found certain things difficult to get my head around. I dont know anybody in my course yet & my lecturer hasnt been too helpful with replying to student queries so i thought this would be a good place to seek help.

    Quote Originally Posted by JavaPF View Post
    Thanks for your help with this guys.

    I have moved thread to correct forum - AWT / Java Swing - Java Programming Forums
    Apologies, I didnt notice this forum.

    Thanks for the help everyone.

Similar Threads

  1. wrong graphics
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 25th, 2010, 09:33 AM
  2. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  3. Perlin Noise (and other Java 2d graphics questions)
    By helloworld922 in forum AWT / Java Swing
    Replies: 3
    Last Post: June 11th, 2010, 07:42 PM
  4. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM
  5. How do i use graphics with Java?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 27th, 2009, 04:16 PM

Tags for this Thread