title: Prevent a game object from going off the graphics window in Java
body: <p>I am building a game in Java that involves the user controlling a ship. Problem is, I do not want the user's ship to go off the screen from both sides while not making the ship moving in a jittery manner. </p>
  
  <p>Here's my code:</p>
  
  <pre><code>/* stop the ship if its goes far from the left side 
   * of the graphics window but allow control of
   * the ship if the ship_dx is above 0
   * this applies to the right side of the graphics
   * window
   */
  
  if(ship_dx &lt; 0) {
      // stop the ship 
      ship_velocity = 0;
      ship_dx = 0;
  }
  else if (ship_dx &gt; 530 ) {
      ship_velocity = 0;
      ship_dx = 530;
  } else {
      ship_velocity = 5;
  }
  </code></pre>
  
  <p>The ship does not move off the screen but if I force the ship to move beyond the graphics window, the ship looks jittery since I am hard-coding the constants into my ship_dx.</p>