Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default draw method for Point2D.Double ?

    I am confused on what to do here.

    Here is my code:

    double d = generator.nextDouble() * pointSize;
    double x = generator.nextDouble() * (getWidth() - d);
    double y = generator.nextDouble() * (getHeight() - d);
    Point2D.Double cloud = new Point2D.Double(x, y);
    g2.draw(cloud);

    The error I am getting is:
    error: method draw in class Graphics2D cannot be applied to given types;
    g2.draw(cloud);//Draws clouds
    ^
    required: Shape
    found: Double
    reason: actual argument Double cannot be converted to Shape by method invocation conversion


    This is not something we have covered in class. I created the Point2D.Double object, because that is what the teacher specified, but we have never seen it before, and I can't figure out how to "call the clouds draw method"

    Any help would be greatly appreciated.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: draw method for Point2D.Double ?

    Go through the draw(...) methods in the Graphics2D API. Do you see any that can take a Point2D.Double argument?

    Then decide which of the draw(...) method overloads may actually be useful to you.

    btw how does a point represent a cloud? or is that just a bad choice of variable name?

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

  3. #3
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    Quote Originally Posted by DarrylBurke View Post
    Go through the draw(...) methods in the Graphics2D API. Do you see any that can take a Point2D.Double argument?

    Then decide which of the draw(...) method overloads may actually be useful to you.

    btw how does a point represent a cloud? or is that just a bad choice of variable name?

    db
    I have gone through the API (I find it a bit confusing though) and found nothing that refers to doubles...

    And I named it cloud...our assignment is to "create an application that displays 100 miniature clouds (Ellipses) randomly in a graphic frame." We are to use three files; CloudViewer.java, CloudComponent.java, and Cloud.java. The CloudViewer was provided, and I am working on the CloudComponent part. I find the directions kinda vague and feel pretty lost trying to figure out what to do, and it does not help when things are thrown in there that we have not covered in class. I am probably doing it all wrong lol....

    abstract void draw(Shape s)
    Strokes the outline of a Shape using the settings of the current Graphics2D context.

    void draw3DRect(int x, int y, int width, int height, boolean raised)
    Draws a 3-D highlighted outline of the specified rectangle.

    abstract void drawGlyphVector(GlyphVector g, float x, float y)
    Renders the text of the specified GlyphVector using the Graphics2D context's rendering attributes.

    abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
    Renders a BufferedImage that is filtered with a BufferedImageOp.

    abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
    Renders an image, applying a transform from image space into user space before drawing.

    abstract void drawRenderableImage(RenderableImage img, AffineTransform xform)
    Renders a RenderableImage, applying a transform from image space into user space before drawing.

    abstract void drawRenderedImage(RenderedImage img, AffineTransform xform)
    Renders a RenderedImage, applying a transform from image space into user space before drawing.

    abstract void drawString(AttributedCharacterIterator iterator, float x, float y)
    Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

    abstract void drawString(AttributedCharacterIterator iterator, int x, int y)
    Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

    abstract void drawString(String str, float x, float y)
    Renders the text specified by the specified String, using the current text attribute state in the Graphics2D context.

    abstract void drawString(String str, int x, int y)
    Renders the text of the specified String, using the current text attribute state in the Graphics2D context.
    Last edited by jwood; 07-25-2013 at 09:21 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: draw method for Point2D.Double ?

    If you need to display ellipses, then why not check out the drawOval method in the Graphics API. It should be trivial to generate random major and minor axes. Also check out Graphics2D which you may need if you want to set RenderingHints.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    Quote Originally Posted by jim829 View Post
    If you need to display ellipses, then why not check out the drawOval method in the Graphics API. It should be trivial to generate random major and minor axes. Also check out Graphics2D which you may need if you want to set RenderingHints.

    Regards,
    Jim
    Because the directions don't allow me to:
    (I do not know why indents do not show up...sorry it makes it hard to read)
    2. paintComponent method
    1. Creates a Cloud object. (See details in Cloud.java section below)
    2. Creates a Random object (from the API Random Class).
    3. Create 100 random points. To create these points, create 100 x and 100 y coordinates using the Random number generator. (need a loop here)
    1. Use the getWidth and the getHeight methods of the JComponent class. This will insure the clouds cover the complete frame when the window is resized. It is a requirement that when the window is resized the clouds cover the complete frame.
    2. Multiply the component’s width (getWidth method) (x coordinate) by a random number, use the Random number generators nextDouble method. This will insure the clouds stay in the frame.
    3. Multiple the component’s height (getHeight method) (y coordinate) value by a random number.
    4. Create a new Point2D.Double object from the x,y coordinates that were generated in the previous steps. The constructor for a Point2D is: new Point2D.Double(x, y).
    5. Add this new point to the cloud object.
    4. Once the loop is completed call the Cloud’s draw method.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: draw method for Point2D.Double ?

    I assume the 100 random points are used to place the clouds (since you you can't generate an ellipse with random points).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    i think that is right.

    here is the rest of it
    2. CloudComponent.java
    1. This class is similar to the component classes we used in chapter 3. (See the Car drawing programming) It contains a:
    1. mAn instance field – called pointSize: a int containing the size of each cloud.
    2. A constructor – takes in a parameter that is assigned to pointSize.
    3. A method, the paintComponent method.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: draw method for Point2D.Double ?

    So I also assume the CloudComponent will have to provide the code to construct the ellipse. Has your instructor provided the formula for an ellipse or are you expected to research it yourself? You indicated in an earlier post you were not allowed to use the Graphics.drawOval() method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: draw method for Point2D.Double ?

    Moved from New to Java

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

  10. #10
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    Quote Originally Posted by jim829 View Post
    So I also assume the CloudComponent will have to provide the code to construct the ellipse. Has your instructor provided the formula for an ellipse or are you expected to research it yourself? You indicated in an earlier post you were not allowed to use the Graphics.drawOval() method.

    Regards,
    Jim
    We draw the ellipse in the cloud.java class:

    3. Cloud.java
    1. The class represents a collection of clouds (ArrayList)
    2. Two instance field:
    1. An Array list of Point2D.Double objects called points that represent the X and Y coordinates of the Ellipses
    2. An int called pointSize that represents the size of the each ellipse
    3. Method
    1. A constructor that creates the empty ArrayList instance field and has a parameter that sets pointSize.
    2. An add method - takes in one parameter, a Point2D.Double object and add that object to the ArrayList.
    3. draw method public void draw(Graphics2D g2)
    1. Loops thru each point in the ArrayList
    2. Creates an Ellipse2D.Double object based on that Point object from the ArrayList. Use the instance field pointSize as the height and width of the Ellipse. Look up the Ellipse2D.Double constructor with Point2D objects as parameters in the API.
    3. Draws that Ellipse.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: draw method for Point2D.Double ?

    I don't see where you've done this:
    Quote Originally Posted by jwood View Post
    2. Creates an Ellipse2D.Double object based on that Point object from the ArrayList. Use the instance field pointSize as the height and width of the Ellipse. Look up the Ellipse2D.Double constructor with Point2D objects as parameters in the API.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: draw method for Point2D.Double ?

    Well, there is a slight disconnect in your design. Ecllipse.Double implements the Shape interface so you can use the Graphics.draw() method. But earlier in your code you used what appeared to be a Point2D.Double instance as an argument to draw(). As the error showed, it was expecting something of type Shape (which Point2D.Double has not implemented).

    I am also a tad confused about Cloud vs CloudComponent. What is the purpose of each one? Does CloudComponent subclass JComponent?

    Regards,
    Jim
    Last edited by jim829; 07-26-2013 at 08:03 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    Here are the full directions:

    1. Create an application that displays 100 miniature clouds (Ellipses) randomly in a graphic frame.
    2. Three files are needed to complete this workshop. Use the zip file in elearning called cloud.zip as your starting point.
    1. CloudViewer.java (Provided)
    1. Creates a JFrame.
    2. Set the width and height of the frame.
    3. Create a CloudComponent object and add it to the frame. (CloudComponent is discussed below)
    2. CloudComponent.java
    1. This class is similar to the component classes we used in chapter 3. (See the Car drawing programming) It contains a:
    1. mAn instance field – called pointSize: a int containing the size of each cloud.
    2. A constructor – takes in a parameter that is assigned to pointSize.
    3. A method, the paintComponent method.
    2. paintComponent method
    1. Creates a Cloud object. (See details in Cloud.java section below)
    2. Creates a Random object (from the API Random Class).
    3. Create 100 random points. To create these points, create 100 x and 100 y coordinates using the Random number generator. (need a loop here)
    1. Use the getWidth and the getHeight methods of the JComponent class. This will insure the clouds cover the complete frame when the window is resized. It is a requirement that when the window is resized the clouds cover the complete frame.
    2. Multiply the component’s width (getWidth method) (x coordinate) by a random number, use the Random number generators nextDouble method. This will insure the clouds stay in the frame.
    3. Multiple the component’s height (getHeight method) (y coordinate) value by a random number.
    4. Create a new Point2D.Double object from the x,y coordinates that were generated in the previous steps. The constructor for a Point2D is: new Point2D.Double(x, y).
    5. Add this new point to the cloud object.
    4. Once the loop is completed call the Cloud’s draw method.
    3. Cloud.java
    1. The class represents a collection of clouds (ArrayList)
    2. Two instance field:
    1. An Array list of Point2D.Double objects called points that represent the X and Y coordinates of the Ellipses
    2. An int called pointSize that represents the size of the each ellipse
    3. Method
    1. A constructor that creates the empty ArrayList instance field and has a parameter that sets pointSize.
    2. An add method - takes in one parameter, a Point2D.Double object and add that object to the ArrayList.
    3. draw method public void draw(Graphics2D g2)
    1. Loops thru each point in the ArrayList
    2. Creates an Ellipse2D.Double object based on that Point object from the ArrayList. Use the instance field pointSize as the height and width of the Ellipse. Look up the Ellipse2D.Double constructor with Point2D objects as parameters in the API.
    3. Draws that Ellipse.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: draw method for Point2D.Double ?

    Could you possibly edit your previous reply and make it look like an outline? It is difficult to parse.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    let me try attaching it, so it is easier to read....
    Attached Files Attached Files

  16. #16
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    Quote Originally Posted by jim829 View Post
    Could you possibly edit your previous reply and make it look like an outline? It is difficult to parse.

    Regards,
    Jim
    I have tried editing it multiple times using indent, and even options in the "go advanced" posts to no avail...I do not understand why , but it just won't do it for me.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: draw method for Point2D.Double ?

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

  18. #18
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    code and php tags a complete fail....even more unreadable! Ended up changing my user settings....seems to have worked....


    1. Create an application that displays 100 miniature clouds (Ellipses) randomly in a graphic frame.


    1. Three files are needed to complete this workshop. Use the zip file in elearning called cloud.zip as your starting point.
      1. CloudViewer.java (Provided)
        1. Creates a JFrame.
        2. Set the width and height of the frame.
        3. Create a CloudComponent object and add it to the frame. (CloudComponent is discussed below)

      2. CloudComponent.java
      3. This class is similar to the component classes we used in chapter 3. (See the Car drawing programming) It contains a:

    1. mAn instance field – called pointSize: a int containing the size of each cloud.

    1. A constructor – takes in a parameter that is assigned to pointSize.
    2. A method, the paintComponent method.
    3. paintComponent method
    4. Creates a Cloud object. (See details in Cloud.java section below)
    5. Creates a Random object (from the API Random Class).
    6. Create 100 random points. To create these points, create 100 x and 100 y coordinates using the Random number generator. (need a loop here)
      1. Use the getWidth and the getHeight methods of the JComponent class. This will insure the clouds cover the complete frame when the window is resized. It is a requirement that when the window is resized the clouds cover the complete frame.
      2. Multiply the component’s width (getWidth method) (x coordinate) by a random number, use the Random number generators nextDouble method. This will insure the clouds stay in the frame.
      3. Multiple the component’s height (getHeight method) (y coordinate) value by a random number.
      4. Create a new Point2D.Double object from the x,y coordinates that were generated in the previous steps. The constructor for a Point2D is: new Point2D.Double(x, y).
      5. Add this new point to the cloud object.

    7. Once the loop is completed call the Cloud’s draw method.
    8. Cloud.java
    9. The class represents a collection of clouds (ArrayList)
    10. Two instance field:
      1. An Array list of Point2D.Double objects called points that represent the X and Y coordinates of the Ellipses
      2. An int called pointSize that represents the size of the each ellipse

    11. Method
    12. A constructor that creates the empty ArrayList instance field and has a parameter that sets pointSize.
    13. An add method - takes in one parameter, a Point2D.Double object and add that object to the ArrayList.
    14. draw method public void draw(Graphics2D g2)
      1. Loops thru each point in the ArrayList
      2. Creates an Ellipse2D.Double object based on that Point object from the ArrayList. Use the instance field pointSize as the height and width of the Ellipse. Look up the Ellipse2D.Double constructor with Point2D objects as parameters in the API.
      3. Draws that Ellipse.

  19. #19
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    Here is what I have so far on my CloudComponent:
    Java Code:
    public class CloudComponent extends JComponent   {
           /**
               An integer containing the size of each cloud.
            */
          private int pointSize;
            
            /**
                 Random generator
           */
          private Random Generator;
            
            
           /**
               Constructor that takes in a parameter that is assigned to pointSize.
                @param aPointSize assigns size of each cloud
            */
          public CloudComponent(int aPointSize)
          {
             pointSize = aPointSize;
              Cloud newCloud = new Cloud(aPointSize);
          }
            
            /**
               Creates a cloud object
                @param Graphics g calls graphics components
            */
          public void paintComponent(Graphics g)
          {
                //recover Graphics2D 
             Graphics2D g2 = (Graphics2D) g;
             Random generator = new Random();
                
             for (int i = 0; i < 100; i++) 
             {
                //sets random location of points until count is reached. 
                //double d = generator.nextDouble() * pointSize;
                double x = generator.nextDouble() * (getWidth() - pointSize);
                double y = generator.nextDouble() * (getHeight() - pointSize);
                Point2D.Double points = new Point2D.Double(x, y);
                       
                          
                points.draw(g2);//Draws points 
                    newCloud.add(points);       
                }
                
            }
        }
    And here are my errors...which I just do not get why I am getting them....

    CloudComponent.java:60: error: cannot find symbol
    points.draw(g2);//Draws points
    ^
    symbol: method draw(Graphics2D)
    location: variable points of type Double
    CloudComponent.java:61: error: cannot find symbol
    newCloud.add(points);
    ^
    symbol: variable newCloud
    location: class CloudComponent

  20. #20
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: draw method for Point2D.Double ?

    I am also getting similiar errors in my cloud class....
    Java Code:
    public Cloud(int aPointSize)      {
                ArrayList<Point2D.Double> clouds = new ArrayList<Point2D.Double>();
             pointSize = aPointSize;   
              CloudComponent newCComp = new CloudComponent(aPointSize);
          }
           /**
               An add method
                @ Point2D.Double p Point2D.Double p object
           */
          public void add(Point2D.Double p)
          {
                  newCComp.paintComponent()= p; 
             clouds.add(p);   
          }

    error:
    Cloud.java:44: error: cannot find symbol
    newCComp.paintComponent()= p;
    ^
    symbol: variable newCComp
    location: class Cloud
    Cloud.java:45: error: cannot find symbol



    Am I calling the classes wrong?

    I also apologize for how it looks. Everything is lined up in jGrasp, but when I copy and paste here it gets misaligned.
    Last edited by jwood; 07-27-2013 at 07:38 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Point2D in integer Grid
    By proggrammer in forum New To Java
    Replies: 1
    Last Post: 06-15-2012, 09:39 AM
  2. How to draw 1/2 or 1/4 of a path2d.double
    By dialuppro in forum Java 2D
    Replies: 1
    Last Post: 05-08-2010, 05:54 AM
  3. Replies: 1
    Last Post: 04-06-2009, 07:45 AM
  4. Replies: 2
    Last Post: 04-03-2009, 06:04 AM
  5. Replies: 5
    Last Post: 03-21-2009, 10:32 PM

Posting Permissions

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