Results 1 to 4 of 4
- 06-16-2010, 05:50 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
Problem in rotating and moving image at the same time
I am making a 2d car game in which you have upper view.
im having a serious problem that is i cant rotate and move my car(image)
at the same time
i mean if i press both up and right arrow keys at the same time it just rotate but i want to do both things
if any one can solve this problem i will be very thankful
here is my code
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; import java.applet.*; import java.net.URL; public class RaceGame extends JComponent { JFrame frame; static int x=560; static int y=410; static int currentAngle=0; static double hspeed,vspeed; static double carspeed=0.1; double hangle,vangle; Image car; Image finish; Image getImage(String filename) { URL url = getClass().getResource(filename); ImageIcon icon = new ImageIcon(url); return icon.getImage(); } public RaceGame() { car=getImage("car1.jpeg"); //finish=getImage("finish.jpg"); } public void CreateBase() { frame=new JFrame("Dare 2 Race"); frame.setBounds(70,30,650,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c=frame.getContentPane(); c.add(new RaceGame()); frame.addKeyListener(new adapter()); c.setBackground(Color.BLACK); GameMain gm=new GameMain(); gm.MainBase(); } public void rotate() { currentAngle+=1; if(currentAngle>=360) { currentAngle=0; } repaint(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d=(Graphics2D)g; AffineTransform origXform=g2d.getTransform(); AffineTransform newXform=(AffineTransform)(origXform.clone()); newXform.rotate(Math.toRadians(currentAngle),x,y); g2d.setTransform(newXform); //g2d.drawImage(finish,12,10,null); if( (x<=640 && x>=420) && y==380 || (x<=640 && x>=320) && y==460 || x==420 && (y<=380 && y>=300) || x==320 && (y<=460 && y>=380) || (x<=420 && x>=125) && y==300 || (x<=320 && x>=230) && y==380 || x==230 && (y>=380 && y<=460) || (x<=230 && x>=2) && y==460 || x==125 && (y>=300 && y<=370) || (x<=125 && x>=105) && y==370 || x==2 && (y<=460 && y>=180) || x==105 && (y<=370 && y>=300) || (x>=2 && x<=450) && y==180 || x==105 && (y<=300 && y>=250) || (x>=105 && x<=550) && y==250 || x==550 && (y<=250 && y>=20) || (x<=550 && x>=275) && y==20 || x==450 && (y<=180 && y>=100) || (x<=450 && x>=360) && y==100 || x==360 && (y>=100 && y<=160) || (x<=360 && x>=10) && y==160 || (x<=360 && x>=10) && y==160 || x==10 && (y<=160 && y>=30) || x==275 && (y>=20 && y<=90) || (x<=275 && x>=110) && y==90 || x==110 && (y<=90 && y>=30) ) { x=560; y=410; carspeed=0.1; repaint(); } else { g2d.drawImage(car,x,y,this); } g2d.setTransform(origXform); if(x<=95 && x>=15 && y<=27 && y>=5) { g.setColor(Color.WHITE); g.setFont(new Font("ArialBlack",Font.BOLD,25)); g.drawString("YOU WON!!",240,240); } g.setColor(Color.WHITE); g.drawLine(640,380,420,380); g.drawLine(640,460,320,460); g.drawLine(420,380,420,300); g.drawLine(320,460,320,380); g.drawLine(420,300,125,300); g.drawLine(320,380,230,380); g.drawLine(230,380,230,460); g.drawLine(230,460,2,460); g.drawLine(125,300,125,370); g.drawLine(125,370,105,370); g.drawLine(2,460,2,180); g.drawLine(105,370,105,300); g.drawLine(2,180,450,180); g.drawLine(105,300,105,250); g.drawLine(105,250,550,250); g.drawLine(550,250,550,20); g.drawLine(550,20,275,20); g.drawLine(450,180,450,100); g.drawLine(450,100,360,100); g.drawLine(360,100,360,160); g.drawLine(360,160,10,160); g.drawLine(10,160,10,30); g.drawLine(275,20,275,90); g.drawLine(275,90,110,90); g.drawLine(110,90,110,30); g.fillRect(15,5,10,10); g.fillRect(35,5,10,10); g.fillRect(55,5,10,10); g.fillRect(75,5,10,10); g.fillRect(95,5,10,10); g.fillRect(25,16,10,10); g.fillRect(45,16,10,10); g.fillRect(65,16,10,10); g.fillRect(85,16,10,10); g.fillRect(15,27,10,10); g.fillRect(35,27,10,10); g.fillRect(55,27,10,10); g.fillRect(75,27,10,10); g.fillRect(95,27,10,10); repaint(); } class adapter extends KeyAdapter { public void keyPressed(KeyEvent e) { /*int key; key=e.getKeyCode(); if(key==KeyEvent.VK_UP || key==KeyEvent.VK_RIGHT) { currentAngle++; carspeed+=0.1; hangle=Math.toRadians(currentAngle); vangle=Math.toRadians(currentAngle); hspeed=(carspeed)*Math.cos(hangle); vspeed=(carspeed)*Math.sin(vangle); x = x - (int) hspeed; y = y - (int) vspeed; repaint(); }*/ switch(e.getKeyCode()) { case KeyEvent.VK_LEFT: currentAngle--; repaint(); break; case KeyEvent.VK_RIGHT: currentAngle++; repaint(); break; case KeyEvent.VK_UP: carspeed+=0.1; if(carspeed>=5.0) { carspeed-=0.1; } hangle=Math.toRadians(currentAngle); vangle=Math.toRadians(currentAngle); hspeed=(carspeed)*Math.cos(hangle); vspeed=(carspeed)*Math.sin(vangle); x = x - (int) hspeed; y = y - (int) vspeed; repaint(); break; case KeyEvent.VK_DOWN: carspeed-=0.1; if(carspeed<=5.0) { carspeed+=0.1; } hangle=Math.toRadians(currentAngle); vangle=Math.toRadians(currentAngle); hspeed=(carspeed)*Math.cos(hangle); vspeed=(carspeed)*Math.sin(vangle); x = x + (int)hspeed; y = y + (int)vspeed; repaint(); break; } } } public static void main(String[]args) { RaceGame race=new RaceGame(); race.CreateBase(); //race.setDoubleBuffered(true); } class GameMain extends JComponent implements ActionListener { JFrame frame2; JOptionPane dialog1,dialog2; JButton button; JMenuItem m1; JMenuItem m2; JMenuItem m3; Image background=getImage("background.jpeg"); public void MainBase() { frame2=new JFrame("Dare 2 Race"); frame2.setBounds(70,30,650,500); frame2.setVisible(true); frame2.setDefaultCloseOperation(frame2.EXIT_ON_CLOSE); Container c2=frame2.getContentPane(); c2.setLayout(null); c2.add(new GameMain()); button=new JButton("Play"); button.setBounds(280,300,80,40); button.addActionListener(this); frame2.add(button); JMenuBar menu=new JMenuBar(); JMenu game=new JMenu("Game"); JMenu htp=new JMenu("Instruction"); JMenu abtus=new JMenu("Help"); menu.add(game); menu.add(htp); menu.add(abtus); m1=new JMenuItem("Exit"); m1.addActionListener(this); m2=new JMenuItem("How To Play"); m2.addActionListener(this); m3=new JMenuItem("About Us"); m3.addActionListener(this); game.add(m1); htp.add(m2); abtus.add(m3); frame2.setJMenuBar(menu); } public void actionPerformed(ActionEvent e) { if(e.getSource()==button) { frame2.setVisible(false); frame.setVisible(true); } else if(e.getSource()==m1) { System.exit(0); } else if(e.getSource()==m2) { dialog1.showMessageDialog(null,"1. Drive the car using Up,Down,Left,Right arrow keys\n"+"\nUp-for acceleration\n"+"Down-for Brake/Reverse\n"+"Right-for steer right\n"+"Left-for steer left\n"+"\n 2. Avoid hitting road\n"+"\n 3. Try to reach finish line quickly to WIN!!","How To Play",JOptionPane.INFORMATION_MESSAGE); } else if(e.getSource()==m3) { dialog2.showMessageDialog(null,"***Developed by Babar,Ehsan & Sidra***\n\n"+" Arid-UIIT\n"+"\n\n Copyrights: All rights reserved","About Us",JOptionPane.INFORMATION_MESSAGE); } } public void paint(Graphics g) { super.paint(g); //Graphics2D g2d=(Graphics2D)g; g.drawImage(background,12,10,this); g.setColor(Color.red); g.drawLine(300,100,100,300); g.fillRect(100,100,200,100); repaint(); } } }
- 06-16-2010, 06:08 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
This looks very familar...
Problem in rotating and moving image at the same time - Java Programming Forums
- 06-16-2010, 09:48 PM #3
Not sure its possible. Add a println() to all the key listeners and you'll see when the system calls your methods.
- 06-16-2010, 10:27 PM #4
Similar Threads
-
Moving an image on a JFrame
By jinkazama in forum AWT / SwingReplies: 3Last Post: 06-04-2010, 12:05 AM -
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 -
Rotating and flipping an image in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:01 PM -
moving image - PROBLEM
By Triss in forum New To JavaReplies: 3Last Post: 01-17-2008, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks