java - How do I add a keystroke inside of keyPressed(KeyEvent e) method to change color of ball? simple KeyListener demo -


I have limited experience, especially when it comes to graphics, I have got very good help on this problem so far . This is the starter code (below) that works fine as I had tested it in eclipse. I am using it from a class to teach a high school student, according to the instructions given below, should anyone know that the simple program extension to change the color of the ball by pressing button C. Easy to do? I'm thinking of adding the following code to key pressed (keyweed e) method:

  else if (keycode == KeyEvent.VK_C) {// Not sure what code to add here //g.setColor (Color.green) for; ---- & gt; This line says "can not be solved"}   

Any suggestion or idea to keep the program simple will be highly appreciated. 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). * * Additionally, modify the program to ensure the following: * - The ball goes all the way to the edge of the screen but remains completely 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); }}

  private color current color; ... public zero color (graphics g) {g.setColor (Color.white); g.fillRect (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); g.setColor (currrentColor); Gifil (x - radius, y - radius, 2 * radius, 2 * radius); } ... Public Zero Key pressed (key Event E) {int keycode = E.K.Kcode (); 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; } And current COLOR = color BLUE; Repaint (); }   

You probably should do something like that.

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 -