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: keylistner + applet isnt working

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation keylistner + applet isnt working

    Hello everybody!
    ths is a project that i have been working on for the past week or soo! it has been going pretty well, until i tried to add in the keylistener to look for the up, down, left , and right direction keys to make an object on the screen move! I am trying to make the BLUE box move. the point of the game is to have a square that you control, that is to avoid the 2 bouncing balls.

    (the code I added with this doesn't have any sort of keylistener yet)

    thanks in advance to everybody!
    post anything please, any help is appreciated

    Code:
    //
    //brandon witt
    //paddle game
    //dec 6 2010
    //
     
     
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
     
    public class paddlegame extends Applet implements Runnable {
    Graphics g;
    //font declaration
     Font font1 = new Font("Helvetica", Font.BOLD, 90);
     Font font2 = new Font("Helvetica", Font.PLAIN, 30);
     //speed/trajectory
     int x_shift = 1;
    int y_shift = 1;
    int x1_shift = 2;
    int y1_shift = 2;
    int x1 = 500;
    int y1 = 500;
    //scorer
    double counter = 0;
    //etc.
    boolean bouncing;
    boolean testOutput = true;
    public void init() {
    r = new Rectangle(30,50,20,20);
    b = new Rectangle (40, 60, 20, 20);
    Thread t = new Thread(this);
    t.start();// behaviour of thread controlled by run method
    bouncing = true;
     
    }
     
     
    public void paint(Graphics g) {
     //square
    	g.setColor(Color.BLUE);
    	g.fillRect(x1, y1, 80, 80);
     //score display
    	g.setColor(Color.BLACK);
     g.setFont(font2);
     g.drawString("Score:" + counter, 500, 25 );
     //bouncing balls
     if (bouncing) g.setColor(Color.RED);
    g.fillOval(r.x,r.y,r.width,r.height);
    if (bouncing) g.setColor(Color.BLUE);
    g.fillOval(b.x,b.y,b.width,b.height);
    if (testOutput)
    System.out.println ("x = " + r.x + " y = " + r.y + " bouncing = " + bouncing);
    if (r.x > x1 && r.x < x1 + 80 && r.y > y1 && r.y < y1 + 80  ){
     //explosion scene
      g.setColor(Color.BLACK);
     g.fillOval(r.x - 100,r.y - 100,100, 100);
     g.setColor(Color.YELLOW);
     g.fillOval(r.x - 20,r.y - 40,100, 100);
     g.setColor(Color.RED);
     g.fillOval(r.x - 30,r.y - 15,100, 100);
     g.setColor(Color.ORANGE);
     g.fillOval(r.x - 100,r.y - 55,100, 100);
     g.setColor(Color.BLUE);
     g.fillOval(r.x - 5,r.y - 30,100, 100);
     g.setColor(Color.PINK);
     g.fillOval(r.x - 10,r.y - 40,100, 100);
     g.setColor(Color.GREEN);
     g.fillOval(r.x - 50,r.y - 50,100, 100);
     g.setColor(Color.RED);
     g.fillOval(r.x - 150,r.y - 150,250, 300);
     g.setColor(Color.orange);
     g.fillOval(r.x - 50,r.y - 50,40, 100);
     g.setColor(Color.orange);
     g.fillOval(r.x - 100,r.y - 80,100, 200);
     g.setColor(Color.yellow);
     g.fillOval(r.x - 50,r.y - 50,50, 100);
     g.setColor(Color.red);
     g.fillOval(r.x - 100,r.y - 40,40, 10);
     g.setColor(Color.ORANGE);
     g.fillOval(r.x - 10,r.y - 30,100, 100);
     g.setColor(Color.ORANGE);
     g.fillOval(r.x - 60,r.y - 80,20, 10);
     g.setColor(Color.BLUE);
     g.setFont(font1);
     g.drawString("YOU LOSE!!!", 300, 300);
     g.setColor(Color.BLACK);
     g.setFont(font2);
     g.drawString("Your Final Score:" + counter, 300, 400 );
     
    }
    if (b.x > x1 && b.x < x1 + 80 && b.y > y1 && b.y < y1 + 80){
     //explosion scene 2
      g.setColor(Color.BLACK);
     g.fillOval(b.x - 100,b.y - 100,100, 100);
     g.setColor(Color.YELLOW);
     g.fillOval(b.x - 20,b.y - 40,100, 100);
     g.setColor(Color.RED);
     g.fillOval(b.x - 30,b.y - 15,100, 100);
     g.setColor(Color.ORANGE);
     g.fillOval(b.x - 100,b.y - 55,100, 100);
     g.setColor(Color.BLUE);
     g.fillOval(b.x - 5,b.y - 30,100, 100);
     g.setColor(Color.PINK);
     g.fillOval(b.x - 10,b.y - 40,100, 100);
     g.setColor(Color.GREEN);
     g.fillOval(b.x - 50,b.y - 50,100, 100);
     g.setColor(Color.RED);
     g.fillOval(b.x - 150,b.y - 150,250, 300);
     g.setColor(Color.orange);
     g.fillOval(b.x - 50,b.y - 50,40, 100);
     g.setColor(Color.orange);
     g.fillOval(b.x - 100,b.y - 80,100, 200);
     g.setColor(Color.yellow);
     g.fillOval(b.x - 50,b.y - 50,50, 100);
     g.setColor(Color.red);
     g.fillOval(b.x - 100,b.y - 40,40, 10);
     g.setColor(Color.ORANGE);
     g.fillOval(b.x - 10,b.y - 30,100, 100);
     g.setColor(Color.ORANGE);
     g.fillOval(b.x - 60,b.y - 80,20, 10);
     g.setColor(Color.BLUE);
     g.setFont(font1);
     //final score text
     g.drawString("YOU LOSE!!!", 300, 300);
     g.setColor(Color.BLACK);
     g.setFont(font2);
     g.drawString("Your Final Score:" + counter, 300, 400 );
     
    }
    }
    public void update(Graphics g) { 
     
    Image offScreenImage = createImage(getSize().width, getSize().height);
    paint(offScreenImage.getGraphics());
    g.drawImage(offScreenImage,0,0, null);
    }
    public void run() {
     
    while (true) {
    	// Thread performs endless loop
     
    r.x += x_shift;
    r.y += y_shift;
    if (r.x>= getSize().width || r.x < 0) {
    x_shift *= -1; 
    }
    if (r.y>= getSize().height || r.y < 40) {
    y_shift *= -1; 
    }
    b.x += x1_shift;
    b.y += y1_shift;
    if (b.x>= getSize().width || b.x < 0) {
    x1_shift *= -1; 
    }
    if (b.y>= getSize().height || b.y < 40) { // Why 40?
    y1_shift *= -1; 
    }
    try {
    	//speed of balls (long)
    Thread.currentThread().sleep((long) 0.9 );
    }
    catch (Exception ke) {
    }
    //loop break if ball 1 or 2 hits square
    if (r.x > x1 && r.x < x1 + 80 && r.y > y1 && r.y < y1 + 80  ){
     break;
    }
    if (b.x > x1 && b.x < x1 + 80 && b.y > y1 && b.y < y1 + 80  ){
    	 break;
    	}
    //repaints the balls at their current location
    repaint();
    //increases the score
    counter += 0.01;
    }
    }
    private Rectangle r;
    private Rectangle b;
    public boolean mouseDown(Event e) {
    return true;
    }
    public boolean mouseDrag(Event e ) {
    return true;
    }
    public KeyEvent events(KeyEvent ke){
     return ke;
    }
    public boolean mouseUp(Event e) {
    return true;
    }
     
     
    }
    Last edited by copeg; January 3rd, 2011 at 11:39 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: keylistner + applet isnt working

    Please do not post your question more than once to the forums, your other posts have been removed. Further, for future reference please flank your code with the [highlight=java][/highlight] tags...I have done this for you.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keylistner + applet isnt working

    Hi,

    I think you should implement the Keydown method of the applet's component class.

    I googled:
    Small Game With KeyListener - Java | Dream.In.Code

    public boolean keyDown (Event e, int key) {
     // user presses left cursor key
     if (key == Event.RIGHT){  .......
    ......
    }

    Suicidal

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keylistner + applet isnt working

    Sorry Brandon95 KeyDown is depricated. You should make a KeyListener.

    How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class JKeyEvApplet extends Applet implements KeyListener {
     
        @Override
        public void init(){
            addKeyListener(this);
     
            m_width = getSize().width;
            m_height = getSize().height;
     
            this.setFocusable(true);
            this.setBackground(Color.BLUE);
        }
     
        public void keyPressed(KeyEvent e) {
            System.out.println("Key pressed: " + e.getKeyChar());
        }
     
        public void keyReleased(KeyEvent e) {
           System.out.println("Key released: " + e.getKeyChar());
        }
     
        public void keyTyped(KeyEvent e) {
            System.out.println("Key typed: " + e.getKeyChar());
        }
     
        int m_height, m_width;
    }

Similar Threads

  1. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  2. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  3. Boggle not working?
    By gandalf5166 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:53 AM
  4. hmm for some reason the char.At isnt working
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2010, 04:27 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread