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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 4 of 4

Thread: Arrowkeys

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrowkeys

    trying to make one of my chars move but it wont move lol

    here are the class files

    Animation class
    package com.magicgalore;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    @SuppressWarnings("serial")
    public class Animation extends JPanel implements ActionListener, KeyListener {
     
    	Timer t = new Timer(5, this);
    	double x = 0, y = 0, valx = 0, valy = 0;
     
    	public void start() {
    		t.start();
    		addKeyListener(this);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);
    	}
     
    	@Override
    	public void keyPressed(KeyEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    }

    GraphicsManager class
    package com.magicgalore;
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.geom.Ellipse2D;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
     
    @SuppressWarnings("serial")
    public class GraphicManager extends JPanel {
     
    	private Animation animation;
     
    	private static Image image;
     
    	public static void paintBackground(Graphics graphics) {
    		try {
    			image = ImageIO.read(new File("cache/Sprites/background.jpg"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		graphics.drawImage(image, 0, 0, null);
    	}
     
    	public static void paintPlayer(Graphics graphics) {
    		try {
    			image = ImageIO.read(new File("cache/Sprites/char/player/man.png"));
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		graphics.drawImage(image, 42, 47, null);
     
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.fill(new Ellipse2D.Double(getAnimation().x, getAnimation().y, 40, 40));
    	}
     
    	public Animation getAnimation() {
    		return animation;
    	}
    }

    KeyBoredHandler
    package com.magicgalore;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JPanel;
     
    @SuppressWarnings("serial")
    public class KeyBoredHandler extends JPanel implements ActionListener, KeyListener {
     
    	private Animation animation;
     
    	public void actionPerformed(ActionEvent e) {
    		repaint();
    		getAnimation().x += getAnimation().valx;
    		getAnimation().y += getAnimation().valy;
    	}
     
    	public void up() {
    		getAnimation().y = -1.5;
    		getAnimation().x = 0;
    	}
     
    	public void down() {
    		getAnimation().y = -1.5;
    		getAnimation().x = 0;
    	}
     
    	public void left() {
    		getAnimation().x = -1.5;
    		getAnimation().y = 0;
     
    	}
     
    	public void right() {
    		getAnimation().x = -1.5;
    		getAnimation().y = 0;
    	}
     
    	public void keyPressed(KeyEvent e) {
    		int code = e.getKeyCode();
    		if (code == KeyEvent.VK_UP) {
    			up();
    		}
    		if (code == KeyEvent.VK_DOWN) {
    			down();
    		}
    		if (code == KeyEvent.VK_LEFT) {
    			left();
    		}
    		if (code == KeyEvent.VK_RIGHT) {
    			right();
    		}
     
    	}
     
    	public void keyTyped(KeyEvent e) {}
    	public void keyReleased(KeyEvent e) {}
     
     
    	public Animation getAnimation() {
    		return animation;
    	}
    }

    ClientFrames
    package com.magicgalore;
     
    import javax.swing.JFrame;
     
     
    /**
    *Author Joe
    *Makes the pannel
    **/
    public class ClientFrame {
     
    	private static Animation animation = new Animation();
     
     
    	public static void main (String args[]) {
    		JFrame frame = new JFrame();
    		frame.setVisible(true);
    		frame.setResizable(false);
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		frame.setTitle("Magic Galore");
    		frame.setSize(700,500);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GraphicManager.paintBackground(frame.getGraphics());
    		GraphicManager.paintPlayer(frame.getGraphics());
    		getAnimation().start();
    	}
     
    	public static Animation getAnimation() {
    		return animation;
    	}
    }

    Just got to make the "Man" image move.
    Last edited by helloworld922; October 31st, 2010 at 09:51 PM.

  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Arrowkeys

    can you please post also the file man.png, thanks.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrowkeys

    O kk sure

    havent got the man anymore for some odd reason but will this do just making a box move.
    Attached Images Attached Images

  4. #4
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Arrowkeys

    the following link give you a good explaination how to move smoothly pics.

    How To Move Image Smoothly

    let me know if this answer your question.

  5. The Following User Says Thank You to j2me64 For This Useful Post:

    javapenguin (November 4th, 2010)