Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Game moving keys

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Thanked 0 Times in 0 Posts

    Default Game moving keys

    I want to write simple game. Why if I press button UP and RIGHT it not move diogonally, may be it have special code. And how I need to write correctly border and gravity? (Sorry for my English, it is not my home language).

    public class Main {
    	public static void main(String args[]) {
    		JFrame frame = new JFrame("Stickman");
    		Ground ground = new Ground();
    		frame.setSize(710, 480);

    public class Ground extends JPanel {
    	public Ground() {
    		addKeyListener(new myKeyAdapter());
    	Image img = new ImageIcon("Resourse/Untitled.jpg").getImage();
    	Stickman stk = new Stickman();
    	private class myKeyAdapter extends KeyAdapter {
    		public void keyPressed(KeyEvent e) {
    		public void keyReleased(KeyEvent e) {
    	public void paint(Graphics g) {
    		g = (Graphics2D) g;
    		g.drawImage(img, stk.sloi1, 0, null);
    		g.drawImage(stk.img, stk.x, stk.y, null);

    public class Stickman {
    	public static final int MAX_TOP = (4);
    	public static final int MAX_BOTTOM = (120);
    	Image img = new ImageIcon("Resourse/Untitled2.png").getImage();
    	int x = 5;
    	int y = 143;
    	int moveAmount = 5;
    	int sloi1 = 0;
    	public void move() {
    		if (y <= MAX_TOP)y = MAX_TOP;
    		if(y >= MAX_BOTTOM)y = MAX_BOTTOM;
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
    		if (key == KeyEvent.VK_RIGHT) {
    			x += moveAmount;
    		if (key == KeyEvent.VK_LEFT) {
    			x -= moveAmount;
    		if (key == KeyEvent.VK_UP) {
    			y -= 20;
    	public void keyReleased(KeyEvent e) {

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 697 Times in 679 Posts

    Default Re: Game moving keys

    Override paintComponent() rather than paint() in a JPanel. For more information, refer to the Oracle Trail on custom painting.

Similar Threads

  1. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  2. [SOLVED] help with java game source code..
    By hemla in forum Object Oriented Programming
    Replies: 7
    Last Post: March 7th, 2013, 06:48 PM
  3. Better solution for moving my rectangle? (simple game)
    By JohnZ in forum Java Theory & Questions
    Replies: 5
    Last Post: May 24th, 2012, 09:54 AM
  4. How to get keys pressed by the user to control the unit in a game.
    By MasterGamer445 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 22nd, 2012, 07:53 AM
  5. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM