Forgive me if me anything is unclear, this is a tricky thing for me to explain.
In an applet I draw a little ship gif. I have it so I can rotate the object and then increase or decrease the y axis to move the little guy around the screen. The problem it seems is that the coordinates for the ship are not in sync with those of the applet window?
For example: the boundaries for my applet window are 800 by 600. I move the ship by rotating it and then increasing or decreasing the y axis, but whenever the Y axis is exceeds 600 or is lower than 0 I get this weird trailing issue. Am I doing buffering right?
here are my "Main" and "ship" classes:
Main class:
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(); } } } }
ship class:
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; } }
much thanks for any suggestions