Search code examples
javaimagerotationimage-rotation

Rotate image by x degrees? - Java


All right, so I am attempting to create a simple-ish game and I have a galaxy swirly id like to rotate, only thing is though, I am unsure how, if I use the Graphics2D "g.rotate(x)" everything on screen I had drawn spins but I only want the galaxy to rotate and not the words and stuff.

Background class:

package TileMap;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class Background {

    private BufferedImage image;
    private int x;
    private int y;
    private int dx;
    private int dy;

    public Background(String imgLctn){

        try{
            image = ImageIO.read(getClass().getResourceAsStream(imgLctn));

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }



    public void draw(Graphics2D g){

        g.drawImage(image, x - 44, y - 33, null);

    }
}

The MainMenu class:

package GameState;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import Main.GamePanel;
import TileMap.Background;

public class MainMenu extends GameState {

    private int currentChoice = 0;

    private Background bg;

    private String[] options = {

            "Start",
            "Options",
            "Quit"

    };

    private Color titleColor;
    private Font titleFont;
    private Font subFont;

    public MainMenu(GameStateManager gsm){

        this.gsm = gsm;

        try{
            bg = new Background("/Backgrounds/MenuBG.png");

            titleColor = new Color(0, 255, 0);
            titleFont = new Font("Youre gone", Font.ITALIC, 30);
            subFont = new Font("Youre gone", Font.PLAIN, 18);

        }
        catch(Exception e){
            e.printStackTrace();
        }

    }


    @Override
    public void init() {

    }

    @Override
    public void update() {

    }

    @Override
    public void draw(Graphics2D g) {

        bg.draw(g);

        g.setColor(titleColor);
        g.setFont(titleFont);
        g.drawString("On My Way To Mars", GamePanel.WIDTH1 / 8, GamePanel.HEIGHT1 / 3);
        g.fillRect(GamePanel.WIDTH1 / 8, GamePanel.HEIGHT1 / 3, (int) (GamePanel.WIDTH1/1.53), 3);


        for(int i = 0;i < options.length;i++){

            g.setFont(subFont);

            if(i == currentChoice){
                g.setColor(new Color(255,0,255));

            }
            else{
                g.setColor(new Color(0,255,255));
            }
            if(i < 1){
                g.drawString(options[i], GamePanel.WIDTH1 / 8, GamePanel.HEIGHT1 / 2 + i);
            }
            else{
                g.drawString(options[i], GamePanel.WIDTH1 / 8, (GamePanel.HEIGHT1 - 2) /2 + (i*20));
            }
        }

    }
    private void select(){

        if(currentChoice == 0){

        }
        if(currentChoice == 1){

        }
        if(currentChoice == 2){

            System.exit(0);

        }

    }

    public void keyPressed(int k){
        if(k == KeyEvent.VK_W){
            System.exit(0);
        }
    }




}

Solution

  • I'm not an expert myself but this might help: Java: Rotating Images

    Here's their answer:

    `// The required drawing location
    int drawLocationX = 300;
    int drawLocationY = 300;
    
    // Rotation information
    
    double rotationRequired = Math.toRadian(45);
    double locationX = image.getWidth() / 2;
    double locationY = image.getHeight() / 2;
    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    
    // Drawing the rotated image at the required drawing locations
    g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);
    

    Please note I'm not taking credit for the original posters code!!!