1

I am working on a task which includes data such as "length" of a road in "millimetre". And as per the specification my JFrame should be 4meter * 3meter ( How to convert this into device coordinate...?).

Now, I know at least that this data is real world data and not device's pixel data. However after finding out few sources you can actually transform these data into pixel by Graphics2D class.

Resource I found these information is in,

Core Java 2, Volume II - Advanced Feature, Author - Cay S. Horstmann & Gary Cornell

However, I googled for actual examples with no luck.

Does anyone know any source where I can read more about this. And if you have any examples where someone has actually did something like this, That would be more informative.

Research so far : So far I know that we need to give a assumptions to Graphics2D class of number of pixels in meters. In Graphics2D there is method which helps you to set this assumption. Method is ,

g2D.scale(pixelsPerMeter, pixelsPerMeter);
//And than I can draw a line with meter or millimetre coordinates
g2D.draw(new Line2D.Double(coordinates in meter));

I tried above code but really hard to draw line where I actually want to.

Any help of understanding will be very helpful.

Thanks

TeaCupApp
  • 11,316
  • 18
  • 70
  • 150

4 Answers4

2

I find it easier to create an java.awt.geom.AffineTransform object and transform the coordinates from meters to pixels myself. In your case, use AffineTransform.getScaleInstance(sx, sy).

Then I call Graphics2D functions to draw my lines and polygons using pixel coordinates.

This makes debugging easier: you can see the coordinate values before and after the transformation.

The Advanced Topics section in the Java Tutorial describes how to do transformations with Graphics2D, with examples: http://download.oracle.com/javase/tutorial/2d/index.html

Simon C
  • 1,977
  • 11
  • 14
  • I like this approach, but the [inverse transform](http://stackoverflow.com/questions/2244157/reverse-java-graphics2d-scaled-and-rotated-coordinates/2244285#2244285) will likely be required. – trashgod Sep 02 '11 at 17:13
  • it's been a good history of accepting your answer TrashGod but I liked simon's approach this time. Thanks for help, However I will explore more about getNormalizingTransform(), looks interesting. – TeaCupApp Sep 03 '11 at 01:22
2

You can obtain the implicit geometry of any particular GraphicsDevice via the getNormalizingTransform() method.

You should also abstract the required model <-> view transformations. This example does so explicitly using four *scale* methods.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This [Java Map Editor](http://www.jhlabs.com/maps/index.html) may be worth a look. I haven't tried it, but I like his map projection library. – trashgod Sep 02 '11 at 17:12
  • For reference, this [example](http://stackoverflow.com/questions/2901472) demonstrates a simple, one-way linear scaling function. – trashgod Sep 02 '11 at 17:20
1

try this

Dimension d = getSize();
   int maxX = d.width - 1, maxY = d.height - 1;
   pixelSize = Math.max(rWidth/maxX, rHeight/maxY);
   centerX = maxX/2; centerY = maxY/2;


   int iX(float x){return Math.round(centerX + x/pixelSize);}//convert to device coordinate
   int iY(float y){return Math.round(centerY - y/pixelSize);}
   float fx(int x){return (x - centerX) * pixelSize;}//convert to logical coordinate
   float fy(int y){return (centerY - y) * pixelSize;}

  where rWidth and rHeight are real numbers
mf_275
  • 21
  • 4
1

I don't think it's a good idea to leave it to the UI toolkit to do all of the scaling for you. You probably won't find hardware that can actually show a 4×3 meter window, so the decision of how to scale or crop the real-world coordinates to something you actually display to your users logically belongs in your code rather than a generic UI layer. For example, if later in the project you want to reuse some generic UI elements, it might be difficult to convince them that they should try to draw a button a meter high, with 40-cm letters on it.

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73
  • Ahhh...I actually have to draw these line and make a road map out of it. So I guess I will use normal pixel values for button height. But to draw a map I have to use given data to map up exactly where they should be. I hope I made my self clear sorry. – TeaCupApp Sep 02 '11 at 16:12