, поэтому прежде всего хочу сказать, что я новичок в использовании функции перевода AffineTranslation и Graphics2D. Прошу прощения, если мой вопрос непонятен! К сожалению, я не знаю, как сформулировать вопрос.
Чего я пытаюсь достичь: Я хочу иметь камеру, которая следует за игроком, и мне также нужен AffineTransform, который управляет вращение игрока. Поэтому мне в основном нужна классная камера c в сочетании с возможностью вращать игрока с помощью кнопок 'D' и 'A' . Пример этого "вращения" :
This is taken out of my game and the rotation works as intended. However when I tried adding a camera that follows the player something weird happened. This is the code inside my paintComponent class:
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
g2d.translate(playerCam.getX(), playerCam.getY());
//Set font
g2d.setFont(pixelFont_reg);
//
AffineTransform backup = g2d.getTransform();
AffineTransform a = AffineTransform.getRotateInstance(player.getRotation(),player.getX() + (player.getWidth() / 2),player.getY() + (player.getHeight() / 2));
//Paint grid of grass as a test of Assets class
for(Tile tile : tiles) {
tile.drawTile(g2d);
}
//Rotate player depending on rotation var
g2d.transform(a);
player.drawEntity(g2d);
//Stop rotating player
g2d.setTransform(backup);
g2d.translate(-playerCam.getX(), -playerCam.getY());
//Draw Font
g2d.setColor(Color.white);
g2d.drawString("FPS: " + Integer.toString(GameLoop.frameRate), 10, 40);
//End of Draw Font
}
This code didn't affect the player at all. It only affected the tiles and the text I had displayed? I predict that my AffineTransform that handles the rotation has something to do with it but I have no clue on why? Does anyone have a solution to this issue? I searched the web but because I didn't know how to word the issue it resulted in zero results.
You can see the issue здесь .
Спасибо!
EDIT [ MRE]
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class myPanel extends JPanel {
private double rotation = Math.toRadians(45);
private static final long serialVersionUID = 1L;
private Rectangle2D.Double player = new Rectangle2D.Double(250,250,50,50);
private double cameraX,cameraY;
public myPanel() {
timer.start();
}
public Dimension getPreferredSize() {
return new Dimension(500,500);
}
public void paintComponent(Graphics g) {
//TODO In this MRE I prove that the AffineTransformation that has an effect on the player Rectangle is not effected by the g2d.translate(). How do I fix this issue?
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
AffineTransform backup = g2d.getTransform();
AffineTransform rotationTransform = AffineTransform.getRotateInstance(rotation,player.getX() + (player.getWidth() / 2), player.getY() + (player.getHeight() / 2));
rotationTransform.concatenate(g2d.getTransform());
//Translate using Graphics2D
cameraX++;
cameraY++;
rotationTransform.translate(cameraX, cameraY);
g2d.setColor(Color.red);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.black);
//Transform Player below
g2d.setTransform(rotationTransform);
g2d.fill(player);
backup.concatenate(g2d.getTransform());
g2d.setTransform(backup);
//End of Transform Player
//Translate back using Graphics2D
rotationTransform.translate(-cameraX, -cameraY);
}
Timer timer = new Timer(60, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
//Rotate player
rotation += 0.1;
}
});
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new myPanel());
frame.getPreferredSize();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Любая помощь приветствуется!