11 Replies - 1332 Views - Last Post: 07 May 2012 - 05:21 PM Rate Topic: -----

#1 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Java Applet - Lag when beginning movement?

Posted 03 May 2012 - 04:38 PM

Hi. As you may have read in the title, I have a simple applet in which two squares move around. This is my first attempt at using swing timers to move things, so it probably isn't right and can be shortened and made simpler. It's pretty messy right now.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Timer;

public class Main extends Applet implements Runnable, ActionListener {
	private static final long serialVersionUID = 1L;
	int p1x = 10, p1y = 10, p2x = 570, p2y = 580;
	boolean p1u = false, p1d = false, p1l = false,p1r = true,
	p2u = false, p2d = false, p2r = false, p2l = true;
	
	
	public void init() {
		addKeyListener(hello);
		setFocusable(true);
		
	}
		
	Timer repainting = new Timer(1/10, new ActionListener() {
        public void actionPerformed(ActionEvent evx) {
            repaint();
        }
     });
	
			Timer p1up = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		            p1y -= 2;
		        }
		     });
			Timer p1left = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		            p1x -= 2;
		        }
		     });
			Timer p1right = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		            p1x += 2;
		        }
		     });
			Timer p1down = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		            p1y += 2;
		        }
		     });
			Timer p2up = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		        	p2y -= 2;
		        }
		     });
			Timer p2left = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		        	p2x -= 2;
		        }
		     });
			Timer p2right = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		        	p2x += 2;
		        }
		     });
			Timer p2down = new Timer(1, new ActionListener() {
		        public void actionPerformed(ActionEvent evx) {
		        	p2y += 2;
		        }
		     });
	
	public void paint(Graphics g) {
		//Players
		g.fillRect(p1x, p1y, 15, 15);
		g.fillRect(p2x, p2y, 15, 15);
		
	}
	
	public KeyListener hello = new KeyListener() {
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_W) {
				p1up.start();
				repaint();
			}
			if (key == KeyEvent.VK_A) {
				p1left.start();
				repaint();
			}
			if (key == KeyEvent.VK_S) {
				p1down.start();
				repaint();
			}
			if (key == KeyEvent.VK_D) {
				p1right.start();
				repaint();
			}
			if (key == KeyEvent.VK_UP) {
				p2up.start();
				repaint();
			}
			if (key == KeyEvent.VK_LEFT) {
				p2left.start();
				repaint();
			}
			if (key == KeyEvent.VK_DOWN) {
				p2down.start();
				repaint();
			}
			if (key == KeyEvent.VK_RIGHT) {
				p2right.start();
				repaint();
			}
			}
		
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_W) {
				p1up.stop();
				repaint();
			}
			if (key == KeyEvent.VK_A) {
				p1left.stop();
				repaint();
			}
			if (key == KeyEvent.VK_S) {
				p1down.stop();
				repaint();
			}
			if (key == KeyEvent.VK_D) {
				p1right.stop();
				repaint();
			}
			if (key == KeyEvent.VK_UP) {
				p2up.stop();
				repaint();
			}
			if (key == KeyEvent.VK_LEFT) {
				p2left.stop();
				repaint();
			}
			if (key == KeyEvent.VK_DOWN) {
				p2down.stop();
				repaint();
			}
			if (key == KeyEvent.VK_RIGHT) {
				p2right.stop();
				repaint();
			}
			}
		
		@SuppressWarnings("unused")
		public void keyTyped(KeyEvent e) {
			int key = e.getKeyCode();
			repaint();
		}
	};
	
	public void start(){}
	public void run(){}
	public void stop(){}
	public void destroy(){}

	public void actionPerformed1(ActionEvent arg0) {
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		
	}

}


The problem is that when I try to move a square, it lags for about a second and then starts moving. I'm pretty sure this isn't the same issue as the windows keyboard thing, where if you hold for example "f" down, it writes one "f", then writes them rapidly. Thanks for any help or feedback provided. Also, is there a way I can do this with floats?

Is This A Good Question/Topic? 0
  • +

Replies To: Java Applet - Lag when beginning movement?

#2 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Lag when beginning movement?

Posted 03 May 2012 - 05:26 PM

A few things:
- first setting a repaint() timer at less than 20 milliseconds is completly useless
A CRT screen is refresh 60 times per second so every 16.666 milli. Setting the timer at 1 milli will make the drawing happens 16 times before thre screen is actually refresh
And the actual repaint() will be done when no timer are running thus the lag

- second you should use only one Timer. This is more than enough and will cut the overhead
use an instance variable directionToUpdate and set it to 1,2,3,4,5 depending of the key in the keyListener
In the actionPerformed() of your single Timer switch() on directionToUpdate and update the good pX1 something like



    if (key == KeyEvent.VK_A) {
         directionTuUpdate = 1;
          return;
    }
    if (key == KeyEvent.VK_S) {
         directionTuUpdate = 2;
          return;
    }


public void actionPerformed(ActionEvent e) {
   switch(directionToUpdate) {
      case 0:
        return;       // do nothing
      case 1:
        p1y -= 2;
        break;
      case 2:
        p1x -= 2;
        break;
      ....
    } // end switch()
    repaint();
}



Happy coding
Was This Post Helpful? 1
  • +
  • -

#3 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Lag when beginning movement?

Posted 03 May 2012 - 10:00 PM

I tried the method you suggested and changed the repaint timer to 20 milliseconds, but now when I press a key (Such as "W"), nothing happens. I don't think I did it right though. Should directionToUpdate be an int? Or what do you mean by an instance variable. Sorry, I'm still learning. This is my new code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Timer;

public class Main extends Applet implements Runnable, ActionListener {
	private static final long serialVersionUID = 1L;
	int p1x = 10, p1y = 10, p2x = 570, p2y = 580;
	boolean p1u = false, p1d = false, p1l = false,p1r = true,
	p2u = false, p2d = false, p2r = false, p2l = true;
		
	int directionToUpdate;
	
	public void init() {
		addKeyListener(hello);
		setFocusable(true);
		
	}
		
	Timer repainting = new Timer(20, new ActionListener() {
        public void actionPerformed(ActionEvent evx) {
            repaint();
        }
     });
	
	public void paint(Graphics g) {
		//Players
		g.fillRect(p1x, p1y, 15, 15);
		g.fillRect(p2x, p2y, 15, 15);
		
	}
	
	public KeyListener hello = new KeyListener() {
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_W) {
				directionToUpdate = 1;
				repaint();
			}
			if (key == KeyEvent.VK_A) {
				directionToUpdate = 2;
				repaint();
			}
			if (key == KeyEvent.VK_S) {
				directionToUpdate = 3;
				repaint();
			}
			if (key == KeyEvent.VK_D) {
				directionToUpdate = 4;
				repaint();
			}
			if (key == KeyEvent.VK_UP) {
				directionToUpdate = 11;
				repaint();
			}
			if (key == KeyEvent.VK_LEFT) {
				directionToUpdate = 12;
				repaint();
			}
			if (key == KeyEvent.VK_DOWN) {
				directionToUpdate = 13;
				repaint();
			}
			if (key == KeyEvent.VK_RIGHT) {
				directionToUpdate = 14;
				repaint();
			}
			}
		
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			
		}
		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
			};
		
		@SuppressWarnings("unused")
		public void keyTyped(KeyEvent e) {
			int key = e.getKeyCode();
			repaint();
		};
	
	public void start(){}
	public void run(){}
	public void stop(){}
	public void destroy(){}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		switch(directionToUpdate) {
		      case 0:
		        return;
		      	case 1:
		      		p1y -= 2;
		        	break;	
		        
		      	case 2:		
		      		p1x -= 2;
		      		break;
		       
		      	case 3:
		      		p1y += 2;
		      		break;
		      	case 4:
		      		p1x += 2;
		      		break;
		      	case 11:
		      		p2y -= 2;
		      		break;
		      	case 12:
		      		p2x -= 2;
		      		break;
		      	case 13:
		      		p2y += 2;
		      		break;
		      	case 14:
		      		p2x +=2;
		    }
		    repaint();
	}
}



Thanks for any help or advice.
Was This Post Helpful? 0
  • +
  • -

#4 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Lag when beginning movement?

Posted 06 May 2012 - 06:04 AM

Bump? It's been two days.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Lag when beginning movement?

Posted 06 May 2012 - 10:42 AM

You have an extra keyTyped(). The compiler complained about it and to fic it you added a @suppress(warning). When something like that happens, ask yourself why this happen instead of just trying to hide the problem.

You added a second actionPerformed() where you put the switch(). The switch should be in the actionPerformed() of the Timer.

You created the Timer as an instance variable but never start it (woulkd be clearer if it's declaration was with the other instance variables, I moved it up before the init() method... but this is just for esthetical raisons

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Timer;

public class Main extends Applet implements Runnable, ActionListener {
	private static final long serialVersionUID = 1L;
	int p1x = 10, p1y = 10, p2x = 570, p2y = 580;

	int directionToUpdate = 0;

	Timer repainting = new Timer(20, new ActionListener() {
		public void actionPerformed(ActionEvent evx) {
			switch(directionToUpdate) {
			case 0:
				return;
			case 1:
				p1y -= 2;
				break;	

			case 2:		
				p1x -= 2;
				break;

			case 3:
				p1y += 2;
				break;
			case 4:
				p1x += 2;
				break;
			case 11:
				p2y -= 2;
				break;
			case 12:
				p2x -= 2;
				break;
			case 13:
				p2y += 2;
				break;
			case 14:
				p2x +=2;
			}
			repaint();
		}
	});


	public void init() {
		addKeyListener(hello);
		setFocusable(true);
		repainting.start();
	}

	public void paint(Graphics g) {
		//Players
		System.out.printf("p1x: %d p1Y: %d p2x: %d p2y: %d\n", p1x, p1y, p2x, p2y);
		g.fillRect(p1x, p1y, 15, 15);
		g.fillRect(p2x, p2y, 15, 15);

	}

	public KeyListener hello = new KeyListener() {
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_W) {
				directionToUpdate = 1;
			}
			if (key == KeyEvent.VK_A) {
				directionToUpdate = 2;
			}
			if (key == KeyEvent.VK_S) {
				directionToUpdate = 3;
			}
			if (key == KeyEvent.VK_D) {
				directionToUpdate = 4;
			}
			if (key == KeyEvent.VK_UP) {
				directionToUpdate = 11;
			}
			if (key == KeyEvent.VK_LEFT) {
				directionToUpdate = 12;
			}
			if (key == KeyEvent.VK_DOWN) {
				directionToUpdate = 13;
			}
			if (key == KeyEvent.VK_RIGHT) {
				directionToUpdate = 14;
			}
			System.out.println("direction to update: " + directionToUpdate);
		}

		@Override
		public void keyReleased(KeyEvent e) {
		}
		@Override
		public void keyTyped(KeyEvent arg0) {
		}
	};


	public void start(){}
	public void run(){}
	public void stop(){}
	public void destroy(){}

	@Override
	public void actionPerformed(ActionEvent e) {

	}
}




Was This Post Helpful? 1
  • +
  • -

#6 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Lag when beginning movement?

Posted 06 May 2012 - 05:31 PM

Ok, I have done what you told, and I tried not to make any simple errors this time. But unfortunately now, I can only have one movement at a time? Would I create another directionToUpdate for the other square?

EDIT: I tried making another directionToUpdate for the second square, and it works, but only if I'm pressing one the the keys for the first square. Here is my code:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Timer;

public class Main extends Applet implements Runnable, ActionListener {
	private static final long serialVersionUID = 1L;
	int p1x = 10, p1y = 10, p2x = 570, p2y = 580;
	boolean p1u = false, p1d = false, p1l = false,p1r = true,
	p2u = false, p2d = false, p2r = false, p2l = true;
		
	int directionToUpdate, directionToUpdate2;
	
	public void init() {
		addKeyListener(hello);
		setFocusable(true);
		repainting.start();
	}
		
	Timer repainting = new Timer(20, new ActionListener() {
        public void actionPerformed(ActionEvent evx) {
            repaint();
            
            switch(directionToUpdate) {
		      case 0:
		        return;
		      	case 1:
		      		p1y -= 2;
		        	break;	
		        
		      	case 2:		
		      		p1x -= 2;
		      		break;
		       
		      	case 3:
		      		p1y += 2;
		      		break;
		      	case 4:
		      		p1x += 2;
		      		break;
		    }
            
            switch(directionToUpdate2) {
            	case 0:
            		return;
            	case 1:
		      		p2y -= 2;
		      		break;
		      	case 2:
		      		p2x -= 2;
		      		break;
		      	case 3:
		      		p2y += 2;
		      		break;
		      	case 4:
		      		p2x +=2;
		      		break;
            }
        }
     });
	
	public void paint(Graphics g) {
		//Players
		g.fillRect(p1x, p1y, 15, 15);
		g.fillRect(p2x, p2y, 15, 15);
		
	}
	
	public KeyListener hello = new KeyListener() {
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_W) {
				directionToUpdate = 1;
				repaint();
			}
			if (key == KeyEvent.VK_A) {
				directionToUpdate = 2;
				repaint();
			}
			if (key == KeyEvent.VK_S) {
				directionToUpdate = 3;
				repaint();
			}
			if (key == KeyEvent.VK_D) {
				directionToUpdate = 4;
				repaint();
			}
			if (key == KeyEvent.VK_UP) {
				directionToUpdate2 = 1;
				repaint();
			}
			if (key == KeyEvent.VK_LEFT) {
				directionToUpdate2 = 2;
				repaint();
			}
			if (key == KeyEvent.VK_DOWN) {
				directionToUpdate2 = 3;
				repaint();
			}
			if (key == KeyEvent.VK_RIGHT) {
				directionToUpdate2 = 4;
				repaint();
			}
			}
		
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_W) {
				directionToUpdate = 0;
				repaint();
			}
			if (key == KeyEvent.VK_A) {
				directionToUpdate = 0;
				repaint();
			}
			if (key == KeyEvent.VK_S) {
				directionToUpdate = 0;
				repaint();
			}
			if (key == KeyEvent.VK_D) {
				directionToUpdate = 0;
				repaint();
			}
			if (key == KeyEvent.VK_UP) {
				directionToUpdate2 = 0;
				repaint();
			}
			if (key == KeyEvent.VK_LEFT) {
				directionToUpdate2 = 0;
				repaint();
			}
			if (key == KeyEvent.VK_DOWN) {
				directionToUpdate2 = 0;
				repaint();
			}
			if (key == KeyEvent.VK_RIGHT) {
				directionToUpdate2 = 0;
				repaint();
			}
		}

		public void keyTyped(KeyEvent arg0) {}
			};
	
	public void start(){}
	public void run(){}
	public void stop(){}
	public void destroy(){}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}


This post has been edited by hunterb100: 06 May 2012 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Lag when beginning movement?

Posted 06 May 2012 - 05:52 PM

Yes that makes sense.
4 keys for one square and 4 keys for the other one so one directionUpdate for each square
and 2 switch() one on directionUpdate1 and one for directionUpdate2.
Was This Post Helpful? 0
  • +
  • -

#8 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Lag when beginning movement?

Posted 06 May 2012 - 08:06 PM

Yes, but my problem - as stated in my last reply - is that the second square only moves if the first square is moving as well.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Lag when beginning movement?

Posted 06 May 2012 - 08:18 PM

What can I say ? You didn't fix it correctly.
Re-post your code
Was This Post Helpful? 0
  • +
  • -

#10 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Lag when beginning movement?

Posted 07 May 2012 - 03:24 PM

The code in my last post is my most recent code. I can't seem to figure it out though, and I don't see what the problem is.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Lag when beginning movement?

Posted 07 May 2012 - 04:55 PM

Now that you have 2 switch() to test

           switch(directionToUpdate) {
		      case 0:
		        return;   // noting to do exit the method


should be
           switch(directionToUpdate) {
		      case 0:
		        break;   // go to next switch


Was This Post Helpful? 1
  • +
  • -

#12 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Lag when beginning movement?

Posted 07 May 2012 - 05:21 PM

Thank you! I haven't found any glitches yet (There are sure to be!), and I might reply later with something that comes up. Greatly Appreciated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1