gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:28 30 4
gpt4 key购买 nike

好吧,所以我正在尝试创建一个简单的游戏,我有一个星系漩涡 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com