Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to make an Image rotate?  (Read 401 times)
0 Members and 1 Guest are viewing this topic.
Offline Shuster
« Posted 2014-06-26 17:06:47 »

Hey ,
How can I make an image rotate?I want to make a rotation animation.
This is that I have tried but that doesnt work...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
   public void render(Graphics g) {
      AffineTransform at = new AffineTransform();
      Graphics2D g2d = (Graphics2D) g;

      if (type == 1) {
         switch (bodypart) {
         case 1:
            g2d.drawImage(tex.exbodypart1, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart1.getWidth() / 2.0, tex.exbodypart1
                       .getHeight() / 2.0);
            break;
         case 2:
            g2d.drawImage(tex.exbodypart2, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart2.getWidth() / 2.0, tex.exbodypart2
                       .getHeight() / 2.0);
            break;
         case 3:
            g2d.drawImage(tex.exbodypart3, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart3.getWidth() / 2.0, tex.exbodypart3
                       .getHeight() / 2.0);
            break;
         case 4:
            g2d.drawImage(tex.exbodypart4, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart4.getWidth() / 2.0, tex.exbodypart4
                       .getHeight() / 2.0);
            break;
         case 5:
            g2d.drawImage(tex.exbodypart5, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart5.getWidth() / 2.0, tex.exbodypart5
                       .getHeight() / 2.0);
            break;
         case 6:
            g2d.drawImage(tex.exbodypart6, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart6.getWidth() / 2.0, tex.exbodypart6
                       .getHeight() / 2.0);
            break;
         case 7:
            g2d.drawImage(tex.exbodypart7, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart7.getWidth() / 2.0, tex.exbodypart7
                       .getHeight() / 2.0);
            break;
         case 8:
            g2d.drawImage(tex.exbodypart8, (int) x, (int) y, null);
               at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart8.getWidth() / 2.0, tex.exbodypart8
                       .getHeight() / 2.0);
            break;
         }
      }

   }

Thanks Smiley
Offline KevinWorkman

JGO Wizard


Medals: 67
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #1 - Posted 2014-06-26 17:09:24 »

What have you tried? What did google tell you?

How are you drawing the image? Are you using Java2D, OpenGL, a library or framework?

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline Shuster
« Reply #2 - Posted 2014-06-26 17:19:10 »

I'm using java2d, I edited the topic.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

JGO Wizard


Medals: 67
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #3 - Posted 2014-06-26 17:21:27 »

Use an AffineTransformOp and pass it into the call to drawImage: http://stackoverflow.com/questions/8639567/java-rotating-images

Edit: There are a bunch of different ways to do this. The above link was just the first result for googling "java rotate image".

Static Void Games - Play indie games, learn game programming, upload your own games!
Online Longarmx
« Reply #4 - Posted 2014-06-26 17:24:04 »

I think that you also have to call
g2d.setTransform(at);
right after rotating.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2014-06-26 17:24:04 »

Use an AffineTransformOp and pass it into the call to drawImage: http://stackoverflow.com/questions/8639567/java-rotating-images
That was my question, 3 years ago Grin
Online SHC
« Reply #6 - Posted 2014-06-26 17:25:16 »

You can use this method. It's from my
ImageTool
class.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
/**
 * Rotates an image. Actually rotates a new copy of the image.
 *
 * @param img The image to be rotated
 * @param angle The angle in degrees
 * @return The rotated image
 */

public static Image rotate(Image img, double angle)
{
    double sin = Math.abs(Math.sin(Math.toRadians(angle))),
           cos = Math.abs(Math.cos(Math.toRadians(angle)));

    int w = img.getWidth(null), h = img.getHeight(null);
    int neww = (int) Math.floor(w * cos + h * sin),
        newh = (int) Math.floor(h * cos + w * sin);

    BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh));

    Graphics2D g = bimg.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(Math.toRadians(angle), w / 2, h / 2);
    g.drawRenderedImage(toBufferedImage(img), null);
    g.dispose();

    return (Image) bimg;
}

Hope this helps.

Offline KevinWorkman

JGO Wizard


Medals: 67
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #7 - Posted 2014-06-26 17:35:42 »

That was my question, 3 years ago Grin

Creepy. Even creepier is when you're googling and start reading an answer and realize YOU wrote that but completely forgot.

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline Shuster
« Reply #8 - Posted 2014-06-27 11:29:49 »

You can use this method. It's from my
ImageTool
class.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
/**
 * Rotates an image. Actually rotates a new copy of the image.
 *
 * @param img The image to be rotated
 * @param angle The angle in degrees
 * @return The rotated image
 */

public static Image rotate(Image img, double angle)
{
    double sin = Math.abs(Math.sin(Math.toRadians(angle))),
           cos = Math.abs(Math.cos(Math.toRadians(angle)));

    int w = img.getWidth(null), h = img.getHeight(null);
    int neww = (int) Math.floor(w * cos + h * sin),
        newh = (int) Math.floor(h * cos + w * sin);

    BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh));

    Graphics2D g = bimg.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(Math.toRadians(angle), w / 2, h / 2);
    g.drawRenderedImage(toBufferedImage(img), null);
    g.dispose();

    return (Image) bimg;
}

Hope this helps.


Thats pretty complicated for such an easy task and I have tried it and it didnt work for me for some reason..

Use an AffineTransformOp and pass it into the call to drawImage: http://stackoverflow.com/questions/8639567/java-rotating-images

Edit: There are a bunch of different ways to do this. The above link was just the first result for googling "java rotate image".

Works like a chram, thanks a lot!!
If someone is interested, this the working code:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
   public void render(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      BufferedImage image = null;
      if (type == 1) {
         switch (bodypart) {
         case 1:
            image = tex.exbodypart1;
            break;
         case 2:
            image = tex.exbodypart2;
            break;
         case 3:
            image = tex.exbodypart3;
            break;
         case 4:
            image = tex.exbodypart4;
            break;
         case 5:
            image = tex.exbodypart5;
            break;
         case 6:
            image = tex.exbodypart6;
            break;
         case 7:
            image = tex.exbodypart7;
            break;
         case 8:
            image = tex.exbodypart8;
            break;
         }

         i += rotation;
         double rotationRequired = Math.toRadians(i);
         double locationX = image.getWidth(null) / 2;
         double locationY = image.getHeight(null) / 2;
         AffineTransform tx = AffineTransform.getRotateInstance(
               rotationRequired, locationX, locationY);
         AffineTransformOp op = new AffineTransformOp(tx,
               AffineTransformOp.TYPE_BILINEAR);
         g2d.drawImage(op.filter(image, null), (int) x, (int) y, null);

      }

   }
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!