Implement Viewports and Windows using AffineTransforms ?

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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
 
A

ak

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();
frame.getContentPane().add(new JScrollPane(new JPanel() {
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();
 
R

Richard A. DeVenezia

ak said:
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();
frame.getContentPane().add(new JScrollPane(new JPanel() {
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top