Hot questions for Using Applets in keylistener

Top Java Programmings / Applets / keylistener

Question:

I am working on a game.I want to add images to the applet but they do not appear there until I resize or minimize the applet.I do not know whether my image observer is wrong or not. The images are in right path.Their in build and class folder with correct spelling. What is wrong with my code? any help would be much appreciated.

public class game extends JApplet implements KeyListener, Runnable {

ScreenDir sd;
Image ball;
Image flower;

public void init() {

    sd = new ScreenDir(this);
    ball = getImage(getCodeBase(), "ball.jpg");
    ballb = new Ball(50, 50, 30, 40, Color.red, ball, sd);
    sd.add(ball);
    flower = getImage(getCodeBase(), "flower.gif");
     //class Flower is just defined like class Ball
    flowerf = new Flower(60, 100, 30, 30, Color.green, flower, sd);
    sd.add(flower);

}

public void paint(Graphics g) {
    sd.draw(g);

}

//These are for moving the ball

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void run() {
    while (true) {
        repaint();
        try {
            Thread.sleep(15);
        } catch (InterruptedException ex) {}}}



  public class ScreenDir {

ArrayList<Object> olist;
JApplet parent;

public ScreenDir(JApplet parent) {
    this.parent = parent;
    olist = new ArrayList<>();
}

public void add(Object o) {
    olist.add(o);
}

Image Img;
Graphics oG;

Img  = parent.createImage(parent.getWidth(), parent.getHeight());
oG  = offImg.getGraphics();

for(int i = 0;i<olist.size ();i++){
         olist.get(i).draw(offG);
}

g.drawImage (Img,0, 0, parent);
 }


    public class Ball extends Object {

ScreenDir sd;

public ball(int x, int y, int w, int h, Color c, Image pi, ScreenDir sd) {
    {
        this.sd = sd;
    }
public void draw(Graphics g) {
    g.drawImage(pi, x, y, w, h, null);
}


public abstract class Object {

    int x, y, w, h;
    Color c;
    Image pi;

    public Object(int x, int y, int w, int h, Color c, Image pi) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.c = c;
        this.pi = pi;
    }

    public abstract void draw(Graphics g) {
    }

Answer:

Applets load images asynchronously, so it is likely the images are not fully loaded before the applet goes to paint. But every Java component worth mentioning implements an ImageObserver that means it will get updates on image loading. So to fix the problem, change this:

g.drawImage(pi, x, y, w, h, null);

To this:

g.drawImage(pi, x, y, w, h, this);
Update

I mistakenly thought the drawImage method was part of the JApplet (which is an ImageObserver). You might simply change the method declaration & painting line to:

public void draw(Graphics g, ImageObserver io) {
    g.drawImage(pi, x, y, w, h, io);
}

Then to call it, change:

sd.draw(g);

To:

sd.draw(g, this);

Question:

I am learning Java language and trying to make a simple maze game. I am able to draw a ball and the objects on the screen but I can't move the ball using arrow keys. Here is my code:

 import java.applet.*;
 import java.awt.*;
 import java.awt.event.KeyEvent;
 import javax.swing.*;
 import java.awt.event.*;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;


public class game extends Applet implements ActionListener, KeyListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;

double x = 0, y = 0;

static int MoveX = 210; 
static int MoveY = 800; 

 public void up(){
        MoveY = -2;
        MoveX = 0;
    }

    public void down(){
        MoveY = 2;
        MoveX = 0;
    }

    public void left(){
       MoveX = -2;
        MoveY = 0;
    }

    public void right(){
        MoveX = 2;
        MoveY = 0;
    }



public void paint(Graphics g){
    //g.drawLine(200+10,150-10, 200+190, 150-10);
    //g.drawLine(200+30,200-30, 200+190, 200-30);
    g.setColor(Color.BLUE);


    g.fillRect(200+10, 150-10, 200+40, 150-10);
    g.fillRect(300+40, 150-10, 300+70, 150-10);


    g.fillRect(200+10, 200-10, 200+10, 200-10);
    g.fillRect(200+10, 230-10, 200+10, 230-10);
    g.fillRect(200+10, 260-10, 200+10, 260-10);
    g.fillRect(200+10, 290-10, 200+10, 290-10);
    g.fillRect(200+10, 320-10, 200+10, 320-10);
    g.fillRect(200+10, 350-10, 200+10, 350-10);
    g.fillRect(200+10, 380-10, 200+10, 380-10);




    g.fillRect(600+300, 90-10, 100+100, 150-10);
    g.fillRect(600+300, 150-10, 100+100, 210-10);
    g.fillRect(600+300, 210-10, 100+100, 270-10);
    g.fillRect(600+300, 270-10, 100+100, 330-10);


    g.fillRect(600+100,400, 200, 180);
    g.fillRect(600+30,400, 200, 180);


    g.fillRect(1000+300, 90-10, 100+100, 150-10);
    g.fillRect(1000+300, 150-10, 100+100, 210-10);
    g.fillRect(1000+300, 210-10, 100+100, 270-10);
    g.fillRect(1000+300, 270-10, 100+100, 330-10);
    g.fillRect(1000+300, 330-10, 100+100, 390-10);
    g.fillRect(1000+300, 390-10, 100+100, 350-10);

    g.setColor(Color.RED); 
    int size =90;
    g.fillOval(MoveX, MoveY, size, size); 






}
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
     int code = e.getKeyCode();

        if (code == KeyEvent.VK_UP){
            up();
        }

        if (code == KeyEvent.VK_DOWN){
            down();
        }

        if (code == KeyEvent.VK_LEFT){
            left();
        }

        if (code == KeyEvent.VK_RIGHT){
            right();
        }

}
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    int code = e.getKeyCode();

    if (code == KeyEvent.VK_UP){
        MoveY = 0;
    }
    if (code == KeyEvent.VK_DOWN){
        MoveY = 0;
    }
    if (code == KeyEvent.VK_LEFT){
        MoveX = 0;
    }
    if (code == KeyEvent.VK_RIGHT){
        MoveX = 0;
    }

}
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
     repaint();
        x += MoveX;
        y += MoveY;

        if(x<0){
            MoveX = 0;
            x = 0;
        }

        if(x>750){
            MoveX = 0;
            x = 750;
        }

        if(y<0);{
            MoveY = 0;
            y = 0;
        }

        if(y>550){
            MoveY = 0;
            y = 550;
        }
   }


  }

Applet started successfully and the objects drawn. I pressed arrow keys but the ball did not move. How can I trigger the KeyListener methods?

Thank you so much!


Answer:

You need to set the key listener on your Applet instance:

public void init() {
    addKeyListener(this);
}

Nothing will happen though, because you are not repainting your Applet. After the user presses a key you will need to repaint:

repaint();

The logic for your up(), down(), etc. functions are also incorrect. When you call the up() function the ball's position will be set to 0,-2, when you call the down() function the ball's position will be set to 0,2. You should be adjusting the existing ball position, not setting it to fixed coordinates.