This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes AffineTransform make an image move Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AffineTransform make an image move" Watch "AffineTransform make an image move" New topic
Author

AffineTransform make an image move

Mauricio Lopes
Greenhorn

Joined: Aug 04, 2009
Posts: 12
I've been trying to make an image rotate in a panel. I did it. But also i want the image to make a translation.

Here is the code i tryed at paintComponent() :

//////////////////////////

Graphics2D g2 = (Graphics2D) g ;
AffineTransform at = new AffineTransform() ;

// at.rotate ( angle in radians ) ;
at.rotate( this.getAngulo(1, 1) ) ;

g2.drawImage(imgemFlecha, at, this);

/////////////////////////////


I cant change image x,y doing this drawImage(). I want to make that change.

Any suggestions ?
Mauricio Lopes
Greenhorn

Joined: Aug 04, 2009
Posts: 12
??
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Mauricio Lopes wrote:??

http://faq.javaranch.com/java/PatienceIsAVirtue


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Cathy McDuff
Greenhorn

Joined: May 09, 2014
Posts: 18
Hi,

I just wanted to say thank you to Craig for posting that class, it's one of the few that actually makes sense and is easy to follow. Other classes using Affine Transform have been horrific to understand. So yeah, thank you very much, you've been a big help )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Always start paintComponent with a super call and it has protected access, as in Craig Wood's post.
Don't use AffineTransforms on a Graphics object and then reuse the Graphics object. Take a defensive copy. What happens is that the calculations for the transform may be impossible to undo exactly, because of the imprecision of floating‑point arithmetic. Scale and translate usually can be undone and I have not had problems with rotate, but shear causes problems. The affine transforms also seem to be built into the Graphics2D object, so you have simpler ways to access them
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

+1 on using Graphics.create. However, you should also dispose it manually:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Rob Spoor wrote: . . . you should also dispose it manually . . .
I never knew that. Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
More about Graphics#dispose here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AffineTransform make an image move
 
Similar Threads
Wanted: Java2D example of zooming and panning with scroll bars
JPanel Form Problems
Rotating a JWindow with multiple JPanels
Image Rotation
Resetting or Undoing AffineTransforms