This discussion is archived
5 Replies Latest reply: Jan 1, 2011 3:26 AM by 827803 RSS

Program won't work

802722 Newbie
Currently Being Moderated
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;
import java.awt.Dimension;

public class circle extends Frame {
  int x = 1300;
  int y = new Random().nextInt(200) + 301;
  int by;
  boolean key_left = false;
  boolean key_right = false;
  static boolean firing = false;
  Shape cannon = new Rectangle2D.Double(600, 900, 40, 75);
  Shape bullet = new Rectangle2D.Double(617, by, 6, 12);

  private static KeyListener keyListener = new KeyListener()
  {
    public void keyPressed(java.awt.event.KeyEvent e) {
      int key = e.getKeyCode();
      System.out.println(key);
      if (key == 97)
      {
        firing = true;
      }
    }
    public void keyReleased(java.awt.event.KeyEvent e) {
      System.out.print("");
    }
    public void keyTyped(java.awt.event.KeyEvent e) {
      System.out.print("");
    }
  };

  public void paint(Graphics g) {
    Graphics2D ga = (Graphics2D)g;
    ga.setPaint(Color.black);
    ga.draw(cannon);
    ga.fill(cannon);
    int by;
    int p = 2;

    for (int x = 1300; x > -50; x = x - 5) {
      if(firing) {
        System.out.println("firing");
      }    
      ga.setPaint(Color.white);
      if(firing) {
        System.out.println("firing");
      }      
      ga.draw(new Ellipse2D.Double(x, y, 50, 50));
      if(firing) {
        System.out.println("firing");
      } 
      ga.setPaint(Color.blue);
      if(firing) {
        System.out.println("firing");
      } 
      ga.fill(new Ellipse2D.Double(x, y, 50, 50));
      if(firing) {
        System.out.println("firing");
      } 
      Long stoptime = 50l;
      if(firing) {
        System.out.println("firing");
      } 
      try {
        Thread.sleep(stoptime);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if(firing) {
        System.out.println("firing");
      } 
      ga.setPaint(Color.white);
      if(firing) {
        System.out.println("firing");
      } 
      ga.draw(new Ellipse2D.Double(x, y, 50, 50));
      if(firing) {
        System.out.println("firing");
      } 
      ga.setPaint(Color.white);
      if(firing) {
        System.out.println("firing");
      } 
      ga.fill(new Ellipse2D.Double(x, y, 50, 50));
    }
    System.out.println(p);
  }

  public static void main(String args[]) {
    Frame frame = new circle();
    frame.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        System.exit(0);
      }
    });
    frame.setBackground(Color.white);
    frame.addKeyListener(keyListener);
    frame.setSize(1300, 1000);
    frame.setVisible(true);
  }
}
the keylistener won't work

Moderator edit: Added code tags
  • 1. Re: Program won't work
    gimbal2 Guru
    Currently Being Moderated
    799719 wrote:
    the keylistener won't work
    How can you be so sure? You put printlns everywhere, but did you try putting them in the keylistener methods themselves to see what is being invoked?

    Also, it might be a problem of focus. Try clicking your frame before typing something. There are specific methods to make the frame grab the focus that you can use at program startup - don't know from memory which method it is though, check the javadocs.

    Finally - is there a specific reason why you are using AWT and not Swing?
  • 2. Re: Program won't work
    802722 Newbie
    Currently Being Moderated
    I want the key listener to work while the circle is going across the screen. It works after the circle has gone across the screen but not while it is. I am new to java so I don't see the difference between AWT and swing.

    Edited by: 799719 on Oct 4, 2010 5:23 PM
  • 3. Re: Program won't work
    DarrylBurke Guru Moderator
    Currently Being Moderated
    799719 wrote:
    I am new to java so I don't see the difference between AWT and swing.
    <font size="2">In that case, drop AWT -- it's outdated and clunky -- and adopt Swing.
    http://download.oracle.com/javase/tutorial/uiswing/index.html</font>


    db
  • 4. Re: Program won't work
    morgalr Explorer
    Currently Being Moderated
    Take note where you have everything located in your application:

    1 - you have a key listener
    2 - you have a paint menthod
    3 - where is your animation loop in reference to 1 and 2?

    Your animation loop is in your paint method. How do you expect anything to break into your paint and do anything? It doesn't and cannot. You have to make a loop based on some type of external event timer (SWING is great for that). Your algo will go something like this:

    add the keylistener
    add a listener for the timer
    start your timer
    -end-

    You still have to make a paint method to put your graphics on the screen, but you need to call a repaint from your listener handling your timer events. Your keylistener has to update appropriate variables that your paint method uses to place your animation. In summary:

    you need a keylistener that updates specific variables used in your animation.
    you need a loop based on a timer (NOT a counter).
    you need the paint method to be called via a repaint() request from your timer event handler (listener)

    If you can implement that, it does not matter much if you use AWT or SWNG, the implementation is basically the same.
  • 5. Re: Program won't work
    827803 Newbie
    Currently Being Moderated
    I'm unsure how you've targeted your work, and whether its for archiving and distribution/web applications/a homework assignment, however from my own knowledge I can safely say I think you've used keyListener incorrectly.

    When adding a key listener, eg addKeyListener(this);+ you'd only expect to see this code in the initialising stage of an applet/applications host class with a runnable method, I don't think it's the right approach trying to intialise a keyListener alongside declare the required typed/pressed/released methods.

    Try creating a keyListener instance at various locations amongst your program, these should be locations where you declare a graphics instance/buffer/boolean game starter. The methods can be kept completely separately in methods of their own, with switch statements to evaluate button presses.


    public void keyTyped(KeyEvent k) { }

    public void keyPressed(KeyEvent k) {
    methodhere(k.getKeyCode());
    }

    public void keyReleased(KeyEvent k) {
    methodhere(k.getKeyCode());
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points