filling a hexagon(very basic!)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: filling a hexagon(very basic!)

  1. #1
    Join Date
    Jun 2004
    Posts
    3

    filling a hexagon(very basic!)

    Hey guys, I started learning Java a couple days ago, and my teacher gave me a assignment to make this hexagon that will stretch with the applet and such. I know that int's wont hold decimals but the doubles wont work either for some reason. Now this is the code I am using and the errors it gives are below:
    public void paint(Graphics g) {
    Graphics2D g2= (Graphics2D)g;
    Polygon myPoly = new Polygon();
    int a=(getWidth()/2);
    int b=(int)a-10*Math.sqrt(3);
    myPoly.addPoint(a,0);
    myPoly.addPoint(b,10);
    myPoly.addPoint(b,30);
    g2.fill(myPoly);

    and it gives me:
    Possible loss of precision on the
    int b=(int)a-10*Math.sqrt(3); line
    please help me out, thanks in advanced

  2. #2
    Join Date
    Jun 2004
    Posts
    3
    scratch that, this is the new code im using:
    Graphics2D g2= (Graphics2D)g;
    Polygon myPoly = new Polygon();
    double a=(getWidth()/2);
    double b=a-10*Math.sqrt(3);
    double c=(getHeight());
    double d=a+10*Math.sqrt(3);
    myPoly.addPoint(a,0);
    myPoly.addPoint(b,10);
    myPoly.addPoint(b,c-10);
    myPoly.addPoint(a,c);
    myPoly.addPoint(d,c-10);
    myPoly.addPoint(d,10);
    g2.fill(myPoly);
    And the Errors:
    addpoint(int,int) in java.awt.Polygon cannot be applied to (double,int)
    and it goes on like that for each point i plotted, what am I doing wrong?

  3. #3
    Join Date
    May 2004
    Posts
    219
    addpoint(int,int) in java.awt.Polygon cannot be applied to (double,int)
    So, that means that the method addpoint requires two integers to be passed to it, and you are giving it a double and an integer, so it can't deal with it.

    Instead of making doubles, you should cast them to integers, or perhaps round them off.

    The reason this:
    Code:
    int b=(int)a-10*Math.sqrt(3);
    didn't work was because you only casted the a variable to an int, and not the whole thing. Try using parentheses.

    Edit to also note: There are no fractional pixels, so that's why using doubles makes no sense

  4. #4
    Join Date
    Jun 2004
    Posts
    3
    thanks a bunch man, that fixed a lot hah. Never thought i needed parenthesees to make the whole line casted into an int!

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    parenthesis are the standard indicator of casting. you should additionally include other parenthesis to indicate what should be cast..

    int b=(int)(a)-10*Math.sqrt(3);
    int b=(int)(a-10)*Math.sqrt(3);
    int b=(int)(a-10*Math.sqrt(3));

    are all different. if possible, use many parenthesis and spaces, to better outline what will be calculated etc:

    int b= ( (int)(a-10*Math.sqrt(3)) ) +20;
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center