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

Thread: wrong graphics

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default wrong graphics

    private void drawNode(double x, double y, double no, Graphics g) {
    		g.fillOval(x, y, 17, 17); // draw an oval which is filled with the current color
    		g.setColor(blue); // set current color to black
    		g.drawOval(x, y, 16, 16); // draw an oval
    		g.drawString(Integer.toString(no), x+5, y+13); // draw the node number
    }

    The code above is wrong in netbeans, it said the fillOval(), drawOval, drawString() cannot have the a double value, so how can i solve it? Please help!!
    Thank you


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: wrong graphics

    Read the API for Graphics. Those methods take int parameters. Evidently you are passing doubles instead of ints, so why are you doing that?

    db

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: wrong graphics

    Because my x and y is a double value, is it i can passed a double value into it? i want to be more accurate, so i want it to be a double value for x and y.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: wrong graphics

    The values passed to these methods relate to pixels, and pixels do not have fractions. You can fix the problem by casting the doubles to int's.

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: wrong graphics

    i want to be more accurate
    Then you'll need to cast the Graphics to Graphics2D and use the methods of the Graphics2D class, along with anti-aliasing.
    Trail: 2D Graphics (The Java™ Tutorials)

    Be aware that it could be a steep learning curve for someone at your level (no offence, we all have to start somewhere, but by that I mean someone who attempts to pass double values where the API clearly shows int parameters).

    What's so inaccurate about rounding pixel values to integers anyhow?

    db

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: wrong graphics

    Do your calculations with doubles, then cast your parameters to the graphics objects as integers. The computer can't draw pixels more accurately than integer increments.

Similar Threads

  1. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  2. Graphics mayhem
    By javapenguin in forum AWT / Java Swing
    Replies: 2
    Last Post: August 9th, 2010, 05:21 PM
  3. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM
  4. How do i use graphics with Java?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 27th, 2009, 05:16 PM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM