将图像旋转 x 度? - Java

Rotate image by x degrees? - Java

好吧,所以我正在尝试创建一个简单的游戏,我有一个星系漩涡状 id 喜欢旋转,但唯一的问题是,我不确定如果我使用 Graphics2D "g.rotate(x)"屏幕上我画的所有东西都在旋转,但我只想让银河旋转,而不是文字和东西。

背景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);

    }
}

主菜单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);
        }
    }




}

我自己不是专家,但这可能会有所帮助: Java: Rotating Images

这是他们的答案:

`// 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);

请注意,我并没有将原始发帖者代码据为己有!!!