Re: AffineTransform.getScaleInstance question



dtown22@xxxxxxxxx wrote:
Hi Knute,

You are correct, I want the shape to be drawn from the mouse position
upwards and to the right, I want to scale the shape so that it seems
like it is growing. My shape is actually a little more complex than a
square, so clipping it would make it look wierd.

I ran your example, and it is close to what i want, I like how it grows
from the bottom up, but I want it to start growing from the point of
the mouse click, rather than the bottom of the frame. I tried playing
with the transfrom, but I didn't have any luck.

Also, why do i not need to dispose g2? is most examples I have seen,
this seems to be the common practice, so I took it as a required step.


This was an intriguing problem and I really didn't know how to solve it at first. I knew there had to be a simple way and there is. It's a good thing that I didn't have a lot of work to do the last couple of days or I'd be in trouble :-).

I think this addresses all of your issues and demonstrates how it works too.

Regarding disposing of the Graphics reference, it isn't necessary in the paintComponent() method. It is if you create a Graphics from an Image that you are going to use for active rendering or for some other purpose. What you need to be careful of though is changing the Graphics transform and not resetting that when you leave the paintComponent(). I avoid that by not setting the transform on the Graphics but using the AffineTransform to modify the Shape. The Graphics.translate() would be a problem if we weren't resetting it every time that we draw the Shape.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class PaintTest2 extends JPanel implements Runnable {
int x,y;
double scale;
Polygon poly = new Polygon(new int[] {0,10,45,60,56,60},
new int[] {0,30,42,27,12,0},6);
static boolean flip;

public PaintTest2() {
setPreferredSize(new Dimension(400,300));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
scale = 0.0;
new Thread(PaintTest2.this).start();
}
});
}

public void run() {
while (scale < 1.0) {
scale += 0.1;
repaint();
try { Thread.sleep(40); }
catch (InterruptedException ie) { }
}
}

public void paintComponent(Graphics g2D) {
Graphics2D g = (Graphics2D)g2D;
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(getForeground());

g.translate(x,y); // follow the mouse

AffineTransform at = new AffineTransform();
at.scale(1.0,scale); // scale it
if (flip)
at.scale(1.0,-1.0); // flip it
Shape s = at.createTransformedShape(poly);
g.fill(s);
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);

final JCheckBoxMenuItem cbmi =
new JCheckBoxMenuItem("Flip It!",flip);
cbmi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
flip = cbmi.getState();
}
});
mb.add(cbmi);

PaintTest2 pt2 = new PaintTest2();
f.add(pt2);

f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: Zoom relative to mouse position
    ... public class ZoomDemo extends JPanel implements Runnable { ... public void mouseWheelMoved{ ... scale = Math.max; ... I think I may be missing a transform or two. ...
    (comp.lang.java.programmer)
  • Re: Select an area of an image...
    ... I am using an AffineTransform for the zoom in and out to scale and translate and draw the entire image so that it is displayed in the centre of the window. ... startX = me.getX; ... startY = me.getY; ... public void mouseReleased{ ...
    (comp.lang.java.programmer)
  • Cant get zooming into a selected area to work correctly.
    ... private BufferedImage _image; ... private Rectangle2D _rectDragBounds; ... public void componentResized{ ... public void setScale(float scale) ...
    (comp.lang.java.programmer)
  • Re: AffineTransform.getScaleInstance question
    ... and the way i defined my polygon. ... I want to scale the shape so that it seems ... It is if you create a Graphics from an Image ... public void mousePressed{ ...
    (comp.lang.java.gui)
  • Re: AWT event model problem
    ... I followed your suggestions and initialized the Graphics g inside each event ... However the FIRST TIME i left click and hold the mouse down, ... public void paint ... public void mouseEntered(MouseEvent e) { ...
    (comp.lang.java.gui)