通过旋转和增加y轴来移动图像会引起尾随问题

原谅我,如果我有什么不清楚,这是一个棘手的事情,我要解释。

在一个小程序中,我画了一个小船gif。 我有它,所以我可以旋转的对象,然后增加或减lessy轴移动屏幕上的小家伙。 问题似乎是船的坐标与applet窗口的坐标不同步?

例如:我的小程序窗口的边界是800乘600.我通过旋转它然后增加或减lessy轴来移动船,但是当Y轴超过600或低于0时,我会得到这个奇怪的拖尾问题。 我正在缓冲吗?

这里是我的“主”和“船”类:

主要课程:

public class Main extends Applet implements Runnable, KeyListener { ship player1; private Image i; private Graphics buffer; AffineTransform affineTransform = new AffineTransform(); public void init() { this.setSize(800, 600); this.setBackground(Color.BLACK); addKeyListener(this); } @Override public void start() { player1 = new ship(); Thread thread = new Thread(this); thread.start(); } @Override public void paint (Graphics g){ Graphics2D g2d=(Graphics2D)g; player1.paint(g2d, this); } @Override public void update(Graphics g){ Graphics2D g2d=(Graphics2D)g; if(i == null){ i = createImage(this.getSize().width, this.getSize().height); buffer = i.getGraphics(); } buffer.setColor(getBackground()); buffer.fillRect(0, 0, this.getWidth(), this.getHeight()); buffer.setColor(getForeground()); paint(buffer); g2d.drawImage(i, 0, 0, this); } @Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()){ case KeyEvent.VK_RIGHT: player1.rotateRight(); break; case KeyEvent.VK_LEFT: player1.rotateLeft(); break; case KeyEvent.VK_UP: player1.moveUp(); break; case KeyEvent.VK_DOWN: player1.moveDown(this); break; } } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void run() { while(true){ repaint(); try { Thread.sleep(17); } catch (InterruptedException e) { e.printStackTrace(); } } } } 

船级:

  public class ship { private Image sprite_ship; //private Image ship_engines_green; private int x = 300; private int y = 500; //private int dx = 10; private int dy = 10; private int my = 0; AffineTransform affineTransform = new AffineTransform(); double rotation; public ship() { sprite_ship = Toolkit.getDefaultToolkit().getImage("ship.gif"); //ship_engines_green = Toolkit.getDefaultToolkit().getImage("ship_engines_green.gif"); } public void moveDown(Main M){ y += dy; System.out.println("Y axis: "+y); System.out.println("X axis: "+x); } public void moveUp(){ y -= dy; System.out.println("Y axis: "+y); System.out.println("X axis: "+x); } public void rotateRight(){ rotation += 5; System.out.println("Rotation : "+rotation); } public void rotateLeft(){ rotation -= 5; System.out.println("Rotation : "+rotation); } public void paint(Graphics g, Main M){ int spriteHeight = sprite_ship.getHeight(null); int spriteWidth = sprite_ship.getWidth(null); Graphics2D g2d=(Graphics2D)g; affineTransform = g2d.getTransform(); g2d.rotate(Math.toRadians(rotation), x+spriteWidth/2, y+spriteHeight/2); g2d.drawImage(sprite_ship,x,y,M); rotation = 0; } } 

非常感谢任何建议,或者如果有人能指出我正在做的事情,这是非常错误的。

我跑了一些testing,你的船图纸代码是不正确的。

目前您正在旋转整个Graphics2D,因为您从Graphics2D获得了AffineTransform。

您需要为船舶创建一个新的AffineTransform,或者在船级中保留一个作为variables。

 public void paint(Graphics2D g, Main M) { int spriteHeight = sprite_ship.getHeight(null); int spriteWidth = sprite_ship.getWidth(null); AffineTransform affineTransform = new AffineTransform(); affineTransform.rotate(Math.toRadians(rotation), x + spriteWidth / 2, y + spriteHeight / 2); affineTransform.translate(x, y); g.drawImage(sprite_ship, affineTransform, M); }