7 Replies - 473 Views - Last Post: 06 June 2012 - 05:35 PM Rate Topic: -----

#1 Benkro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-June 12

Image does not move(simple java game)

Posted 04 June 2012 - 01:42 AM

Ok so this is my first question and post in the forum, to start first i want to apologize if i write something wrong because i don't usually write in english but i will do my best(checked the grammar before posting), ok now im trying to start a little game in java, got the background, the character, the neccesary to start at first i just wanted the character to move left and right but the moment i run the code everything looks fine but it doesn't move.

I investigated a bit about the problem and found something about how swing manages threads but got me more confused i founded a couple of vids about java game developing but didn't help much, well i appreciate any help you can give me and thank you for reading.





package Game;

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class plomero {
int x,y,movx;
Image marioderecha;

	public plomero(){
		ImageIcon i = new ImageIcon("C:/bla/bla/bla.gif");
		marioderecha= i.getImage();
		x=10;
		y=250;
	}
	
	public void move(){
		 x += movx;
	}
	
	
	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	public Image getImage(){
		return marioderecha;
	}
	
	public void KeyPressed(KeyEvent e){
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_LEFT){
			movx = -1;
		
		}
			
	
		if (key == KeyEvent.VK_RIGHT){
			movx = 1;
			
		}
			
	
		
	}
	
	public void KeyReleased(KeyEvent e){
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_LEFT){
			movx = 0;
		}
			
		
		if (key == KeyEvent.VK_RIGHT){
			movx = 0;
		}
			
		
	}




 
package Game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class panelG extends JPanel implements ActionListener{
plomero p;
Image img;
Timer time;


public panelG(){
		p= new plomero();
		addKeyListener(new ActionEscuchador());
		setFocusable(true);
		ImageIcon i = new ImageIcon("C:/Users/Benji/Desktop/java/win/bin/Juego/es.jpg");
		img= i.getImage();
		time = new Timer(5, this);
		time.start();
		
	}

	
	public void actionPerformed(ActionEvent e) {

				p.move();
				repaint();
	}
	
	
	public void paint(Graphics g){
		
		super.paint(g);
		g.drawImage(img, 0, 0, this);
		g.drawImage(p.getImage(), p.getX(), p.getY(), this);
		repaint();
	}
	
	private class ActionEscuchador extends KeyAdapter{
		
		public void KeyReleased(KeyEvent e){
			p.KeyReleased(e);
			
		}
		
		public void KeyPressed(KeyEvent e){
			p.KeyPressed(e);
			
		}
		
	}

	
}



package Game;
import javax.swing.*;


public class windowG extends JFrame implements Runnable{

		public void run(){
			setTitle("Game");
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			add(new panelG());
			setSize(800, 500);
			setVisible(true);
			setLocationRelativeTo(null);
		}
		
		
		public static void main(String[] args){
		
				SwingUtilities.invokeLater(new ventanaG());
			 
			    }
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Image does not move(simple java game)

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: Image does not move(simple java game)

Posted 04 June 2012 - 03:31 AM

You have typos in your KeyListener. Java is case sensitive and method names in Java begin lower case. Your methods will therefore never be called.
Was This Post Helpful? 1
  • +
  • -

#3 Benkro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-June 12

Re: Image does not move(simple java game)

Posted 04 June 2012 - 03:50 AM

Thanks a lot Mr.g00se i dont believe the error was that simple, and i was investigating other things like crazy, now i can start working again thanks a lot again.
Was This Post Helpful? 0
  • +
  • -

#4 oha055  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 229
  • Joined: 02-February 09

Re: Image does not move(simple java game)

Posted 04 June 2012 - 05:23 AM

Don't think it will make any difference to your problem, but you shouldn't override the paint method. Override paintComponent() and put all your custom drawing there instead.

Read more

This post has been edited by oha055: 04 June 2012 - 05:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: Image does not move(simple java game)

Posted 04 June 2012 - 05:50 AM

Quote

Override paintComponent()


Yes, that's better. And NEVER call repaint() in there or in paint()
Was This Post Helpful? 0
  • +
  • -

#6 Benkro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-June 12

Re: Image does not move(simple java game)

Posted 04 June 2012 - 10:09 AM

Well guess i need to learn more thanks a lot for your advice i already updated the code with your suggestions, well thanks again mister g00se, and thanks mister oha055

And forgive me if i write something wrong hehehe.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,170
  • Joined: 06-March 08

Re: Image does not move(simple java game)

Posted 04 June 2012 - 11:54 AM

also a Timer with a time of 5 milliseconds is just a waste of resources
A monitor refresh its screen every 16.6 milliseconds so your actual paintComponent() method will draw in memory 3 times before the screen is actually refreshed, so you paint 2 times for nothing in memory

A Timer of less than 20 is kind of useless for most of the animations
Was This Post Helpful? 1
  • +
  • -

#8 Benkro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-June 12

Re: Image does not move(simple java game)

Posted 06 June 2012 - 05:35 PM

Thanks for the advice mister pbl i alredy added it to the code, so much to learn.........
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1