Implement Viewports and Windows using AffineTransforms ?

Discussion in 'Java' started by Richard A. DeVenezia, Jun 7, 2004.

1. Richard A. DeVeneziaGuest

0,0 1000,0
+-------------------------+
| |
| .75,.75 |
| +-----------+ |
| | 100,100| |
| | | |
| | | |
| | | |
| |20,20 | |
| +-----------+ |
| .25,.25 |
| |
+-------------------------+
0,1000 1000,1000

I am trying to handle Viewports and Windows using multiple AffineTranforms

w=1000 // width of image in pixels
h=1000 // height of image in pixels

// viewport is specified as fraction of image
vllx = .25 // viewport lower left
vlly = .25
yurx = .75 // viewport upper right
yury = .75

// window is specified in world coordinates
wllx = 20 // window lower left
wlly = 20
wury = 100 // window upper right
wury = 100

*** transforms here ***

// Draw three circles centered at world coordinate 60,60 with radii 10,20,30
g.drawOval(60-10,60-10,20,20);
g.drawOval(60-20,60-20,40,40);
g.drawOval(60-30,60-30,60,60);

I am having a terrible time trying to figure out what the transforms should be.

The mapping from world coords (Wx,Wy) to pixels coords (Px,Py) is:

// pixel X
Px = 0 + w * ( vllx + (Wx - wllx)/(wurx - wllx) * (vurx - vllx);

// pixel Y
Py = h - h * ( vlly + (Wy - wlly)/(wury - wlly) * (vury - vlly);

I tried
g.translate (-wllx, -wlly);
g.scale (vw/ww,vh/wh);
g.translate (vllx, vlly);
g.scale (w, -h);
g.translate (0, h);
but nothing showed

I tried
g.translate (-wllx, -wlly);
g.scale (vw/ww,vh/wh);
which drew something, but is obviously not correct.

A program with only this stuff can be found at
www.devenezia.com/java/affine/Sample.java

Any help is greatly appreciated.

Richard A. DeVenezia

Richard A. DeVenezia, Jun 7, 2004

2. akGuest

> A program with only this stuff can be found at
I tested it, I think you may have some problem with ImageIO.

I don't have ImageIO installed, so I replaced image saving with following
code:

JFrame frame = new JFrame();
protected void paintComponent(Graphics g) {
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(buffer, 0, 0, null);
}

public Dimension getPreferredSize() {
return new Dimension(buffer.getWidth(), buffer.getHeight());
}
}));
frame.pack();
frame.show();

--
http://uio.dev.java.net

ak, Jun 7, 2004

3. Richard A. DeVeneziaGuest

ak wrote:
>> A program with only this stuff can be found at
>> www.devenezia.com/java/affine/Sample.java
>>

> I tested it, I think you may have some problem with ImageIO.
>
> I don't have ImageIO installed, so I replaced image saving with
> following code:
>
> JFrame frame = new JFrame();
> protected void paintComponent(Graphics g) {
> g.fillRect(0, 0, getWidth(), getHeight());
> g.drawImage(buffer, 0, 0, null);
> }
>
> public Dimension getPreferredSize() {
> return new Dimension(buffer.getWidth(),
> buffer.getHeight()); }
> }));
> frame.pack();
> frame.show();

ak,
Thanks for the JFrame snippet. I don't do alot of java gui, so it really
helped me out. (I was using Irfan to view the .png I was writing)

Instead of concatenating several Tx operations upon the Graphics2D (which
wasn't working out for me), I broke up the px= and py= equations into
components that the match affine matrix coefficients. Things are working as
I expect now, yeah!

Updated sample at
www.devenezia.com/java/affine

--
Richard A. DeVenezia

Richard A. DeVenezia, Jun 8, 2004