使用Java中的算法更改图形的角度/位置 [英] Change the angle/position of a drawing with a algorithm in Java
问题描述
您好我很好奇如何解决这个问题:我用Java创建了一个带有fillArc,drawArc方法的pacman,我现在屏幕上有一个pacman,无论它走向何方,它始终向右看。我的问题是..有没有办法在Java中水平更改对象或者在Java中水平翻转它?
Hello I am very curious how to solve this problem: I created a pacman with the fillArc, drawArc method in Java and I have a pacman guy on my screen now that is always looking to the right no matter what direction it goes.. my question is.. is there a way to change the object by degrees or flip it horizontally in Java?
我试图使用AffineTransform但是我没有到达我想要文档......我应该如何使用switch语句实现这一目标?我试着做以下但是因为我不知道如何继续而陷入困境。
i tried to use AffineTransform but i don't get where i want with the documentation... How should I be able to achieve this using a switch statement? I tried to do the following but I get stuck at this part because I don't know how to continue.
DrawPacMan pacman = new DrawPacMan();
DrawPacMan ghost1 = new DrawPacMan();
DrawPacMan ghost2 = new DrawPacMan();
AffineTransform pac = new AffineTransform();
public void setPacManView(int waarde) {
// set the view of pacman
switch (waarde) {
case 0 :
// here one view of pacman
break;
case 1 :
// here one view of pacman
break;
case 2 :
// here one view of pacman
break;
case 3 :
// here one view of pacman
break;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// pacman movement
diameter = 75;
pacman.drawPacMan(g, getHorPlaats(), getVerPlaats(), diameter, Color.yellow);
// ghosts movement
int g1x;
for(g1x = 0; g1x < 10; g1x++) {
pacman.drawGhost(g, g1x, 40, diameter, Color.red);
}
pacman.drawGhost(g, 170, 70, diameter, Color.blue);
}
推荐答案
尝试类似......
Try something like...
该演示旨在允许图像通过虚拟天使旋转(角度<0&> 360),但基本概念是相同的。 ..
The demo is designed to allow images to be rotated through virtual angels (angles < 0 & > 360), but the basic concept is the same...
public class TestFlipImage {
protected static final String IMAGE_PATH = "/path/to/your/image";
public static void main(String[] args) {
new TestFlipImage();
}
public TestFlipImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
BufferedImage image = null;
try {
image = ImageIO.read(new File(IMAGE_PATH));
} catch (IOException ex) {
}
JPanel mainPane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
mainPane.add(new ImagePane(image, 0));
mainPane.add(new ImagePane(image, 90));
mainPane.add(new ImagePane(image, 180));
frame.add(mainPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage masterImage;
private BufferedImage renderedImage;
public ImagePane(BufferedImage image, int angle) {
masterImage = image;
applyRotation(angle);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(renderedImage.getWidth(), renderedImage.getHeight());
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
protected int getVirtualAngle(int angle) {
float fRotations = (float) angle / 360f;
int rotations = (int) (fRotations - (fRotations / 1000));
int virtual = angle - (rotations * 360);
if (virtual < 0) {
virtual = 360 + virtual;
}
return virtual;
}
// The code is designed to rotate an image through 90 degree
// angles, but it can handle angle's less then 0 and greater then
// 360 degrees
public void applyRotation(int angle) {
// This will only work for angles of 90 degrees...
// Normalize the angle to make sure it's only between 0-360 degrees
int virtualAngle = getVirtualAngle(angle);
Dimension size = new Dimension(masterImage.getWidth(), masterImage.getHeight());
int masterWidth = masterImage.getWidth();
int masterHeight = masterImage.getHeight();
double x = 0; //masterWidth / 2.0;
double y = 0; //masterHeight / 2.0;
switch (virtualAngle) {
case 0:
break;
case 180:
break;
case 90:
case 270:
size = new Dimension(masterImage.getHeight(), masterImage.getWidth());
x = (masterHeight - masterWidth) / 2.0;
y = (masterWidth - masterHeight) / 2.0;
break;
}
renderedImage = new BufferedImage(size.width, size.height, masterImage.getTransparency());
Graphics2D g2d = renderedImage.createGraphics();
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(Math.toRadians(virtualAngle), masterWidth / 2.0, masterHeight / 2.0);
g2d.drawImage(masterImage, at, null);
g2d.dispose();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int width = getWidth() - 1;
int height = getHeight() - 1;
int x = (width - renderedImage.getWidth()) / 2;
int y = (height - renderedImage.getHeight()) / 2;
g2d.drawImage(renderedImage, x, y, this);
}
}
}
附加
您可能还想看看 AffineTransform.rotate() - 如何同时进行xlate,旋转和缩放? wch讨论在水平轴和垂直轴上翻转图像的方法
You may also want to take a look at AffineTransform.rotate() - how do I xlate, rotate, and scale at the same time? wch discusses a means for flipping a image on its horizontal and vertical axis
这篇关于使用Java中的算法更改图形的角度/位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!