Whats wrong whit code?

Why image doesnt move?

 package org.me.hello;

 import java.applet.*;
 import java.awt.*;
 import java.awt.event.*;
      
 public class peli extends Applet implements KeyListener {
  
 Image karkki;
 Image tikkari;
 int x,y;
 public boolean oikee;
 public boolean vasen;
 
 public void run() {

 while (true) {

 repaint();
     
 }    
 }
 
 public void init(){
 resize(600,400);
 x=200;
 y=350;        
 }
    
 public void keyPressed(KeyEvent e) {
 
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         vasen = true;
      }
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         oikee = true;
      }
 }
 
 public void keyReleased(KeyEvent e) {
 
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
          oikee = false;
      }
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
          vasen = false;
      }
 }
       
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {} 

 
public void oikee(boolean b) {
if(b == true){
x = x+2;            
}
}

public void vasen(boolean b) {
if(vasen == true){
x = x-2; 
}
} 
 
 public void paint(Graphics g) {
 tikkari = getImage(getDocumentBase(),"tikkari.png");
 karkki = getImage(getDocumentBase(),"karkki.png");
 if(oikee = true){
 x = x+2;
 } 
 if(vasen = true){ 
 x = x+2;
 }

 g.drawImage(karkki,x,y,50,50,this);

 }  
 }

First, you have to add the KeyListener in the init method. I realize that the class is the Applet and the KeyListener, but you still have to add it. Add the line “addKeyListener(this);” to the init method.

Also, it looks like your if statements in the paint method have “=” in them. If statements should always use “==” for boolean comparison. I’m surprised that code even gets past the compiler.

Additionally, instead of checking the right key twice, you probably want to check the left key and the right key.

That should be enough to get the image moving, but there are some other improvements you should make. You should only load the images in the init method and store them in variables to be used by the paint method. In general, you shouldn’t include anything except painting in the paint method. In fact, you should remove the coordinate updating from the paint method and put it in the keyReleased method, eliminating all the code that’s in the keyPressed and keyReleased methods, as well as eliminated the booleans.

doesnt work…
new code:

package org.me.hello;

 import java.applet.*;
 import java.awt.*;
 import java.awt.event.*;
      
 public class peli extends Applet implements KeyListener {
 
 Image ukko;    
 Image karkki;
 Image tikkari;
 int x,y;
 public boolean oikee;
 public boolean vasen;
 
 public void run() {

 while (true) {

 repaint();
     
 }    
 }
 
 public void init(){
 addKeyListener(this);
 tikkari = getImage(getDocumentBase(),"tikkari.png");
 karkki = getImage(getDocumentBase(),"karkki.png");
 resize(600,400);
 x=300;
 y=350;
 }
    
 public void keyPressed(KeyEvent e) {
 
      if (e.getKeyCode() == KeyEvent.VK_LEFT) {
         vasen = true;
      }
     
      
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         oikee = true;
      }
      
 if(oikee == true){
 x = x+2;
 } 
          
      
  if(vasen == true){ 
 x = x+2;
 }    
      
 Image tausta;
 }
 
 public void keyReleased(KeyEvent e) {
 
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
          oikee = false;
      }
      
      if (e.getKeyCode() == KeyEvent.VK_LEFT) {
          vasen = false;
      } 
 }
       
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {} 
 
 public void paint(Graphics g) {

 g.drawImage(karkki,x,y,50,50,this);

 }  
 }</sub>

Hello,
there are demo applets from sun here: http://java.sun.com/applets/jdk/1.4/index.html take a look at theese they should help.

Your big problem is that you are never calling repaint(). It’s likely your applet is working, but not getting redrawn so you don’t even know it. You’re going to want to add repaint() to your keyPressed() method.

Speaking of that method… You don’t need to use your boolean way of doing things at all. As long as a key is being held, it will continue to call the keyPressed method. So…


public void keyPressed(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             x -= 2;
          }
         
          
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             x += 2;
          }
          repaint();
     }

Go with that, and just get rid of everything in your keyReleased() method, and get rid of those two booleans you have. I also don’t understand why you would declare an Image there… if you declare a variable and don’t ever reference it in that scope, it will simply get cleaned up by the garbage collector. For that reason, I removed " Image tausta;" from the end of KeyPressed().

Also, in the future put all your code in [ code] tags when you are posting on the forums. It makes things easier for us.

It could use a sleep(50); in the run() method also…

now i have an new problem:
image(donut) isnt walling normally… how do i fix this problem?



     package org.me.hello;

     import java.applet.*;
     import java.awt.*;
     import java.awt.event.*;
          
     public class peli extends Applet implements KeyListener,Runnable {
             
     Font fontti = new Font("Helvetica", Font.BOLD,  20);    
     
     Image homer;
     Image homerv;      
     Image sydan;
     Image donut; 
     
     int äks;
     int x,y;
     int pisteet,livet;
    
     public boolean oikee;
     public boolean vasen;
     public boolean tippuu;
     
     public void run(){             
                     
     while (true) {
                         
     repaint();
         
     }    
     }
     
     public void init(){
     this.äks=(int)(4*Math.random()*100+4);      
     addKeyListener(this);
     setBackground(Color.BLACK);
     donut = getImage(getDocumentBase(),"donutti.png");
     homer = getImage(getDocumentBase(),"homer.png");
     homerv = getImage(getDocumentBase(),"homerv.png");
     sydan = getImage(getDocumentBase(),"sydan.png");     
     resize(600,400);
     livet = 4;
     x=175;
     y=-70;
     }
             
     public void keyPressed(KeyEvent e) {
    
          if (e.getKeyCode() == KeyEvent.VK_ENTER) {
             tippuu = true;             
          }
         
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             vasen = true;
          }
         
          
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             oikee = true;
          }
          
     if(oikee == true){
     if(x<350)x = x+10;
     } 
              
          
     if(vasen == true){
     if(x>0)x = x-10;
     }
                    
     repaint();
     }
     
     public void keyReleased(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
              oikee = false;
          }
          
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
              vasen = false;
          } 
     }
           
    public void actionPerformed(ActionEvent e) {}
    
     public void alas(){
               
     }
     
     public void paint(Graphics g) {
     
     g.setColor(Color.red);
     g.drawRect(450,1,5,1000);
     g.setFont(fontti);
     g.drawString("Lives:",470,50);
     
     if(livet > 1){
     g.drawImage(sydan,470,65,30,30,this);
     }
     
     if(livet > 2){
     g.drawImage(sydan,500,65,30,30,this);
     }
     
     if(livet > 3){
     g.drawImage(sydan,530,65,30,30,this);
     }
     
     g.drawString("Points:"+pisteet+"",470,130);
         
     if(vasen==false){    
     g.drawImage(homer,x,313,83,100,this);
     }
    
     if(vasen==true){
     g.drawImage(homerv,x,313,83,100,this);               
     }
     
     g.drawImage(donut,äks,y,50,50,this);
     if(tippuu==true);
     }
     
    public void keyTyped(KeyEvent e) {
    }
     }

It’s your statements like

if(x>0)x = x-10;

.

If x == 5, then you’ll subtract 10 and get -5.

Instead, you want to do something like:

x -= 10;
if(x < 0)
   x = 0;

And somethign similar for hitting the other wall. Or, at least, I think that’s what you’re asking about.