Results 1 to 2 of 2
- 12-27-2012, 03:54 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 28
- Rep Power
- 0
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; } } }
- 12-27-2012, 04:56 AM #2
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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Help with Image Rotating?
By Leospaceman in forum Java 2DReplies: 2Last Post: 05-31-2011, 02:48 PM -
Rotating Buffered Image distorts image
By VortexSpin in forum Java 2DReplies: 1Last Post: 02-13-2011, 05:54 AM -
rotating a rectangular image
By ighor10 in forum Java 2DReplies: 1Last Post: 10-24-2010, 07:22 PM -
Rotating an image
By lackofcolor in forum Java 2DReplies: 3Last Post: 02-27-2009, 11:54 PM -
Rotating Image?
By sciguy77 in forum Java AppletsReplies: 9Last Post: 02-17-2009, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks