Results 1 to 2 of 2
  1. #1
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Default Rotating a Image with keylistener?

    Hi,
    As the title suggests, is there a method/way in java that can rotate a image by a certain degree(by using key listener). I am in need of such method because i have a drawImage() method with (image, intx, inty, image observer) which prints a image on the screen of a car from the top view. The car is able to move up down right and left. BUT i would like to rotate the car actively on the screen by the input of the left key.
    Thx
    Heres my code so far

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    
    public class startGame extends JPanel implements ActionListener,KeyListener
    {
    	Timer time = new Timer(5,this);
    	int x=0,y=0,velX = 0,velY=0;
    	Image car1;
    	
    	public static void main(String[] args) 
    	{
    		startGame game = new startGame();
    		JFrame frame = new JFrame();
    		frame.setTitle("NEED FOR SPEED");
    		frame.setSize(800,800);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(game);
    		
    
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		
    		super.paintComponent(g);
    		Graphics2D ga = (Graphics2D) g;
    		ImageIcon ii = new ImageIcon("car1.jpg");
    		car1=ii.getImage();
    		ga.drawImage(car1, x, y, null);
    		time.start();
    		
    		
    	}
    	public startGame()
    	{
    
    		time.start();
    		addKeyListener(this);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);
    		repaint();
    		
    		
    		
    	}
    	
    	public void actionPerformed(ActionEvent e) 
    	{
    		if(x<0)
    		{
    			velX=0;
    			x=0;
    		}
    		if(y<0)
    		{
    			velY=0;
    			y=0;
    		}
    		if(x>800)
    		{
    			velX=0;
    			x=800;
    		}
    		if(y>800)
    		{
    			velY=0;
    			y=800;
    		}
    		x = x + velX;
    		y = y + velY;
    		repaint();
    	}
    
    	public void keyTyped(KeyEvent e)
    	{
    		
    	}
    	public void keyReleased(KeyEvent e)
    	{
    		velX=0;
    		velY=0;
    	}
    	public void keyPressed(KeyEvent e) 
    	{
    		int c = e.getKeyCode();
    		
    		if(c == KeyEvent.VK_LEFT)
    		{
    			velX = -1;
    			velY = 0;
    		}
    		if(c == KeyEvent.VK_UP)
    		{
    			velX = 0;
    			velY = -1;
    		}
    		if(c == KeyEvent.VK_RIGHT)
    		{
    			velX = 1;
    			velY = 0;
    		}
    		if(c == KeyEvent.VK_DOWN)
    		{
    			velX = 0;
    			velY = 1;
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Rotating a Image with keylistener?

    Don't conflate two issues. Reacting to keyboard input and drawing an image rotated are two disparate subjects. From your code, it appears that you don't have a problem with the former. For the latter, Graphics2D has a rotate(...) method, or you can use AffineTransform.

    You might like to go through this Tutorial Trail: 2D Graphics (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with Image Rotating?
    By Leospaceman in forum Java 2D
    Replies: 2
    Last Post: 05-31-2011, 02:48 PM
  2. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  3. rotating a rectangular image
    By ighor10 in forum Java 2D
    Replies: 1
    Last Post: 10-24-2010, 07:22 PM
  4. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-27-2009, 11:54 PM
  5. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •