Design and Implement an Applet That Draws 100 Circles

  1. #1

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

    Hullo everyone. I'thou having some problem with my assignment. I need to design an applet that draws five circles of random radius(between 25-100) and in random locations. Use a width of 400 and height of 300 every bit the size of the applet.

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

    Below is my lawmaking so far. Right now the output is 5 circles that descend from the top/left to lesser/right of the screen. They also increase in size one at a time. Then aye, they are not random. What can I use to ameliorate my lawmaking? Am I using the random number generator correctly here? Give thanks you lot 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 ten,y;
    int width, height;
    setBackground(Color.white);

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

    random=generator.nextInt(lxxx)+25;
    page.setColor(Color.blackness);
    ten=random;
    y=random;
    width=random;
    height=random;
    folio.drawOval(x, y, width, superlative);

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

    random=generator.nextInt(lxxx)+25;
    page.setColor(Colour.blackness);
    x=random;
    y=random;
    width=random;
    height=random;
    folio.drawOval(x, y, width, superlative);

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

    }


  2. Default Related threads:


  3. #2

    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 ten 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 concluding two that chronicle to the circle's radius. Imagine a circumvolve of radius r inside a tightly fitting square box. Information technology should be articulate that the height an width of the box are twice the radius. So to brand the circle y'all first get a randomly generated value for the radius and and so 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(Colour.black); ten=random;	 y=random; width=random; peak=random;	 page.drawOval(ten, y, width, height);

    A few things about this... Offset you only have to prepare the colour of page one time. Information technology'll stay blackness until it's set to something else.

    But you lot practise accept call nextInt() several times because the values of the position have nothing (?) to do with the value of the radius. (the radius determining the meridian and width equally above.)

    Terminate and think how you would randomly generate a radius betwixt 25 and 100. Your get-go try - nextInt(80) - is no adept considering it can exist as small as zero and your radius should be at least 25. nextInt(80)+25 is better: but that 1 can get too large! Y'all need a goldilocks condition that exactly expresses "betwixt 25 and 100". (Your question should take made information technology articulate whether or non 25 and 100 themselves are valid values. Programmers are neurotic nigh such things and the nextInt() documentation makes information technology articulate what information technology returns.) Once y'all accept that radius value you double information technology and utilize that for the top and width.

    -----

    Also I can't figure out how to size the applet to 400x300.

    This is supposed to be an applet, right? Every bit in a thing on a web page, and not a Swing application. I enquire because you lot don't usually ready the size of an applet: the web browser does that based on the html lawmaking on the page.
    Concluding edited past pbrockway2; March 20th, 2012 at 02:06 AM. Reason: typos

  4. #3

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

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

  5. #4

    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 organisation, the radius is actually one-half of the width and acme values. correct? The instructions specifically say "circumvolve" so that means the randomly generated top and width value must exist equal in social club for it to be a circumvolve. I likewise need to randomly generate a number for the x and y values in order for them to be in random locations every bit well. So for the between 25-100, generator.nextInt(75)+25;. Doesn't this hateful that it has to be twice that size? Since for instance, Width 50 and Acme fifty would equal to a radius of 25 and 25? What is a goldilocks weather? Thanks again, pb.

  6. #5

    Default Re: Help with my Java plan: Applet that draws v random circles.

    Quote Originally Posted by pbrockway2 View Post

    Stop and think how you would randomly generate a radius between 25 and 100. Your kickoff attempt - nextInt(80) - is no good because it can be as small as zero and your radius should exist at least 25. nextInt(80)+25 is better: simply that 1 can get as well big! You need a goldilocks condition that exactly expresses "betwixt 25 and 100". (Your question should take made it articulate whether or not 25 and 100 themselves are valid values. Programmers are neurotic nearly such things and the nextInt() documentation makes it clear what information technology returns.) One time you have that radius value you double it and utilize that for the acme and width.

    The instructions actually say a radius betwixt 25 and 100 inclusively, and then I assume it ways including 25 and 100?

  7. #6

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

    Ok so I fabricated some changes. It seems much better than earlier. I cannot confirm this myself though. What do you recollect?

    import javax.swing.JApplet;
    import coffee.awt.*;
    import coffee.util.Random;
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    public class Cartoon extends JApplet
    {
    public void paint(Graphics folio)
    {
    Random generator=new Random();

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

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

    It prints a bunch of interconnecting circles of different sizes.


  8. #7

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

    Sad, by "goldilocks" I just meant i that would not be besides big, nor besides pocket-size. "generator.nextInt(75)+25" looks good.

    And yes, double the radius to go the width and summit. And then if generator.nextInt(75)+25 gave a value of, say, lx then you would use 120 every bit the width and the tiptop.

    While you are testing the code - and you should be testing all the fourth dimension, 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 almost just ane circumvolve.

    But at some indicate you lot have to generate values for x and y as well. I'm assuming that the circles must lie entirely within the 400x300 expanse. To go along 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 lx units to the left of the middle. And what the largest value could exist. These ii values demand to exist used forth with nextInt() to randomly generate the value for 10. And a similar process for y.

    I'yard going off line now, but I wait forwards to seeing what you lot come up with!


  9. The Following User Says Thanks to pbrockway2 For This Useful Post:

    iDizzle (March 20th, 2012)

  10. #eight

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

    Thank you over again for your help pb. I posted a revised lawmaking just above. Let me know what you think when you get a hazard! Thanks!

  11. #ix

    gauravk is offline

    Inferior Member


    Default Re: Aid 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 coffee.util.Random;

    public class Circles extends JApplet
    {
    public void paint(Graphics chiliad)
    {
    //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(Colour.red);
    g.drawOval(startX, startY, circleRad, circleRad);
    }
    }

    What I desire to know is WHY does this generate TWO random circles instead of i? Help volition be profoundly appreciated


  12. #ten

    Default Re: Help with my Java plan: Applet that draws v random circles.

    Sorry, this may not be much assistance since I am still a beginner only where are you int declarations for startX,Y and circleRad? Other than that, I merely see one instance of a circle existence drawn and then I'yard non tooo sure =/

  13. #11

    gauravk is offline

    Junior Member


    Default Re: Help with my Java program: Applet that draws five 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 incorrect :/


  14. #12

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

    Hmmm. Accept a look at my lawmaking above and see if y'all tin make anything out of it.

  15. #thirteen

    gauravk is offline

    Junior Fellow member


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

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

  16. #xiv

    Default Re: Assist with my Java program: Applet that draws five random circles.

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

  17. #15

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

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

    Eg with your code I run into a new circle every time I cover and uncover the applet window. This may affect iDizzle's code also.

    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 demand to happen once only - that is are part of the applet'due south initialisation.


  18. #sixteen

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

    Hi lead. Practice you lot hateful if y'all elevate the window and resize information technology, information technology will recreate the random circles? Because it does practice that.

  19. #17

    gauravk is offline

    Junior Member


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

    I'm non sure I understand what you hateful. Could you delight explain what I tin can do to accept only 1 circle showing? The purpose of this program is to randomly generate l circles of random radius and positions. I tin practice that with a loop after but I demand to figure this out first.

  20. #18

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

    Do you hateful if you drag the window and resize information technology, information technology will recreate the random circles? Considering it does do that

    Yes - resize, minimise/maximise, obscure/uncover... These will all cause the applet to paint itself. Then if the code that positions and sizes the circle is in the pigment() method you will go another circle.

    Could you lot delight 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 circumvolve. The thing is you are not doing them in the right places. (b) belongs where you lot take it: in the paint() code. Merely (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 annotation of the variable that code calls buffer: specifically where information technology 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 lawmaking to randomly generate values goes in the init() or the commencement() method, simply information technology shouldn't get in paint() or you will get multiple circles.


  21. The Following User Says Thank You lot to pbrockway2 For This Useful Mail service:

    gauravk (March 27th, 2012)

  22. #nineteen

    Default Re: Assistance with my Java programme: Applet that draws 5 random circles.

    Quote Originally Posted by pbrockway2 View Post

    Yep - resize, minimise/maximise, obscure/uncover... These will all cause the applet to paint itself. And then if the code that positions and sizes the circle is in the pigment() method you will get another circle.

    The things you are doing at the moment are (more or less) right. Y'all are (a) giving startX, startY and circleRad some values and (b) using these values to describe a circumvolve. The thing is you are not doing them in the correct places. (b) belongs where you take 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 Wheel of an Applet folio in Oracle'due south Tutorial. Have special note of the variable that code calls buffer: specifically where information technology is declared, where it is initialised, and where it is used (the paint() method). For your purposes I don't recollect it matters much whether the code to randomly generate values goes in the init() or the start() method, simply it shouldn't go in paint() or y'all volition get multiple circles.

    Pb, by his (a) existence in the wrong identify do you hateful that his int variables should be declared at the very outset of the main? Lead, you lot know that I am no java expert only information technology does look odd to me. Instinctually I would declare the int variables first before giving them values below.

  23. #20

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

    Declaring a variable and giving it its beginning value all on ane line is quite common in Java. (and similar languages). In this example 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 i place and initialised (given a value) in another. Finally, information technology 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 exist visible both when information technology is given a value *and* when it is used to describe the text. Something very similar applies to the 10- y- positions and radius of the circle.


  24. #21

    gauravk is offline

    Junior Member


    Default Re: Aid 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 one circle. Thank you for the help, both of you!


craneboymor.blogspot.com

Source: http://www.javaprogrammingforums.com/whats-wrong-my-code/14691-help-my-java-program-applet-draws-5-random-circles.html

0 Response to "Design and Implement an Applet That Draws 100 Circles"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel