Jump to content

  • Log In with Google      Sign In   
  • Create Account

Getting my object to rotate and move 'ala Asteroid

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1   Members   


Posted 28 January 2013 - 06:02 PM

I'm trying to work with rotation and movement but I have a problem that I can't really solve. When I rotate from the starting point then it works fine and a can rotate at any direction and then move to that direction. But when I try to rotate from my new position, then the object moves around the "starting point" (g2d.Translate). I try to somehow update the root position to my new position, but it doesn't really work. Try it out and see. Maybe I don't use the easiest method for this, but I feel that I'm close with this method and I want to use it. Here is my code:


import java.applet.AudioClip;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Level1 extends JPanel implements ActionListener{

	Image i;
	Graphics doubleG;
	ImageIcon keyIcon = new ImageIcon(this.getClass().getResource("img/BubbleSpeed.gif"));
	Image key = keyIcon.getImage();
	ImageIcon backgroundIcon = new ImageIcon(this.getClass().getResource("img/background3.png"));
	Image background = backgroundIcon.getImage();
	URL music = null;
	AudioClip musicSound;
	int x = 400;
	int y = 400;
	int x2 = 400;
	int y2 = 400;
	double dx;
	double dy;
	int rotate;
	int center = 20;
	Timer tim;

		tim = new Timer(25, this);

	public void move(){
		y += dy;
		x += dx;

	public void update(Graphics g) {
		if(i == null){
			i = createImage(this.getWidth(), this.getHeight());
			doubleG = i.getGraphics();
		doubleG.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.drawImage(i, 0, 0, this);

	public void paintComponent(Graphics g){
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(background, 0, 0, null);
		g2d.rotate(Math.toRadians(rotate), x2,y2);
		g2d.drawImage(key, x-center, y-center, null);

	public void actionPerformed(ActionEvent arg0) {

	KeyListener k = new KeyAdapter(){
		public void keyPressed(KeyEvent e) {

			if(e.getKeyCode() == KeyEvent.VK_X){	
			if(e.getKeyCode() == KeyEvent.VK_Z){	
			if(e.getKeyCode() == KeyEvent.VK_W){	
				dy = -10;
			if(e.getKeyCode() == KeyEvent.VK_S){	
				dy = 10;
		public void keyReleased(KeyEvent e) {
			dx = 0;
			dy = 0;
			x2 = x;
			y2 = y;



Edited by Patriarch K, 28 January 2013 - 06:31 PM.

#2   Members   


Posted 28 January 2013 - 06:14 PM

What happens if you do the rotate before the translate? You are translating then rotating there.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3   Members   


Posted 28 January 2013 - 06:30 PM

I did some editing in my first post since I didn't use the translate method at all. It was there for no reason. Check out the code now instead! :)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.