Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AffineTransform make an image move

 
Mauricio Lopes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
??
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mauricio Lopes wrote:??

http://faq.javaranch.com/java/PatienceIsAVirtue
 
Cathy McDuff
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 on using Graphics.create. However, you should also dispose it manually:
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . you should also dispose it manually . . .
I never knew that. Thank you.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More about Graphics#dispose here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic