java - How can I prevent the Ball from moving off of the edge of the screen? simple KeyListenerDemo example -


I have limited experience, especially when it comes to graphics, I have got very good help on this problem so far And I am sure to be more familiar with this site as well as to contribute more. The starter code (below) that works fine as I had tested it in the eclipse. I am using it from a classroom to teach a high school student, according to the instructions given below, will an easy way to extend the simple program in advance so that I can get the ball out of the window edge Preventable from being able to? The ball runs fine with the arrow keys, but I can still move the scene completely out of the window / any easy ways to extend this demo program greatly appreciate. Thank you.

  Import java.awt. *; Import java.awt.event. *; // # 1 import javax.swing. *; / *************************************************************************************************** **************** KeyListenerDemo.java * Provides keyboard input for using the KeyListener interface * * Program 18: Adding some more keystroke commands to this program Increase: * z (VK_Z) - The cause of the ball to jump at a random new place. * S (VCSI) - Make the ball small - its diameter is 1/2 times. * B (VKB) - Make the ball bigger - increase its diameter by 2. times (vk) - Change the color (by any means you want). * * * Also, modify the program to ensure the following: * - The ball goes all the way to the edge of the screen but remains perfectly on the screen *. * - If the double diameter is not fit, then make it as big as possible. * - Make sure the ball never disappears altogether. * *************************************************************************************************** **************************************************************************************************** ************************************* KeyListenerDemo extends JFrame KeyListener // # 2 {// Class Scope Final Personal Stable Final Integer SCREEN_WIDTH = 1000; Personal Static FINAL END SCREEN_HEIGHT = 800; Personal static ending ending START_RADIUS = 25; Personal fixed ending end START_X = 100; Private static final end START_Y = 100; Private stable final IT STEP_SIZE = 10; // class scope variables personal static x = START_X; // Private stationary in the center of the ball = = yY; // y in the center of the ball, the private constant int radius = START_RADIUS; // Line of radius // Method / ** * Create a window and register it as a kernelist * * Ultimate Arguments * / Public Static Wide Men (string [] ARGs) {// Set GeFame window. KeyListenerDemo GP = New KeyListenerDemo (); Gp.setSize (SCREEN_WIDTH, SCREEN_HEIGHT); Gp.setVisible (true); gp.addKeyListener (GP); // # 3 // If this class was a constructor and you created this line in // then this constructor can not refer to this GP because the variable // is local for this method. Instead you will write: // addKeyListener (this); } / ** * is called when a key is pressed first * required for any keyListener * * @param e pressed * / public zero keypressed (KeyEvent e) key // # 4a {int keycode = e.getKeyCode Information about (included); If (keycode == KeyEvent.VK_LEFT) {x = x - STEP_SIZE; } And if (keycode == KeyEvent.VK_RIGHT) {x = x + STEP_SIZE; } And if (keycode == KeyEvent.VK_UP) {y = y - STEP_SIZE; } And if (keycode == KeyEvent.VK_DOWN) {y = y + STEP_SIZE; } Color (); } / ** * When a key typing is completed * * Key * required for any KeyListener * typed * @param * / Public Zero keyTyped (KeyEvent e) key # 4b {} / ** * When the information is included, it is called the key is issued * Required key releases for any Keygenist * * @ Ultimate E contains information about public release / key vacuum (KAVEEDE) // # 4 C {} / ** * Paint - Drag Draw * * @ Ultimate G Graphics * / Sub Nick void paint (Graphics g) object to draw in {g.setColor (Color.white); g.fillRect (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); G.setColor (Color.blue); Gifil (x - radius, y - radius, 2 * radius, 2 * radius); }}    

Just set a max before reprint:

  Public Zero Key Pressed (keyEvent e) // # 4A {int keyCode = e.getKeyCode (); If (keycode == KeyEvent.VK_LEFT) {x = x - STEP_SIZE; } And if (keycode == KeyEvent.VK_RIGHT) {x = x + STEP_SIZE; } And if (keycode == KeyEvent.VK_UP) {y = y - STEP_SIZE; } And if (keycode == KeyEvent.VK_DOWN) {y = y + STEP_SIZE; } X = mathematics.max (0, x); X = Math (SCREEN_WIDTH, x + radius); Y = Math.Max ​​(0, Y + radius); Y = Math (SCREEN_HEIGHT, Y); Repaint (); }    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -