I'm trying to write a program that allows a user controlled character to move around a maze. That problem i'm having is having it determine when it runs into a wall. I have a code done up to handle it. I'm just running into some errors i need help cleaning up

public int CheckPixel(int x, int y) {
	File file = new File("Zombie_Map.jpg");
	BufferedImage image = ImageIO.read(file);
    	clr = image.getRGB(x,y); 
	red   = (clr & 0x00ff0000) >> 16;
	green = (clr & 0x0000ff00) >> 8;
	blue  =  clr & 0x000000ff;
    }

This is the code i'm using currently to test if a pixel is a certain colour, in this case the walls are black also (Zombie_Map) is just a mat version of the maze i made, nothing moves it just makes it easier to run the program.

This is the program i'm using for the user controlled character

public void keyPressed(KeyEvent e) {
        switch( e.getKeyChar() ) {
            case 'a':
		int xValue = (int)human.x;
		int yValue = (int)human.y;
		CheckPixel(xValue-10,yValue); 
		System.out.println(xValue + " " + yValue);
		if ((red == 0.0)&&(blue == 0.0)&&(green == 0.0)){
		    break;
		}
                else {
                    human.x -= 20.0;
                }
        }
        repaint();

So that when i press the 'a' key, the character should move 20 pixels to the left unless there's a wall in it's way, represented by 0,0,0 on a RGB scale. So in this case i test 10 pixels to the left of the cursor's current position

However when i run the program like this i get these errors

javac ZombieEscape.java
ZombieEscape.java:170: missing return statement
    }
    ^
ZombieEscape.java:165: unreported exception java.io.IOException; must be caught or declared to be thrown
	BufferedImage image = ImageIO.read(file);
	                                  ^
2 errors

I will admit, exceptions, and throwing and catching are not my strong suit when it comes to coding, I was just wondering if someone would be able to help me out here.

Recommended Answers

All 23 Replies

missing return statement

The compiler thinks there should be return statement where it shows this error.

unreported exception java.io.IOException; must be caught or declared to be thrown

Enclose that statement in a try catch block. Be sure to add a call to the printStackTrace() method in that catch block.

So, when you say "Enclose that statement" do you mean the CheckPixel class? also, what's a printStackTrace() method?

No, a statement is not the same as a class.

what's a printStackTrace() method?

Its a method in the Throwable class the prints the stack for tracing.

public int CheckPixel(int x, int y) throws IOException{
	File file = new File("Zombie_Map.jpg");
	BufferedImage image = ImageIO.read(file);
    	clr = image.getRGB(x,y); 
	red   = (clr & 0x00ff0000) >> 16;
	green = (clr & 0x0000ff00) >> 8;
	blue  =  clr & 0x000000ff;
	System.out.println(red + " " + green + " " + blue);
	return red;

To distinguish the colour ^^

case 'a':
		int xValue = (int)human.x;
		int yValue = (int)human.y;
		try {
		    CheckPixel(xValue-10,yValue); 
		    System.out.println(xValue + " " + yValue);
		    if ((red == 0)&&(blue == 0)&&(green == 0)){
		        break;
		    }
		}
		catch (IOException err) { 
		    System.out.println(err.getMessage());
		}

This is what i have now, and in the case of two areas it works....
What is going wrong now?

What is going wrong now?

Please explain. Does it compile?
Does it execute?

Copy and post the full text of errors here.

Nevermind, i fixed the error, Simple drawing was wrong.
Now i'm wondering however if there's a way to make an array of shapes.
Because i'm trying to make 10 circles move randomly in a maze,

int randomInt = randomGenerator.nextInt(4);
	    switch( randomInt ) {
		case 0:
		    xValue = (int)zombie1.x;
		    yValue = (int)zombie1.y;
		    try {
		        CheckPixel(xValue-10,yValue); 
		        if ((red == 0)&&(blue == 0)&&(green == 0)){
		            break;
		        }
		    }
		    catch (IOException err) { 
		        System.out.println(err.getMessage());
		    }
		    zombie1.x -= 20.0;
		    break;

I have this, which works wonderfully, except it just moves one shape, in one direction. Is there anyway i could use the same code but just cycle all 10 shapes through it? they're all named zombie1... zombie10

If the shapes are defined in a class, they could all be put into an ArrayList and the drawing code could cycle through the contents of the ArrayList.

in that case what would i set up the arraylist like so?

ArrayList<Object> Zombies = new ArrayList<Object>();

Why not have the ArrayList contain Zombies vs Objects?
The name should start with lowercase letter.

i thought that it had to be something specific, like an int, or double. or something like that...

It has to be a class, not a primitive. Your class should work. try it.

Okay, so i've got the array set up, i just seem to be having trouble now calling on it i get this error

ZombieEscape.java:190: array required, but java.util.ArrayList<java.awt.geom.Ellipse2D.Double> found
            g2.fill( zombs[i] );

Disregard that last message,
I've made two arrays, one with the x coordinates and one with the y coordinates of each "Zombie"

for (int i = 0; i <= xZombies.length; i++){
	    if ((xZombies[i] == human.x)&&(yZombies[i] == human.y)){
	        Caught();
	    }
	}

When i try and run the code above, which i think should work i get this assortment of errors

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
	at ZombieEscape.<init>(ZombieEscape.java:136)
	at ZombieEscape.main(ZombieEscape.java:403)

Remember array indexes go from 0 to the length-1 (not to length) Use: < vs <=

Oh right, my bad

int randomInt = randomGenerator.nextInt(4);
	    System.out.print(randomInt + " ");
	    switch( randomInt ) {
		case 0:
		    xValue = (int)zombie1.x;
		    yValue = (int)zombie1.y;
		    try {
		        CheckPixel(xValue-10,yValue); 
		        if ((red == 0)&&(blue == 0)&&(green == 0)){
		            break;
		        }
		    }
		    catch (IOException err) { 
		        System.out.println(err.getMessage());
		    }
		    zombie1.x -= 20.0;
		    break;

Now, i;m trying to use those arrays for this part of a code, such that, every time the number 0 is chosen all the zombies will move 20 pixels to the right, (if possible) but when i try and use the loop it causes them to start phasing through walls and such.

You need to test if the move will go through a boundary and if so, not make the move.

public int CheckPixel(int x, int y) throws IOException{
	File file = new File("Zombie_Map.jpg");
	BufferedImage image = ImageIO.read(file);
    	clr = image.getRGB(x,y); 
	red   = (clr & 0x00ff0000) >> 16;
	green = (clr & 0x0000ff00) >> 8;
	blue  =  clr & 0x000000ff;
	return red;
    }
switch( randomInt ) {
		case 0:
		    for(int n = 0; n < xZombies.length; n++){
		    	xValue = (int)xZombies[n];
		    	yValue = (int)yZombies[n];
		    	try {
		            CheckPixel(xValue-10,yValue); 
		            if ((red != 0)&&(blue != 0)&&(green != 0)){
		    	        zombie1.x -= 20.0;
		    	        zombie2.x -= 20.0;
		    	        zombie3.x -= 20.0;
		    	        zombie4.x -= 20.0;
		    	        zombie5.x -= 20.0;
		    	        zombie6.x -= 20.0;
		    	        zombie7.x -= 20.0;
		    	        zombie8.x -= 20.0;
		    	        zombie9.x -= 20.0;
		    	        zombie10.x -= 20.0;
		    	        break;
		            }
		        }
		        catch (IOException err) { 
		            System.out.println(err.getMessage());
		        }
		    }
		    break;

I made this, which i thought should work, if the number 0 came up, it would go through all the x and y values in the arrays xZombies and yZombies (respectively) and check if they can or cannot move, i also know that this class works when dealing with just one zombie, so i'm a tad confused why it doesn't work for all of them, some phase through the walls anyway

That list of variables looks like it should be an array in a loop.

why it doesn't work for all of them,

Only if they are all at the same place.
You will need code for each zombie that uses its location.
You need to create a zombie class that knows its location and can move itself and keep itself in bounds. Your current way of coding with all the variables will be a dead end.

damn... alright, i'll have to fix that then

It will be much simpler when there is a class to control everything.

yea i imagine there would be. I'm just trying to make it all work first before i condense it down.

Hey, i got all the zombies to finally move around the way i like. Now i'm having trouble with something i feel i shouldn't.

I've made two classes, one with my actual game and one with a menu bar, with save, and quit and the rules and everything, and i can't figure out how to get both of them to run from calling on just ZombieEscape.java (my actual game class) I've extended App (the menu) to include ZombieEscape, and when i run it it tells me nothing is wrong, but it's not working at all

it's not working at all

What are you going to do then?

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.