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

Thread: coordinate double -> pixel int

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default coordinate double -> pixel int

    Hello,

    I want to draw a Line with gDrawLine ().

    What I would like to make, it is from coordinates such as A (0,2) and B (6,9), to ask gDrawLine to draw me this right with these coordinates. However, gDrawLine use a system of pixel to represent it.

    As a result, for what I try to make, it is to create a function xToPixel, yToPixel so that the right coordinates A and B displays well in my window.

    In a way, that my Line is proportional in my JFrame

    Tracks? Thanks you


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,462
    My Mood
    Mellow
    Thanks
    87
    Thanked 481 Times in 472 Posts

    Default Re: coordinate double -> pixel int

    You're talking about Graphics.drawLine(), right? And you have a coordinate system in the JFrame that differs from that provided natively? Then I suggest you provide a translation from your coordinate system to the native coordinates and draw your lines using your values translated to JFrame's native coordinates.

    I also recommend that you not draw directly on the JFrame but on a JPanel or other JComponent placed in the JFrame so that you can override the paintComponent() method.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: coordinate double -> pixel int

    Hi, Thanks for reply.

    Yes, i want to do a translation coordinate/pixel.
    I already display them in a JComponent.

    But the probleme is that I don't know how to do this conversion ..

    I tried to do a translation of my real coordinate to pixel coordinate I'm searching a function to calcul it using xMin , xMax, the width of my JComponent etc

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,462
    My Mood
    Mellow
    Thanks
    87
    Thanked 481 Times in 472 Posts

    Default Re: coordinate double -> pixel int

    Describe your coordinate system in terms of the native coordinate system. From there, it's relatively simple math. For example, if the display area is 400 x 400, and you desire the center of the area ( 200, 200 ) to be ( 0, 0 ), that defines the translation. You might write down on paper the four corners and the center of the display area in both the native coordinates and your own to see how to translate across the entire displayed area.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: coordinate double -> pixel int

    I tried to search/try how to draw two line X absyssa /Y ordonate in (0,0) on the center of my JFrame and no success..

    I also saw the translate method but the axe of Y is like top -> bottom and not bottom -> top like an basically graph

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,462
    My Mood
    Mellow
    Thanks
    87
    Thanked 481 Times in 472 Posts

    Default Re: coordinate double -> pixel int

    I don't get the difficulty. The center of your JFrame is:

    centerX = frame.getWidth() / 2;
    centerY = frame.getHeight() / 2;

    That defines the center of the JFrame which is the origin of a coordinate system centered on the JFrame. I don't know what you're doing to make it so complicated. It's not.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: coordinate double -> pixel int

    Edited

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,462
    My Mood
    Mellow
    Thanks
    87
    Thanked 481 Times in 472 Posts

    Default Re: coordinate double -> pixel int

    That clears it up!

    I have no idea what we're supposed to do with that - either one.

Similar Threads

  1. convert double to int in ArryList
    By vector_ever in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 6th, 2013, 10:07 AM
  2. Need help converting from int to double
    By newmanj8 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 26th, 2013, 04:24 PM
  3. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 10:00 PM
  4. [SOLVED] double to int problem...
    By proxyspam in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 1st, 2012, 03:24 PM
  5. int / double validation
    By exnuke1972 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 20th, 2011, 04:08 PM