How do I scale/rotate an image?

We will assume that you know how to import Minueto into your project and how to create a MinuetoWindow. If this is not the case, please read How to build your first game Window?.

The MinuetoImage features two functions to accomplish these tasks.

MinuetoImage rotated = mimOriginalImage.rotate(Math.PI);
MinuetoImage scaled = mimOriginalImage.scale(2,2);

The first function will created a new copy of mimOriginalImage after rotating it 180 degrees (remember that Minueto works in radians). The second function will created a new copy of mimOriginalImage after scaling it to be twice as big.

Unfortunately, scaling and rotating are slow operations and you should avoid doing them in realtime. An easy solution to this is to pre-rotate or pre-scale your image at the start of your application.

MinuetoImage[] preRotatedShip = new MinuetoImage[360];

for(int i; i < 360; i++) {
    preRotatedShip[i] = spaceShip.rotate(i * Math.PI / 180);
}

This example creates 360 rotated copies of an image and stores them into a MinuetoImage array. This allows us quickly to draw an image of any orientation by retrieving it from our image array. Of course, depending on the type of application you are building and the size of the image, you might want to use less than 360 images.

The following code will load the image of a spaceship and create 120 rotated copies of the image. It will then draw the spaceship in the top left corner, continually rotating it. The program might require a few additional seconds to start (because of the caching), but the animation will be much faster.

import org.minueto.*; 
import org.minueto.handlers.*; 
import org.minueto.image.*; 
import org.minueto.window.*; 

public class Demo {

    public static void main(String[] args) {
        MinuetoWindow window; 
        MinuetoImage spaceShip;

        MinuetoImage[] preRotatedShip = new MinuetoImage[360];
        int orientation = 0;

        window = new MinuetoFrame(640, 480, true); 

        try {
            spaceShip = new MinuetoImageFile("spaceship.png");
        } catch(MinuetoIOException e) {
            System.out.println("Could not load image file");
        }

        for(int i; i < 120; i++) {
            preRotatedShip[i] = spaceShip.rotate(i * Math.PI / 60);
        }

        window.setVisible(true);
        while(true) { 
            orientation = (orientation + 1) % 120;
            window.draw(preRotatedShip[orientation], 0, 0);
            window.render();
            Thread.yield();
        }
    }
}