I'm working on a project at school and for some reason can't figure out why my keyListener isn't working. The project is in the CVM(Controller View Model) design.
here is the view code:
import javax.swing.JFrame; import java.awt.Graphics; import javax.swing.JPanel; import java.util.ArrayList; public class View extends JFrame { private Controller controller; private class MyPanel extends JPanel { MyPanel(Controller c) { addKeyListener(c); addMouseListener(c); } public void paintComponent(Graphics g) { controller.update(g); revalidate(); } } public View(Controller c) throws Exception{ controller = c; setTitle("Assignment 4"); setSize(1200, 800); getContentPane().add(new MyPanel(c)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } }
here is the Controller:
import java.awt.Graphics; import java.io.IOException; import java.awt.event.KeyEvent; import java.awt.event.MouseListener; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; class Controller implements MouseListener, KeyListener { private Model model; private View view; Controller() throws IOException, Exception { view = new View(this); model = new Model(view.getWidth(), view.getHeight()); view.requestFocus(); } public void update(Graphics g) { if (model != null) { model.update(g); } } public Model getModel() { return model; } public void mousePressed(MouseEvent e) { model.forward(); view.repaint(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void keyPressed(KeyEvent e) { char s = e.getKeyChar(); if(s == 'r' || s == 'R') { model.initialize(); } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws Exception { // Use the following line to determine which directory your program // is being executed from, since that is where the image files will // need to be. //System.out.println("cwd=" + System.getProperty("user.dir")); new Controller(); } }
Here is the Model:
import java.awt.Graphics; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import java.util.Iterator; class Model { private ArrayList<Sprite> sprites; private int width; private int height; Random r; Model(int w, int h) throws IOException { sprites = new ArrayList<Sprite>(); width = w; height = h; r = new Random(); initialize(); } public ArrayList<Sprite> getSpriteList() { return sprites; } public void initialize() { if((sprites != null)&&(!sprites.isEmpty())) { Iterator<Sprite> iter = sprites.iterator(); while(iter.hasNext()) sprites.remove(iter); } for (int i=0; i<50; i++) { int newX = r.nextInt(width-50); int newY = r.nextInt(height-50); if (i%2==0) { sprites.add(new Razorback(newX, newY, width, height)); } else { sprites.add(new Opponent(newX, newY, width, height)); } } } public void update(Graphics g) { for (Sprite sprite : sprites) { sprite.update(g); } } public void forward() { Iterator<Sprite> iter1 = sprites.iterator(); while(iter1.hasNext()){ Sprite obj1 = iter1.next(); if(obj1.shouldRemove()) { iter1.remove(); } } for (Sprite s : sprites) { s.move(); Iterator<Sprite> iter2 = sprites.iterator(); while(iter2.hasNext()) { Sprite obj2 = iter2.next(); if(s.overlaps(obj2)) { if((s instanceof Razorback)&&(obj2 instanceof Opponent)){ obj2.hit(); } else if((s instanceof Opponent)&&(obj2 instanceof Razorback)){ s.hit(); } } } } } }
When a key is pressed the model is suppose to be re initialized. Any Idea why the keyListener keyPressed handler is not working?