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 21 of 21

Thread: Help with my Java program: Applet that draws 5 random circles.

  1. #1
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Help with my Java program: Applet that draws 5 random circles.

    Hi everyone. I'm having some trouble with my assignment. I need to design an applet that draws 5 circles of random radius(between 25-100) and in random locations. Use a width of 400 and height of 300 as the size of the applet.

    There are a few things I don't completely understand about the directions. It says a random "radius" between 25-100. the drawOval statement requires a x and y value as the argument to draw the "imaginary" rectangle in which the oval is contained. What exactly do the instructions mean when they say "radius". Also I can't figure out how to size the applet to 400x300.

    Below is my code so far. Right now the output is 5 circles that descend from the top/left to bottom/right of the screen. They also increase in size one at a time. So yes, they are not random. What can I use to improve my code? Am I using the random number generator correctly here? Thank you for your help!
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
    import javax.swing.JApplet;
    import java.awt.*;
    import java.util.Random;

    public class Drawing extends JApplet
    {
    public void paint(Graphics page)
    {
    Random generator=new Random();

    int random;
    int x,y;
    int width, height;
    setBackground(Color.white);

    random=generator.nextInt(80);
    page.setColor(Color.black);
    x=random;
    y=random;
    width=random;
    height=random;
    page.drawOval(x, y, width, height);

    random=generator.nextInt(80)+25;
    page.setColor(Color.black);
    x=random;
    y=random;
    width=random;
    height=random;
    page.drawOval(x, y, width, height);

    random=generator.nextInt(80)+25;
    page.setColor(Color.black);
    x=random;
    y=random;
    width=random;
    height=random;
    page.drawOval(x, y, width, height);

    random=generator.nextInt(80)+25;
    page.setColor(Color.black);
    x=random;
    y=random;
    width=random;
    height=random;
    page.drawOval(x, y, width, height);

    random=generator.nextInt(80)+25;
    page.setColor(Color.black);
    x=random;
    y=random;
    width=random;
    height=random;
    page.drawOval(x, y, width, height);
    }

    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    It says a random "radius" between 25-100. the drawOval statement requires a x and y value as the argument to draw the "imaginary" rectangle in which the oval is contained. What exactly do the instructions mean when they say "radius"
    drawOval() actually takes four arguments: x, y, width and height. It is the last two that relate to the circle's radius. Imagine a circle of radius r inside a tightly fitting square box. It should be clear that the height an width of the box are twice the radius. So to make the circle you first get a randomly generated value for the radius and then make the width and height arguments to drawOval() twice as big as that radius.

    The role of x and y is to determine the position of the circle.

    random=generator.nextInt(80)+25;
    page.setColor(Color.black);
    x=random;	
    y=random;
    width=random;
    height=random;	
    page.drawOval(x, y, width, height);

    A few things about this... First you only have to set the colour of page once. It'll stay black until it's set to something else.

    But you do have call nextInt() several times because the values of the position have nothing (?) to do with the value of the radius. (the radius determining the height and width as above.)

    Stop and think how you would randomly generate a radius between 25 and 100. Your first attempt - nextInt(80) - is no good because it can be as small as zero and your radius should be at least 25. nextInt(80)+25 is better: but that one can get too big! You need a goldilocks condition that exactly expresses "between 25 and 100". (Your question should have made it clear whether or not 25 and 100 themselves are valid values. Programmers are neurotic about such things and the nextInt() documentation makes it clear what it returns.) Once you have that radius value you double it and use that for the height and width.

    -----

    Also I can't figure out how to size the applet to 400x300.
    This is supposed to be an applet, right? As in a thing on a web page, and not a Swing application. I ask because you don't usually set the size of an applet: the web browser does that based on the html code on the page.
    Last edited by pbrockway2; March 20th, 2012 at 02:06 AM. Reason: typos

  3. #3
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Thanks for chiming in, pb. Let me take a moment to absorb what you said in your response.

  4. #4
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Ok I think I understand the radius part. Since the coordinate system in Java is not like the traditional cartesian system, the radius is actually half of the width and height values. correct? The instructions specifically say "circle" so that means the randomly generated height and width value must be equal in order for it to be a circle. I also need to randomly generate a number for the x and y values in order for them to be in random locations as well. So for the between 25-100, generator.nextInt(75)+25;. Doesn't this mean that it has to be twice that size? Since for example, Width 50 and Height 50 would equal to a radius of 25 and 25? What is a goldilocks conditions? Thanks again, pb.

  5. #5
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Quote Originally Posted by pbrockway2 View Post
    Stop and think how you would randomly generate a radius between 25 and 100. Your first attempt - nextInt(80) - is no good because it can be as small as zero and your radius should be at least 25. nextInt(80)+25 is better: but that one can get too big! You need a goldilocks condition that exactly expresses "between 25 and 100". (Your question should have made it clear whether or not 25 and 100 themselves are valid values. Programmers are neurotic about such things and the nextInt() documentation makes it clear what it returns.) Once you have that radius value you double it and use that for the height and width.
    The instructions actually say a radius between 25 and 100 inclusively, so I assume it means including 25 and 100?

  6. #6
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Ok so I made some changes. It seems much better than before. I cannot confirm this myself though. What do you think?

    import javax.swing.JApplet;
    import java.awt.*;
    import java.util.Random;
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    public class Drawing extends JApplet
    {
    public void paint(Graphics page)
    {
    Random generator=new Random();

    int random,randomx,randomy;
    int x,y;
    int width, height;
    setBackground(Color.white);

    random=generator.nextInt(75)+25;
    randomx=generator.nextInt(400);
    randomy=generator.nextInt(300);
    page.setColor(Color.black);
    x=randomx;
    y=randomy;
    width=random*2;
    height=random*2;
    page.drawOval(x, y, width, height);

    It prints a bunch of interconnecting circles of different sizes.

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Sorry, by "goldilocks" I just meant one that would not be too big, nor too small. "generator.nextInt(75)+25" looks good.

    And yes, double the radius to get the width and height. So if generator.nextInt(75)+25 gave a value of, say, 60 then you would use 120 as the width and the height.

    While you are testing the code - and you should be testing all the time, to catch and deal with any compiler messages - you can pretty much use any position values. And it is enough, to begin with, to worry about just one circle.

    But at some point you have to generate values for x and y as well. I'm assuming that the circles must lie entirely within the 400x300 area. To continue with the radius of 60 (width=height=120) example above, ask yourself what would be the smallest value x could have given that the circle will extend 60 units to the left of the centre. And what the largest value could be. These two values need to be used along with nextInt() to randomly generate the value for x. And a similar process for y.

    I'm going off line now, but I look forward to seeing what you come up with!

  8. The Following User Says Thank You to pbrockway2 For This Useful Post:

    iDizzle (March 20th, 2012)

  9. #8
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Thanks again for your help pb. I posted a revised code just above. Let me know what you think when you get a chance! Thanks!

  10. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Hi I could use some helps with drawing a random circle. My code is as follows:

    import java.awt.*;
    import javax.swing.JApplet;
    import java.util.Random;

    public class Circles extends JApplet
    {
    public void paint(Graphics g)
    {
    //draws a border for the circles
    g.drawRect(0, 0, 300, 225);

    Random generator = new Random();

    int startX = generator.nextInt(120);
    int startY = generator.nextInt(105);
    int circleRad = generator.nextInt(90);

    g.setColor(Color.red);
    g.drawOval(startX, startY, circleRad, circleRad);
    }
    }


    What I want to know is WHY does this generate TWO random circles instead of one? Help will be greatly appreciated

  11. #10
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Sorry, this may not be much help since I am still a beginner but where are you int declarations for startX,Y and circleRad? Other than that, I only see one instance of a circle being drawn so I'm not tooo sure =/

  12. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    int startX = generator.nextInt(120);
    int startY = generator.nextInt(105);
    int circleRad = generator.nextInt(90);

    ^those are the declarations. The 120, 105, and 90 are the maximum random integer that can occur. I really have no clue what I'm doing wrong :/

  13. #12
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Hmmm. Take a look at my code above and see if you can make anything out of it.

  14. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    This is really strange, I used your codes and removed the four other instances of circles and I STILL got 2 circles. Maybe there's a problem with the Applet viewer?

  15. #14
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Hmmm. It's possible. I wouldn't know though, sorry =/. I use Eclipse to run this stuff.

  16. #15
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Just wondering, but won't the code draw a randomly positioned and sized circle every time it repaints itself? This can happen many times over the life of a component.

    Eg with your code I see a new circle every time I cover and uncover the applet window. This may affect iDizzle's code as well.

    The solution is to consider that applets have a method to *initialise* things as well as a method to paint things. The painting happens whenever it needs to happen. But the setting of the radius and centre need to happen once only - that is are part of the applet's initialisation.

  17. #16
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Hi pb. Do you mean if you drag the window and resize it, it will recreate the random circles? Because it does do that.

  18. #17
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    I'm not sure I understand what you mean. Could you please explain what I can do to have only 1 circle showing? The purpose of this program is to randomly generate 50 circles of random radius and positions. I can do that with a loop later but I need to figure this out first.

  19. #18
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Do you mean if you drag the window and resize it, it will recreate the random circles? Because it does do that
    Yes - resize, minimise/maximise, obscure/uncover... These will all cause the applet to paint itself. So if the code that positions and sizes the circle is in the paint() method you will get another circle.

    Could you please explain what I can do to have only 1 circle showing?
    The things you are doing at the moment are (more or less) right. You are (a) giving startX, startY and circleRad some values and (b) using these values to draw a circle. The thing is you are not doing them in the right places. (b) belongs where you have it: in the paint() code. But (a) belongs in the code that initialises (prepares/gets ready etc) the applet.

    Have a look at the Life Cycle of an Applet page in Oracle's Tutorial. Take special note of the variable that code calls buffer: specifically where it is declared, where it is initialised, and where it is used (the paint() method). For your purposes I don't think it matters much whether the code to randomly generate values goes in the init() or the start() method, but it shouldn't go in paint() or you will get multiple circles.

  20. The Following User Says Thank You to pbrockway2 For This Useful Post:

    gauravk (March 27th, 2012)

  21. #19
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Quote Originally Posted by pbrockway2 View Post
    Yes - resize, minimise/maximise, obscure/uncover... These will all cause the applet to paint itself. So if the code that positions and sizes the circle is in the paint() method you will get another circle.



    The things you are doing at the moment are (more or less) right. You are (a) giving startX, startY and circleRad some values and (b) using these values to draw a circle. The thing is you are not doing them in the right places. (b) belongs where you have it: in the paint() code. But (a) belongs in the code that initialises (prepares/gets ready etc) the applet.

    Have a look at the Life Cycle of an Applet page in Oracle's Tutorial. Take special note of the variable that code calls buffer: specifically where it is declared, where it is initialised, and where it is used (the paint() method). For your purposes I don't think it matters much whether the code to randomly generate values goes in the init() or the start() method, but it shouldn't go in paint() or you will get multiple circles.
    Pb, by his (a) being in the wrong place do you mean that his int variables should be declared at the very beginning of the main? Pb, you know that I am no java expert but it does look odd to me. Instinctually I would declare the int variables first before giving them values below.

  22. #20
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Declaring a variable and giving it its first value all on one line is quite common in Java. (and similar languages). In this case not in main() - because it's an applet.

    Did you look at the Tutorial link? buffer is an example of a variable that is declared in one place and initialised (given a value) in another. Finally, it is actually used in a third place: the paint() method. It is quite important that it be declared where it is because the variable has to be visible both when it is given a value *and* when it is used to draw the text. Something very similar applies to the x- y- positions and radius of the circle.

  23. #21
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Applet that draws 5 random circles.

    Ok so turns out I'm supposed to import java.applet.Applet and extend Applet instead of JApplet since I'm drawing circles and don't need any components. I did this and my problem's fixed! Just 1 circle. Thanks for the help, both of you!

Similar Threads

  1. generate a random number from 100 to 150, inclusive in applet
    By chonch in forum Java Theory & Questions
    Replies: 12
    Last Post: February 14th, 2014, 05:44 AM
  2. Trying to make 3 images become random in an Applet. Need help
    By slahsdash in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 9th, 2012, 09:39 PM
  3. Replies: 1
    Last Post: February 10th, 2012, 07:01 AM
  4. Help with creating random circles
    By cool48 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2011, 09:18 AM
  5. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 04:34 PM