Im new to Java and when tring to get some textures into my game I got this error stated ImageIO cannot b resolved. Here is my code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;






public class gameLoop extends Applet implements Runnable, KeyListener{



public int x, y;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background,foreground,w1,w2,w3,w4;

public void run() {
    x = 100;
    y = 100;
    try {
        background = ImageIO.read(new File("gameBackground.png"));
        foreground = ImageIO.read(new File("gameForeground.png"));
        w1 = ImageIO.read(new File("playerWalk1"));
        w2 = ImageIO.read(new File("playerWalk2"));
        w3 = ImageIO.read(new File("playerWalk3"));
        w4 = ImageIO.read(new File("playerWalk4"));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    while(true){
        if (left == true){
            x-=5;
        }if (right == true){
            x+=5;
        }if (up == true){
            y-=5;
        }if (down == true){
            y+=5;
        }
    repaint();
    try {
        Thread.sleep(20);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }

}

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == 37){
        left = true;
    }
    if (e.getKeyCode() == 38){
        up = true;
    }
    if (e.getKeyCode() == 39){
        right = true;
    }
    if (e.getKeyCode() == 40){
        down = true;
    }
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == 37){
        left = false;
    }
    if (e.getKeyCode() == 38){
        up = false;
    }
    if (e.getKeyCode() == 39){
        right = false;
    }
    if (e.getKeyCode() == 40){
        down = false;
    }
}

public void keyTyped(KeyEvent e) {}



}

My other java class:

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


public class Game extends gameLoop {



public void init(){
    setSize(854,480);
    Thread th = new Thread(this);
    th.start();
    offscreen = createImage(854, 480);
    d = offscreen.getGraphics();
    addKeyListener(this);
}
public void paint(Graphics g){
    d.clearRect(0, 0, 854, 480);
    d.drawImage(background, 0, 0, this);

    d.drawOval(x, y, 20, 20);
    g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g){
    paint(g);
}


}

Can someone help me.

Recommended Answers

All 6 Replies

I see no ImageIO class, and I see no import for an ImageIO class. that might be the problem.

  1. there isn't reason to use prehistoric AWT Applet, nor JApplet to use JFrame instead

  2. background = ImageIO.read(new File("gameBackground.png")); etc aren't valid path for File in Java, there are two ways put images as files to Java packages or to type full path (I'd not be suggest)

  3. don't to use Thread.sleep(int) in todays code, it can works for prehistoric AWT Applet and compiled in prehistoric Java, use Swing Timer instead

  4. repaint() should be placed in KeyEvents directly

  5. use JFrame, put there JPanel override paintComponent for JPanel, 1st code line should be super.paintComponent otherwise painting are cumulated or background isn't repainted

  6. paint() ins't placed correctly, never will be called (this is automatically, from methods implemented in APIs) for more info to read Oracle trail 2D Graphics

I tried to make a ImageIO class and did what I can but my problem isn't fixed can you please check my 3 java files:

Game.java:

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


public class Game extends gameLoop {



public void init(){
    setSize(854,480);
    Thread th = new Thread(this);
    th.start();
    offscreen = createImage(854, 480);
    d = offscreen.getGraphics();
    addKeyListener(this);
}
public void paint(Graphics g){
    d.clearRect(0, 0, 854, 480);
    d.drawImage(background, 0, 0, this);
    d.drawImage(w1, x, y, this);

    g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g){
    paint(g);
}


}

gameLoop.java:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;



public class gameLoop extends Applet implements Runnable, KeyListener{



public int x, y;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background,foreground,w1,w2,w3,w4;

public void run() {
    x = 100;
    y = 100;
    background = ImageIO.read(new File("gameBackground.png"));
    foreground = ImageIO.read(new File("gameForeground.png"));
    w1 = ImageIO.read(new File("playerWalk1"));
    w2 = ImageIO.read(new File("playerWalk2"));
    w3 = ImageIO.read(new File("playerWalk3"));
    w4 = ImageIO.read(new File("playerWalk4"));
    while(true){
        if (left == true){
            x-=5;
        }if (right == true){
            x+=5;
        }if (up == true){
            y-=5;
        }if (down == true){
            y+=5;
        }
    repaint();
    try {
        Thread.sleep(20);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }

}

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == 37){
        left = true;
    }
    if (e.getKeyCode() == 38){
        up = true;
    }
    if (e.getKeyCode() == 39){
        right = true;
    }
    if (e.getKeyCode() == 40){
        down = true;
    }
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == 37){
        left = false;
    }
    if (e.getKeyCode() == 38){
        up = false;
    }
    if (e.getKeyCode() == 39){
        right = false;
    }
    if (e.getKeyCode() == 40){
        down = false;
    }
}

public void keyTyped(KeyEvent e) {}



}

and ImageIO.java:

import java.awt.image.BufferedImage;
import java.io.File;


public class ImageIO {



public static BufferedImage read(File file) {
    return null;
}



}

somehow I doubt you were supposed to write one yourself.
so, you tried to run that code without having an ImageIO class, and you don't know which one is/should be used.
this leads me to believe you did not write this code yourself.
where did you find it?

ImageIO is a class that is provided as part of the standard JavaSE installation. You just need to import it, as in

inport javax.imageio.ImageIO;

make that 'import' though :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.