Results 1 to 5 of 5
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Things acting weirdly?

    Hi!
    Sorry for posting twice today :P
    In order to learn i wrote this.
    Java Code:
    package xyexs.snake.src;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JPanel;
    
    public class Panel extends JPanel {
    
    	public int x = 0;
    	public int y = 0;
    
    	public void paintComponent(Graphics g) {
    		super.paintComponents(g);
    		g.setColor(Reference.BACKGROUND_COLOR);
    		g.fillRect(0, 0, Reference.WINDOW_X, Reference.WINDOW_Y);
    		g.setColor(Reference.SNAKE_COLOR);
    		g.fillRect(y, x, Reference.PIXEL_SIZE, Reference.PIXEL_SIZE);
    	}
    
    	public Panel() {
    		keyHandler handlerObject = new keyHandler();
    		setFocusable(true);
    		addKeyListener(handlerObject);
    	}
    
    	public Dimension getPreferredSize() {
    		return new Dimension(Reference.WINDOW_X, Reference.WINDOW_Y);
    	}
    
    	private class keyHandler extends KeyAdapter {
    		@Override
    		public void keyPressed(KeyEvent key) {
    			if (key.getKeyCode() >= 37 && key.getKeyCode() <= 40) {
    
    				switch (key.getKeyCode()) {
    
    					case KeyEvent.VK_LEFT: {
    						// left
    						x = x - 1;
    					}
    					case KeyEvent.VK_UP: {
    						// up
    						y = y - 1;
    					}
    					case KeyEvent.VK_RIGHT: {
    						// right
    						x = x + 1;
    					}
    					case KeyEvent.VK_DOWN: {
    						// down
    						y = y + 1;
    					}
    				}
    				repaint();
    			}
    		}
    	}
    }
    It's supposed to move the box in the direction of the arrow you press.




    The weird thing is that if i press up it ... see below...




    If i press... It moves...
    Up Down
    Right Right/down
    Down Right
    Left Nowhere
    Last edited by Xyexs; 08-23-2013 at 05:05 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Things acting weirdly?

    Posting twice in a day isn't a problem.

    Multiple blank lines in your code make it diffcult to read.

    See inline comments:
    Quote Originally Posted by Xyexs View Post
    Java Code:
    ...
    			// don't use 'magic numbers'
    			// use defined constants instead.
    			if (key.getKeyCode() >= 37 && key.getKeyCode() <= 40) {
    
    				switch (key.getKeyCode()) {
    					case KeyEvent.VK_LEFT: {
    						// left
    						x = x - 1;
    					}
    					case KeyEvent.VK_UP: {
    						// up
    						x = x + 1; // HUH?  x or y?
    					}
    					case KeyEvent.VK_RIGHT: {
    						// right
    						x = x + 1;
    					}
    					case KeyEvent.VK_DOWN: {
    						// down
    						y = y + 1;
    					}
    				}
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,645
    Rep Power
    5

    Default Re: Things acting weirdly?

    See DB? Switch fall through - its an exploding rocket ship waiting to happen!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Things acting weirdly?

    Oops, didn't even notice that. A few sysouts would have caught it though.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Things acting weirdly?

    Sorry, Switch fall through? sysout would catch? Anyway, that was not tha actual code i was using, updated OP (just some minor things ,and those blank lines werent actually there in eclipse)

    EDIT : i see! (google) I don't know why it does that though, isn't switch just an alternative to if elseif else if elseif else ?
    I just put breaks in every switch
    Last edited by Xyexs; 08-23-2013 at 05:33 PM.

Similar Threads

  1. JMenuBar acting weird
    By MR bruto in forum AWT / Swing
    Replies: 2
    Last Post: 06-02-2013, 06:31 AM
  2. primitive acting like a Object
    By killutch in forum New To Java
    Replies: 5
    Last Post: 09-15-2012, 07:59 PM
  3. Text Fields Acting Up
    By Tohron in forum Java Applets
    Replies: 5
    Last Post: 09-05-2012, 06:14 AM
  4. actionPerformed acting strangely
    By Fortu in forum New To Java
    Replies: 18
    Last Post: 04-10-2011, 04:07 AM
  5. Acting Java as if it's C problem
    By reis3k in forum New To Java
    Replies: 13
    Last Post: 10-18-2010, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •