1 Replies - 11274 Views - Last Post: 17 May 2003 - 01:24 AM Rate Topic: -----

#4 marleymang  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 08-April 03

Re: Java Coordinate System

Posted 08 April 2003 - 11:47 AM

how do you change the x y coordinates in java? i'm reading off of a dat file that has the x y coordinates for a cartesian coordinate system and i was wondering how i could convert/change the coordinates to java's. thanks.

Was This Post Helpful? 1

#5 pergesu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-October 02

Re: Java Coordinate System

Posted 17 May 2003 - 01:24 AM

You'll just have to offset all the cartesian coordinates. Every component has a height and width, which you get from getHeight() and getWidth() methods. Go figure. The cartesian point (0, 0) corresponds to the very center of your screen. Midway through the width and height. So (0, 0) is equivalent to (width() / 2, height() / 2). The easiest thing to do is create two methods which return the offset for you. Why methods instead of just variables? Well once you define a variable, you can change it, but the value never gets updated unless you explicitly do it. Create methods to get the offset, and they'll return the correct values even after a component has been resized. Also, if you decide you want to add something like a 5 pixel padding, you just put it in the method and you're good to go. Here's what I'd do:

public int getXOffset()
     return (int) getWidth() / 2;

public int getYOffset()
     return (int) getHeight() / 2;

public void foo()
     // All your stuff to read the cartesian coordinates
     int x = cartX + getXOffset();
     int y = cartY + getYOffset();
     // And do whatever you want with it

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1