java - 将图像旋转 x 度? - java

标签 java image rotation image-rotation

好吧,所以我正在尝试创建一个简单的游戏,我有一个星系漩涡 id 喜欢旋转,唯一的问题是,我不确定如何,如果我使用 Graphics2D“g.rotate(x)”,屏幕上的所有内容都会旋转,但我只希望星系旋转,而不是文字和其他东西。

背景类:

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

    }
}

MainMenu 类:

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

请注意,我不会将原始海报代码归功于我!!!

关于java - 将图像旋转 x 度? - java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29872393/

相关文章:

javascript - 我如何在 Javascript 中旋转图像?

c++ - 轨道物体围绕轨道物体

c# - 如何旋转图片框中的图片

java - 使用扫描仪的逻辑错误。文本文件的第一行不打印到标准输出

php GD 创建透明png图片

java - 如何停止多线程聊天客户端

android - 如何在 Android Q 中获取深度图

php - 如何使用 PHP 的 GD 库对图像执行缝线雕刻?

java - 单选按钮在 JSP/Java 中没有响应

java - 更改 Java 默认信任库密码的影响