Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you inverse coordinates in Java2D?

Status
Not open for further replies.

Nandito

Programmer
Apr 12, 2000
31
0
0
AU
Visit site
I am trying to figure out how to develop a charting system.(for graphs)<br>eg...user puts some value...and would chart them.<br>The program runs smoothly runs good.<br>But........i think that java inverses the coordinates..<br>so that (0,0) would be on top left corner, and not on bottom left corner...is there an easy way to inverse this??
 
coordinates have always been standard on the top left corner. I'm not sure why you'd want to erase it... but here you go.<br><br>What's the bottom most coordinate? Whatever it is, that (minus the input) will give you what you want.<br><br>So, if you want (0,0) and the bottom-most coordinate is (365,0), then (0,0) becomes (365-0,0), or (365,0). If you want the top left, that would be in Java's system (0,0), but you would want it represented as (365, 0) - so all you'd need is (365-365,0) or (0,0). Just have this line at your top:<br><br><FONT FACE=monospace>static final int CONVERSION_COORD_X = 365; // or whatever else you need</font><br><br>and then for each point you make, have your point be <FONT FACE=monospace>(CONVERSION_COORD_X - x, y)</font>. Does that make sense? Hope that helps.. best of luck! <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
my bad- there's a better way to change the origin. (I just bought the O'Reilly Java Foundation Classes and hadn't had a chance to look yet, but now that I have, I've got a clue- this book rocks, I'm telling you.) This is in both the Graphics and the Graphics2D package, but since you said you're using 2D, here's the 2D example (it's identical otherwise, just don't include the letters '2D').<br><br>Graphics2D g;<br>g.translate(0, 365);<br><br>that will make the point (0, 365), or 0 pixels from the left and 365 pixels down, the new origin. Now when they say origin, I'm not sure what they mean... because with a real origin, positive y goes up while negative goes down, but I'm not sure how that's matched in Java. You'll have to check that out. But that should be much better than what I proposed earlier... hope this helps you out more. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
If you want to be able to input coordinates based on the lower left corner of the window or screen being the origin with the y positive axis pointing up, you will need a method that translates these coordinates to the one the computer uses. As Liam said, the origin that the computer uses is (0,0) in the top left corner with the y positive pointing down. That origin is always (0,0). To make things more intuitive for yourself and others, it is easier to think of the origin in the lower left as being (0,0). For example say you had a rectangle that was 10 by 10 and you wanted to plot a pixel at (5,3) relative to the lower right corner, ie the lower right corner being (0,0). The only corrdinate you would have to change is the y coordinate. because the x coordinate would remain the same for all cases. The y corrdinate is calculated by subtracting the y value (3) from the actual y value of the lower right corner (10) which yields 7. 7 is what you would feed the computer to plot the pixel where you want it.<br><br>To iterate:<br>1) You need to know the corrdinates of the lower left corner<br><br>2) you don't need to transform the x coordinate<br><br>3) the y corrdinate is claculated from the y-origin - y to plot.<br><br>Hope this helps.... <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
fenris,<br><br>like I mentioned, the origin is NOT always in the top left corner... Graphics.translate() can move it anywhere you'd like. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Does Graphics.translate() also change the orientation of the coordinate system? <br><br>The method that I talk about would not change the coordinate system, it would only map the coordinates we understand to the inverse ones.....<br><br>Here is the class that was used in my compsci course:<br><br>&nbsp;/*&nbsp;&nbsp;A class to set up a window in a world coordinate system.<br>&nbsp;&nbsp;&nbsp;The window is defined by its lower left corner (xl,yb) and its upper <br>&nbsp;&nbsp;&nbsp;right corner (xr,yt), and it is mapped to a default user coordinate system <br>&nbsp;&nbsp;&nbsp;with origin (0,0) at the top left corner and lower right corner at (w-1,y-1).<br>*/<br>&nbsp;&nbsp;&nbsp;<br>public class CoordinateSystem<br>{<br>&nbsp;&nbsp;&nbsp;private double xLeft, xRight;<br>&nbsp;&nbsp;&nbsp;private double yBottom, yTop;<br>&nbsp;&nbsp;&nbsp;private double width, height;<br>&nbsp;&nbsp;&nbsp;private double scaleX, scaleY;<br><br>&nbsp;&nbsp;&nbsp;/**<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Construct a coordinate system and mapping from a world coordinate system<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;window defined by lower left corner (x1,yb) and upper right corner (xr,yt) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to a default user space window defined by origin (0,0) at top left corner<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and lower right corner at (w-1,h-1).<br>&nbsp;&nbsp;&nbsp;*/ <br>&nbsp;&nbsp;&nbsp;public CoordinateSystem(double xl, double xr, double yb, double yt,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double w, double h)<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xLeft = xl; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xRight = xr;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yBottom = yb; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yTop = yt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width = w; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height = h;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scaleX = (width - 1.0) / (xRight - xLeft);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scaleY = (height - 1.0) / (yBottom - yTop);<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;/**<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map world coordinate x value to user space value.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@param x the world x coordinate to map<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@returns the x coordinate in user space.<br>&nbsp;&nbsp;&nbsp;*/<br>&nbsp;&nbsp;&nbsp;public double x(double x)<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (x - xLeft) * scaleX;<br>&nbsp;&nbsp;&nbsp;}<br><br><br>&nbsp;&nbsp;&nbsp;/**<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map world coordinate y value to user space value.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@param y the world y coordinate to map<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@returns the y coordinate in user space.<br>&nbsp;&nbsp;&nbsp;*/<br>&nbsp;&nbsp;&nbsp;public double y(double y)<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (y - yTop) * scaleY;<br>&nbsp;&nbsp;&nbsp;}<br>}<br><br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
That's what I was unsure about. but my first solution would take care of that in any case... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Sorry Liam, your first post was right. You basically were doing the same thing as I was. I should read things a little more carefully and not try to absorb as much information as my internet connection ;)<br><br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top