我已经被这个问题困扰了好几个小时了。我有一个JPanel,我用它画了几张图。
这是一个2D游戏,我只需使用repaint();就可以将所有内容打印到屏幕上。
在某种程度上,我想画一个开着手枪的人。到目前为止,一切都很好,但前提是这个人要朝北看,因为我的动画图片都画到了北边。手动旋转它们并将它们粘贴到我的项目中会让它走得太远,所以我决定旋转图像,使其在任何方向上都可用,但我不知道怎么做。下面是一些代码:
此方法中的一个简单开关检查当前朝向的方向。请注意,第一种情况( north )工作正常,因为图片是默认绘制到北方的。
switch(CharDirection){
case NORTH:
if(PistolAnim == 0){
try {
Man = ImageIO.read(ManNorthURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try{
if(PistolAnim == 1){
Man = ImageIO.read(Shoot1URL);
PistolAnim = 2;
return;
}
if(PistolAnim == 2){
Man = ImageIO.read(Shoot2URL);
PistolAnim = 3;
return;
}
if(PistolAnim ==3){
Man = ImageIO.read(Shoot3URL);
PistolAnim = 0;
return;
}
} catch (IOException e){
}
}
break;
case EAST:
if(PistolAnim == 0){
try {
Man = ImageIO.read(ManEastURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try{
if(PistolAnim == 1){
Man = ImageIO.read(Shoot1URL);
PistolAnim = 2;
return;
}
if(PistolAnim == 2){
Man = ImageIO.read(Shoot2URL);
PistolAnim = 3;
return;
}
if(PistolAnim ==3){
Man = ImageIO.read(Shoot3URL);
PistolAnim = 0;
return;
}
} catch (IOException e){
}
}
break;整数变量"PistolAnim“用于显示动画的状态。0-无正在进行的动画1-图片1
2-图2
3-图3
paint函数(嗯,最重要的部分)如下所示:
g.drawImage(Man, CharX, CharY, Man.getWidth(), Man.getHeight(), null);我试着使用下面的旋转方法:
public void rotate(double Degrees, BufferedImage img1){
ImageIcon icon = new ImageIcon(img1);
BufferedImage BlankCanvas = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)BlankCanvas.getGraphics();
g2d.rotate(Math.toRadians(Degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);
g2d.drawImage(img1, 100, 100, null);
img1 = BlankCanvas;
}我在互联网上找到了它,它在我的一些测试中起作用了,但现在它没有做它应该做的事情。我插入了这行
rotation(90, Man);几乎在我的代码的每一点上,但都不起作用。我也不能在paint中旋转整个Graphics2D对象(它被称为g),因为它也会绘制其他图片。
有谁有主意吗?
谢谢!
发布于 2015-03-14 23:46:49
Graphics2D g2d = (Graphics2D)BlankCanvas.getGraphics();这个代码是错误的。不应使用getGraphics()方法。您应该从JPanel的paintComponent()方法中进行自定义绘制,并使用传递给此方法的Graphics对象。然后,当您调用“rotate(...)”方法时,将此Graphics对象传递给该方法,并使用它来绘制旋转后的图像。
不用担心旋转/平移代码,只需使用Rotated Icon即可。然后,您可以使用paintIcon(...)方法绘制图标:
RotatedIcon rotated = new RotatedIcon(icon, degrees);
rotated.paintIcon(...) 发布于 2020-01-11 20:26:27
下面的方法会将你的BufferedImage旋转到你指定的角度。您可以使用此方法旋转图像。
//method for rotating the image to a specific angle
public BufferedImage rotateImage(BufferedImage image, double angle) {
//calculate the new size of the rotated image
double rad = Math.toRadians(angle);
double sin = Math.abs(Math.sin(rad));
double cos = Math.abs(Math.cos(rad));
int newWidth = (int) Math.floor(image.getWidth() * cos + image.getHeight() * sin);
int newHeight = (int) Math.floor(image.getHeight() * cos + image.getWidth() * sin);
//createding a new bufferedimage with size that will have rotated image
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_INDEXED);
//we set the information of how much will the image be
//displaced in x an y axis during the transform operation
AffineTransform at = new AffineTransform();
at.translate((newWidth - image.getWidth()) / 2, (newHeight - image.getHeight()) / 2);
//calculate the center of transformation
int coordX = image.getWidth() / 2;
int coordY = image.getHeight() / 2;
//setting the center and angle information for rotate operation
at.rotate(rad, coordX, coordY);
//executing the rotate operation
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
rotated = scaleOp.filter(image, rotated);
return rotated;
}将图像旋转到您想要的角度后,您可以绘制图像。
BufferedImage rotatedImage = rotateImage(image,90);
//draw imagehttps://stackoverflow.com/questions/29050769
复制相似问题