Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ball KeyListener Problem I Guess.. Have a look  (Read 742 times)
0 Members and 1 Guest are viewing this topic.
Offline copycat030

Junior Newbie





« Posted 2012-05-02 23:37:32 »

Main.class


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;



public class Main extends Applet implements Runnable{
   
   private Image img;
   private Graphics doubleG;
   private int width = 800;
   private int height = 600;
   
   
   
   Ball b;
   
   @Override
   public void init() {
      setSize(width, height);
      
   }
   
   
   @Override
   public void run() {
      
         while(true){
            b.update(this);
            repaint();
            try {
               Thread.sleep(17);
            } catch (InterruptedException e){
               e.printStackTrace();
            }
         }
      
   }
   @Override
   public void start(){
      Thread thread = new Thread(this);
      thread.start();
      b = new Ball();
      
      
   }

   @Override
   public void stop() {
      
   }
   
   @Override
   public void destroy() {
      
   }
   
   @Override
   public void paint(Graphics g) {
      b.paint(g);
   }
   
   //Double buffering
   @Override
   public void update(Graphics g) {
      if(img == null){
         img = createImage(this.getSize().width, this.getSize().height);
         doubleG = img.getGraphics();
      }
      doubleG.setColor(getBackground());
      doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
      doubleG.setColor(getForeground());
      paint(doubleG);
      
      g.drawImage(img, 0, 0, this);
   }


   
   
}




/////////////////////////////////////////////////////////////////////////////

Ball.class


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Ball implements KeyListener{
   
   private int x = 0;
   private int y = 0;
   private int radius = 50;
   private double dx;
   private double dy;
   private double gravity = 10;
   private double energyloss = .8;
   private double friction = .35;
   private double dt = .2;
   private static boolean right = false;
   private static boolean left = false;
   private static boolean up = false;
   private static boolean down = false;
   
   public Ball(){
      
   }
   public void update(Main m){
      
      // X düzlemi
      if(x + dx > m.getWidth() - radius - 1){
         x = m.getWidth() - radius -1;
         dx *= -1;
      }
      else if(x + dx < 0){
         x = 0;
         dx *= -1;
      }
      
      else{
         x += dx;      
      }
      
      
      // Y düzlemi
      
      if(y > m.getHeight() - radius - 1){
         y  = m.getHeight() - radius -1;
         dy *= energyloss;
         dy *= -1;
      }
      
      else{
         dy += gravity * dt;
         y += dy*dt - .5 * gravity *dt * dt ;
         y += dy;               
      }
      
      
   }
   
   public void paint(Graphics g) {
      g.setColor(Color.RED);
      g.fillOval(x, y, radius, radius);
   }
   
   
   @Override
   public void keyPressed(KeyEvent e) {
      int keycode = e.getKeyCode();
      
         if(keycode == KeyEvent.VK_RIGHT){
            right = true;
         }
         if(keycode == KeyEvent.VK_LEFT){
            left = true;
         }
         if(keycode == KeyEvent.VK_DOWN){
            down = true;
         }
         if(keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_SPACE){
            up = true;
         }
         moveLeft();
         moveRight();
         moveUp();
         moveDown();
      }
   

   @Override
   public void keyReleased(KeyEvent e) {
      int keycode = e.getKeyCode();
         if(keycode == KeyEvent.VK_RIGHT){
            right = false;
         }
         if(keycode == KeyEvent.VK_LEFT){
            left = false;
         }
         if(keycode == KeyEvent.VK_DOWN){
            down = false;
         }
         if(keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_SPACE){
            up = false;
         }
         moveLeft();
         moveRight();
         moveUp();
         moveDown();
         
      
   }

   @Override
   public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub
      
   }
   
   public void moveRight(){
      if(right)   {
         if(dx +1 <10){
            if(dx < -5){
               dx = +3;
            }
            dx += 2;
         }
      }
   }
   
   public void moveLeft(){
      if(left){
         if(dx -1 > -10){
            if(dx > 5){
               dx = -3;
            }
         dx -= 2;
         }
      }
   }
   
   public void moveUp(){
      if(up){
         if(dy > -30){
            dy = -30;
         }
      }
   }
   
   public void moveDown(){
      if(down){
         dy = +30;
      }
   }
   

}
///////////////////////

I split up my ball events and main thinks so I have two class as a result but my ball is neither let me control him nor comply with my physic rules. Why is that can anybody help me please ? Thank you.
Offline Tim_Wilson

Senior Member


Medals: 7
Projects: 1



« Reply #1 - Posted 2012-05-03 00:25:36 »

Right under "b = new Ball();", add this:

1  
addKeyListener(b);
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2012-05-03 00:55:47 »

haha, classic mistake.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline copycat030

Junior Newbie





« Reply #3 - Posted 2012-05-03 09:18:47 »

Right under "b = new Ball();", add this:

1  
addKeyListener(b);


Well, it didn't work.

haha, classic mistake.

And yet it hasn't solved Smiley

Magically, it has solved after ten minutes lol, thank you so much Smiley

It doesn't run properly all the time, anyone knows why is that?

///

Exception in thread "Thread-4" java.lang.NullPointerException
   at Main.run(Main.java:30)
   at java.lang.Thread.run(Unknown Source)
///

This is the error.
Offline copycat030

Junior Newbie





« Reply #4 - Posted 2012-05-03 10:04:58 »

I added this

1  
2  
3  
4  
5  
6  
try{
               b.update(this);
               b2.update(this);
            }catch(NullPointerException e){
               e.fillInStackTrace();
            }


and my problem has solved completely. I did this but I don't really know what I did Cheesy
Offline 65K
« Reply #5 - Posted 2012-05-03 10:52:57 »

NullPointerExceptions shouldn't be catch but fixed Wink
The problem is probably that you start the thread and afterwards set the ball, so at the beginning the ball reference is empty in run().

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!