Implement Viewports and Windows using AffineTransforms ?

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

  1. 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
    #1
    1. Advertising

  2. Richard A. DeVenezia

    ak Guest

    > 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();
    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();

    --
    http://uio.dev.java.net
    http://reader.imagero.com
     
    ak, Jun 7, 2004
    #2
    1. Advertising

  3. 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();
    > 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

    --
    Richard A. DeVenezia
     
    Richard A. DeVenezia, Jun 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rafael Veronezi

    Implement Windows and Forms Authentication

    Rafael Veronezi, Nov 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    360
    Rafael Veronezi
    Nov 4, 2003
  2. Jean CADEAU
    Replies:
    1
    Views:
    397
    Alvin Bruney
    Nov 30, 2003
  3. Marco Vasquez
    Replies:
    0
    Views:
    388
    Marco Vasquez
    Dec 30, 2003
  4. =?Utf-8?B?TmFk?=
    Replies:
    8
    Views:
    381
    Alok Chauhan
    May 16, 2005
  5. Steven Woody
    Replies:
    2
    Views:
    331
    Jay Nabonne
    Dec 29, 2005
Loading...

Share This Page