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.