-1

I am writing a video game in Java (a horrible idea, yes I know). But I have run into a massive roadblock. I have written code that accesses a text document to output a 'dungeon' through the use of 1s and 0s. This is read into an multidimensional Array [imageArray] which then places my "wall' images. This works like a charm. What I am now trying to do is create a second array that can be accessed outside of the try/catch brackets. My problem is I have tried several different ways of accessing a secondary array, and I just need help.

Much appreciated.

This is the Code.

import javax.swing.*;  
import java.awt.*;  
import java.awt.event.*;
import java.util.Random;  
import java.io.*;
import java.util.Scanner;

public class main extends JPanel implements KeyListener, ActionListener
{ 

    Timer t = new Timer(8, this);

    int xCoord, yCoord;
    int x=1;
    int y=1;
    int d,v;
    private Image image1, image2, image3, image4;
    boolean pressed;


    Random ran = new Random();
    int cubeDimension = 40;
    int set = cubeDimension + 2;


    public main(){
    xCoord = 0;
    yCoord = 0;
    t.start();  
      addKeyListener(this);  
      setFocusable(true);
      setFocusTraversalKeysEnabled(false);

    }

    public void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            g.setColor(Color.black);
                g.fillRect(0,0, 1000,1000);         
            g.setColor(Color.white);
            for(int i = 0; i < 795; i += set){
                for(int e = 0; e < 595; e += set){
                    g.fillRect(i, e, cubeDimension, cubeDimension);
                }
            } 

              //random code  (ran.nextInt(9)+1)
            //ImageIcon m1 = new ImageIcon("monster1.jpg");
            //image2=m1.getImage(); 
            ImageIcon w = new ImageIcon("wall.jpg");
            image3 = w.getImage();
            ImageIcon j = new ImageIcon("Hero.jpg");
            image1 = j.getImage();

            try{
            Scanner file = new Scanner(new File("room.dat"));
            int cnt = file.nextInt();
            int cnt2 =file.nextInt();   
            int[][] imageArray = new int[cnt][cnt2];

            for(int i=0;i<cnt;i++)
            {
                yCoord=i;
                for(int c=0;c<cnt2;c++)
                {
                    xCoord=c;
                    int n = file.nextInt();
                    imageArray[i][c] = n;
                    mapArray[xCoord][yCoord]=n;
                    if (n==1)
                    {
                        g.drawImage(image3,c*set,i*set,null);
                    }
                    if (n==2)
                    {
                        g.drawImage(image1,c*set*x+5,i*set*y,null);
                    }   

                }


            }       
            }
            catch(FileNotFoundException e){
                System.out.println("Error");
            } 


        }


        //border code  
        public void keyReleased(KeyEvent e)
        {

        int code = e.getKeyCode();
            if(code==KeyEvent.VK_NUMPAD8)
                if(y>=0)
                    up();
            if(code==KeyEvent.VK_NUMPAD7)
                if(y>=0&&x>=0)
                    upleft();
            if(code==KeyEvent.VK_NUMPAD2)
                if(y<=14)   
                    down();
            if(code==KeyEvent.VK_NUMPAD1)
                    if(y<=14&&x>0)
                        downleft();
            if(code==KeyEvent.VK_NUMPAD6)
                if(x<=19)
                    right();
            if(code==KeyEvent.VK_NUMPAD3)
                if(x<=19&&y<=14)
                    downright();
            if(code==KeyEvent.VK_NUMPAD4)
                if(x>=0)
                    left();
            if(code==KeyEvent.VK_NUMPAD9)
                if(x<=19&&y>0)
                    upright();  
        }

        public void actionPerformed(ActionEvent e){
            repaint();
        }


        public void up(){ y= y-1 ;}
        public void upleft(){y= y-1;x= x-1;}
        public void down(){ y= y+1;}
        public void downleft(){ y= y+1; x= x-1;}
        public void left(){ x = x-1;}
        public void upright(){x = x+1;y= y-1;}
        public void right(){ x = x+1;}
        public void downright(){y= y+1;x = x+1;}


        public void keyTyped(KeyEvent e){}
        public void keyPressed(KeyEvent e){}
}
jeroen
  • 91,079
  • 21
  • 114
  • 132
Raulwicke
  • 3
  • 2
  • You probably shouldn't have file IO in your paintcomponent method unless you're actually reading in a different file everytime you paint. You should read that in then use the array to paint based on the info. You just need to declare the second array in a scope outside the try catch. – Carlos Bribiescas Apr 30 '14 at 18:08
  • 1
    Creating a game in java isn't that bad of an idea. Its got graphics card supported 3D graphics these days (through LWJGL or JMonkey); and can run them at high frame rates – Richard Tingle Apr 30 '14 at 18:10
  • possible duplicate of [Problem with "scopes" of variables in try catch blocks in Java](http://stackoverflow.com/questions/2854144/problem-with-scopes-of-variables-in-try-catch-blocks-in-java) – Lily Chung Apr 30 '14 at 18:16

1 Answers1

1

My problem is accessing a secondary array outside of the try/catch brackets.

Move imageArray before try block.

int[][] imageArray = null;

try{
        Scanner file = new Scanner(new File("room.dat"));
        int cnt = file.nextInt();
        int cnt2 =file.nextInt();   
        imageArray = new int[cnt][cnt2];
        ...

}catch(FileNotFoundException e){
    System.out.println("Error");
} 
Braj
  • 46,415
  • 5
  • 60
  • 76