Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: am i using the right structure for applet?

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default am i using the right structure for applet?

    let me try again by posting my full code this time.

    1st - i am using thread in this applet, but should i delet Thread and use Timer?
    - or should i keep thread, run method and also use Timer method?
    2nd - this is the hardest question, still cant understand the jump idea using gravity for some reason. here is what i understand.....

    main variable i would need to make my player jump are:
    gravity which i understand, velocity_Y wich i understand but i cant seem to put them together in code.

    int y; //player y position
    int gravity = 2;
    int velocity_y = -10;
    loop start
    if player jump
    velocity_y = velocity_y + gravity //valocity 1st time -8
    y = y + velocity_y //y = y - 8 //so when neg velo go up, when pos velocity go down
    else player let go of jump button
    velocity_y = velocity_y + gravity
    y = y + velocity_y
    if y+30 > g.getY() //g.getY() = y posistion of ground ---- add 30 bc of player full height
    y = g.get() - 30 // sub 30 his feet are on ground and not head
    velocity_y = 0 //on ground so no velocity_y
    loop end





    my full code that iam been working on for a week now.
    i have 3 classes: Main.java, Ground.java, Player.java

    Main.java
    public class Main extends Applet implements Runnable, KeyListener //TimerListener
    {
    	//Double buffering variables
    	Image dbImage;
    	Graphics dbGraphics;
     
    	Thread thread;
    	Timer timer;
     
    	//Class variables
    	Ground ground_class;
    	Player player_class;
    	Platform platform_class;
    	Shoot shoot_class;
    	Enemy  enemy_class;
     
     
     
    	/*** init method ***/
    	@Override
    	public void init() 
    	{
    		setSize(900, 400);
    		addKeyListener(this);
    	}/*** end of init method ***/
     
     
     
     
    	/*** start method ***/
    	@Override
    	public void start()
    	{
    		ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height
    		player_class = new Player(10, 352);  //x, y
    		platform_class = new Platform();
    		shoot_class = new Shoot();
    		enemy_class = new Enemy(500, 345);   //x, y
     
    		thread = new Thread(this); //start run method
    		thread.start();
    		//timer = new Timer(1000, this); //1 sec
    		//timer.start();
    		//void stop()
    		//void setDelay(int delay)
    		//boolean isRunning()
    		//int getDelay
    	}/*** end of start method ***/
     
     
     
    	/*** run method ***/
    	public void run()
    	{
    		while(true) //player moves
    		{
    			ground_class.update(this, player_class);                         //ground + player collions
    			player_class.PLAYER_MOVE(this, ground_class);                                  //player moves
    			platform_class.update(this, player_class);
    			shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class);  //player shoot bullet moves
    			player_class.PLAYER_ENEMY_COLLISION(this, enemy_class);          //player + enemy collision
     
    			repaint();
    			try
    			{
    				Thread.sleep(10);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}/*** end of run method ***/
     
     
     
     
     
     
     
    	/*** update method ***/
    	@Override
    	public void update(Graphics g) 
    	{
    		if(dbImage == null) //if image is empty than create new image
    		{
    			dbImage = createImage(this.getSize().width, this.getSize().height);
    			dbGraphics = dbImage.getGraphics();
    		}
    		dbGraphics.setColor(getBackground());  //set the background color
    		dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
    		dbGraphics.setColor(getForeground());
    		paint(dbGraphics);     //call paint method
    		g.drawImage(dbImage, 0, 0, this);
    	}/*** end of update method ***/
     
     
     
     
     
     
    	/*** paint method ***/
    	@Override
    	public void paint(Graphics g) 
    	{
    		ground_class.paint(g);             //call paint method from Ground class  //draw ground than player on top
    		player_class.paint(g);             //call paint method from Player classr 
    		platform_class.paint(g);
    		shoot_class.paint(g, player_class); //draw bullet
    		if(enemy_class.isDEAD() == false)  //draw enemy if it's not dead
    		{
    			enemy_class.paint(g);
    		}
     
    	}/*** end of paint method ***/
     
     
     
     
     
     
    	/*** stop method ***/
    	@Override
    	public void stop()
    	{
     
    	}/*** end of stop method ***/
     
    	/*** destroy method ***/
    	@Override
    	public void destroy()
    	{
     
    	}/*** end of destroy method ***/
     
     
     
     
     
     
     
     
     
     
     
     
     
    	/************** key *********************/
    	@Override
    	public void keyPressed(KeyEvent e)
    	{
    		int keys = e.getKeyCode();
    		if(keys == KeyEvent.VK_RIGHT) 
    		{
    			player_class.hitRIGHT();
    		}
    		else if(keys == KeyEvent.VK_LEFT)
    		{
    			player_class.hitLEFT();
    		}	
    		else if (keys == KeyEvent.VK_UP)
    		{
    			player_class.hitJUMP();
    		}
    		else if(keys == KeyEvent.VK_SPACE)
    		{
    			shoot_class.hitSHOOT();
    		}
    	}
    	@Override
    	public void keyReleased(KeyEvent e)
    	{
    		int keys = e.getKeyCode();
    		if(keys == KeyEvent.VK_RIGHT) 
    		{
    			player_class.stopRIGHT();
    		}
    		else if(keys == KeyEvent.VK_LEFT)
    		{
    			player_class.stopLEFT();
    		}	
    		else if (keys == KeyEvent.VK_UP)
    		{
    			player_class.stopJUMP();
    		}
    		else if(keys == KeyEvent.VK_SPACE)
    		{
    			shoot_class.stopSHOOT();
    		}
    	}
    	@Override
    	public void keyTyped(KeyEvent e){}
     
    	/************    MOUSE  **********/
    	public void mousePressed(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    	public void mouseClicked(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    }


    Player.java class
    public class Player 
    {
    	//Player movement variable
    	private int sx = 10;
    	private int sy = 352;
    	private int x;     //current x
    	private int y;     //current y
    	private int dx = 1;    //speed of player
     
     
    	private int gravity = 2;    
    	private int velocity_Y = 10;  //speed of jump
     
    	private boolean dead = false;
     
    	private boolean look_right = true;
    	private boolean walk_right;
    	private boolean walk_left;
    	private boolean jump = false;
    	private boolean jump_lock = false;
    	private boolean fall = false;                  
    	private boolean hitSPACE = false;
     
    	/*** image variable ***/
    	private static ImageIcon player_walk_right = new ImageIcon("Image/player/player_walk_right.gif");
    	private static ImageIcon player_walk_left = new ImageIcon("Image/player/player_walk_left.gif");
    	private static ImageIcon player_stand_right = new ImageIcon("Image/player/player_stand_right.gif");
    	private static ImageIcon player_stand_left = new ImageIcon("Image/player/player_stand_left.gif");
    	private static ImageIcon player_jump_right = new ImageIcon("Image/player/player_jump_right.gif");
    	private static ImageIcon player_jump_left = new ImageIcon("Image/player/player_jump_left.gif");
     
     
     
    	/*** constructor Method ***/
    	public Player()
    	{
    	}
    	/*** constructor Method2 ***/
    	public Player(int ix, int iy) 
    	{
    		x = ix;
    		y = iy;
    	}	
     
    		/*** get/set method ***/
    		//need these to draw image's
    		public static Image get_player_walk_right()  
    		{ return player_walk_right.getImage(); }
    		public static Image get_player_walk_left()  
    		{ return player_walk_left.getImage(); }
    		public static Image get_player_stand_right()  
    		{ return player_stand_right.getImage(); }
    		public static Image get_player_stand_left()  
    		{ return player_stand_left.getImage(); }
    		public static Image get_player_jump_right()  
    		{ return player_jump_right.getImage(); }
    		public static Image get_player_jump_left()  
    		{ return player_jump_left.getImage(); }
     
    		public boolean isDEAD()
    		{ return dead; }
    		public void setDEAD(boolean value) 
    		{ this.dead =  value; }
     
    		public int getX()
    		{ return x; }
    		public void setX(int value) 
    		{ this.x =  value; }
     
    		public int getY()
    		{ return y; }
    		public void setY(int value) 
    		{ this.y =  value; }
     
    		public int getDX()
    		{ return dx; }
    		public void setDX(int value) 
    		{ this.dx =  value; }
     
    		//public int getDY()
    		//{ //return dy; }
    	    //public void setDY(int value) 
    		//{ this.dy =  value; }
     
     
     
    		/*** main key methods ***/
    		/*** RIGHT ***/
    		public void hitRIGHT()  //if user hit right button
    		{ 
    			walk_right = true; 
    			look_right = true;
    		}
    		public void stopRIGHT() //if user let go of right button
    		{
    			walk_right = false;  //stop 
    			look_right = true;
    		}
     
    		/*** LEFT ***/
    		public void hitLEFT()  //move left
    		{
    			walk_left = true;
    			look_right = false;
    		}
    		public void stopLEFT()
    		{
    			walk_left = false;  //stop
    			look_right = false;
    		}
     
    		/*** JUMP ***/
    		public void hitJUMP()
    		{
    			jump = true;
    		}
    		public void stopJUMP()
    		{
    			jump = false;
    		}
     
     
     
     
     
    		/**************************/
    		/****** LOOP METHODS ******/
    		/**************************/
    		/*** player move method ***/
    		//loop method
    		[B]public void PLAYER_MOVE(Main m, Ground g) //player move's
    		{
    			//always same (Main m)
    			//loop - collion check here 
    			if(walk_right == true)
    			{
    					x += dx;
    			}
    			else if(walk_right == false)
    			{
    			}
     
    			if(walk_left == true)  //walking left
    			{
    				if(x - dx < 0)  //cant go left
    				{
    					x = 0;
    				}
    				else
    				{
    					x -= dx;      //move left
    				}
    			}
    			else if(walk_left == false)
    			{
    			}
     
     
    			if(jump == true) //jump && not in air
    			{
    				velocity_Y += gravity;
    				y -= velocity_Y;	
    			}
     
    			else if(jump == false) 
    			{
    				velocity_Y -= gravity; //dont think i need this
    				y -= velocity_Y;
    				if(y+30 > g.getY())  //if player hit ground(+30 for player height)
    				{
    					y = g.getY()-30; //set player y pos (-30 for height)      
    					velocity_Y = 0; //hit ground
    				}
    			}[/B]
    		}/***end of update method ***/
     
     
     
     
     
     
     
     
     
    		/*** player + enemy collion ***/
    		public void PLAYER_ENEMY_COLLISION(Main m, Enemy e)
    		{
     
    		}
    		/*** end of PlAYER_ENEMY_COLLISION METHOD ***/
     
     
     
     
     
     
    		/*** paint method ***/
    		public void paint(Graphics g)
    		{
    			if(jump == false)
    			{
    				if(look_right == true)
    					{
    						if(walk_right == true)
    						{
    							g.drawImage(this.get_player_walk_right(), x, y, null);
    						}
    						else if(walk_right == false)
    						{
    							g.drawImage(this.get_player_stand_right(), x, y, null);
    						}
    					}
    				else if(look_right == false)
    				{
    					if(walk_left == true)
    						{
    							g.drawImage(this.get_player_walk_left(), x, y, null);
    						}
    					else if(walk_left == false)
    					{
    						g.drawImage(this.get_player_stand_left(), x, y, null);
    					}
    				}	
    			}
    			else if(jump == true)
    			{
    				if (look_right == true)
    				{
    					g.drawImage(this.get_player_jump_right(), x, y, null);
    				}
    				else if(look_right == false)
    				{
    					g.drawImage(this.get_player_jump_left(), x, y, null);
    				}
    			}
    		}
    }/*** end of class ***/


    Ground.java
    public class Ground 
    {
    	private int x;
    	private int y;
    	private int width;
    	private int height;
     
    	private static ImageIcon ground_image = new ImageIcon("Image/ground/ground.gif");
     
     
    	/*** constructor Method ***/
    	public Ground(int ix, int iy, int iw, int ih) 
    	{
    		x = ix;
    		y = iy;
    		width = iw;
    		height = ih;
    	}	
     
     
    	/*** get/set method ***/
    	public static Image getGIMAGE()  
    	{ return ground_image.getImage(); }
     
    	public int getX()
    	{ return x; }
    	public void setX(int value) 
    	{ this.x =  value; }
     
    	public int getY()
    	{ return y; }
    	public void setY(int value) 
    	{ this.y =  value; }
     
    	public int getWdth()
    	{ return width; }
    	public void setWidth(int value) 
    	{ this.width =  value; }
     
    	public int getHiehgt()
    	{ return height; }
    	public void setHeight(int value) 
    	{ this.height =  value; }
     
     
     
     
    	//player Collisions on ground
    	//loop method
    	public void update(Main m, Player p)
    	{
    		int playerX = p.getX();
    		int playerY = p.getY();
     
    		//playerx > x   so every thing after platform_top_left
    		/*System.out.println("playerX="+playerX+"-"+"playerY="+playerY+"----x="+x+"-y="+y);
    		if(playerY > y) //y+height  |  | left right of rect
    		{
    			p.setY(y-30);
    		}*/
    	}/*** end of update method ***/
     
     
     
    	/*** paint method ***/
    	public void paint(Graphics g) 
    	{
    		g.drawImage(this.getGIMAGE(), x, y, width, height, null); 
    	}/*** paint method ***/
    }
    Last edited by hwoarang69; October 23rd, 2012 at 03:00 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: am i using the right structure for applet?

    Using a Timer is probably the way to go, until you know for sure you need to use threading.

    Please keep your question on jumping contained to a single thread. You're making it harder to keep track of what's already been said, which will make it harder for people to help you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    hwoarang69 (October 23rd, 2012)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: am i using the right structure for applet?

    ic and i am getting a error when i use timer on

    timer = new Timmer(1000, this);


    public class Main extends Applet implements TimerListener, KeyListener
    {
    Timer timer();
    int delay = 1000;

    public void start()
    {
    timer = new Timer(int delay ,this);
    timer.start();
    }


    public void run()
    {
    //user tiemr loop here??
    }

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: am i using the right structure for applet?

    What is the exact error? Take a look at the syntax.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: am i using the right structure for applet?

    ... ok, forget every thing i said. just answer one question.

    when i hold up key my player keep on going up for every. only thing for past 1 week i am trying is to limit my player jump.

    this is the code in player.java i have it on top.
    if(jump == true) //jump && not in air
    {
    velocity_Y += gravity;
    y -= velocity_Y;
    }

    else if(jump == false)
    {
    velocity_Y -= gravity; //dont think i need this
    y -= velocity_Y;
    if(y+30 > g.getY()) //if player hit ground(+30 for player height)
    {
    y = g.getY()-30; //set player y pos (-30 for height)
    velocity_Y = 0; //hit ground
    }
    }

    how can i use this code and limit his jump. i just want to get a idea so i can code it.

    i am thinking in my if statment
    if(jump == true)
    i can have another if statment which will limit my player y postion. for ex:
    if(playerY == 40) so that player y cant 40 pix.
    but with this another problem will be what if i jump on platform but my will never every be able to height than 40 pix
    Last edited by hwoarang69; October 23rd, 2012 at 06:09 PM.

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: am i using the right structure for applet?

    else if (keys == KeyEvent.VK_UP)
    {
    player_class.hitJUMP();
    }
    So far so good. Pressing up arrow triggers the jump method when the key is pressed down. This is all that is needed. The release of the space bar is irrelevant.





    /*** JUMP ***/
    public void hitJUMP()
    {
    jump = true;
    }
    public void stopJUMP()
    {
    jump = false;
    }
    Using this method is the cause of your ever-jumping character while holding the space bar. It is how to code it when you want to be able to fly maybe... Not what you want. The code should look something closer to:
    public void hitJump() {
       if(player.isOnGround || player.isAllowedDoubleJump) {
          player.speedY += -10;// -10 to go with examples given on previous threads, use what ever works for you
       }
    }





    public void PLAYER_MOVE
    This is the method that should (and looks like it does) get called from the game loop. In this method is where speedY will determine how high you jump.




    if(jump == true)
    This conditional will not be necessary when the movement code is complete. I will try to explain how to set it up. The player has a position, that is an x and y coordinate used to locate itself. The player has a speed, that is a speedX and a speedY which determines how far the player will move each time the method is called. The speedX will be determined by if an arrow key is held down. The jump will not. Gravity will always pull down the player. So every time the method is called gravity is applied to the y axis. The code may look like this:
    player.posY += level.gravity;
    But wait. That is not all. In order to properly get the position we must consider the player's y speed too. The code may look like this:
    player.posY += player.speedY + level.gravity;
    With this setup, every time the method is called gravity attempts to move the player downward by level.gravity amount. If jump has been pressed, and jumping was allowed (in this sample either of two ways), speedY gets set to -10.
    With gravity (for example) set to 2.0, and speedY set to -10.0, that means the player will move deltaY which is -8 on the y axis. speedY is updated to -8 since gravity has slowed the ascent by 2.
    Next time the method is called speedY will have dropped to the -8 from before, and added to gravity again, leaving deltaY at -6 this time. Player moves in y by -6 and speedY is set to -6.
    The third call during the jump cycle will take speedY (-6) and gravity (2) and move the player deltaY(-4). Again update speedY to -4.
    Fourth call, speedY(-4) and gravity (2) , making deltaY(-2).

    Do you see how gravity diminishes speedY over time? This has nothing to do with when the space bar is released. It has to do with the values speedY and gravity and how they change the player's position in a natural manner as the method is called. There is no need to predict how high the player will go before starting to plane off, other than to get the boost set to adjust the height. The boost as I call it is adding the value of -10 to speedY (as in this example)

  8. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 23rd, 2012)

  9. #7
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: am i using the right structure for applet?

    thanks alot man!! finally got it to working

  10. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: am i using the right structure for applet?

    I am glad you got it working.

    Be a kind soul and mark the thread as solved
    refer to the announcements page if you do not yet know how
    good luck on the next project

  11. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 24th, 2012)

Similar Threads

  1. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  2. Re: Data Structure
    By jim17 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 16th, 2011, 11:14 PM
  3. HI can some one tell me the size of the below structure.
    By sucheth13 in forum Java Native Interface
    Replies: 1
    Last Post: March 11th, 2011, 03:08 AM
  4. .xls data structure
    By helloworld922 in forum JDBC & Databases
    Replies: 3
    Last Post: August 20th, 2009, 07:12 PM