好吧,所以我试图创造一个简单的游戏,我有一个旋转的星系旋转,只有事情是,我不确定如何,如果我使用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);
}
}
}
答案 0 :(得分: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);
请注意我不会赞成原始海报代码!!!