Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Use 2D array to layout map with pictures

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Use 2D array to layout map with pictures

    Hello everyone,

    Please forgive me for my mistakes since I am a beginning programmer. Here's my situation:
    I'm making a Pacman look-a-like game, now I'm making the map out of an 2D array (as a grid).
    I already initialized the 2d array spots with "0" and "1". I managed to do this but now I'm stuck.

    I want to use the "0" and "1" to print the map. For example the "0" are grass and the "1" are walls.
    The map should be printed within a JFrame. And I would like to make the width and height 32x32 pixels.

    I searched on the internet and i found a couple of example codes but non of it seems to work properly.
    This is the code I'm using to make the 2D array and initialize the spots.:


    public class Level1 extends javax.swing.JFrame{
     
        final int ROWS = 17;
        final int COLS = 17;
        int[][] field = new int[ROWS][COLS];
     
        public Level1() {
            initComponents();
            setLocationRelativeTo(null);
            JPanel panel = new JPanel();
     
            int[][] field = new int[][]{
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            };     
        }
     
        public void makeMap(){
            String output = "";
            for (int row = 0; row < ROWS; row++) {
                for (int col = 0; col < COLS; col++) {
                    output += " " + field[row][col];
                }
                //somekind of code?
            }
        }
     
        protected void paintComponent(Graphics g) {
     //what do i put here?
        }
     
        private void draw(Graphics g) {
           //what do i put here?
            }
        }
        }
    }

    Can anyone help me with this because I'm really stuck here.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Use 2D array to layout map with pictures

    I'm really stuck here.
    Can you explain what the problem is?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Use 2D array to layout map with pictures

    Read the api for the graphics class. There are all kinds of methods which you can use in the paintCompnent method to paint your custom JFrame.

Similar Threads

  1. Replies: 36
    Last Post: February 22nd, 2014, 04:59 AM
  2. GUI Layout - Does anyone know how to set a layout?
    By mikemontesa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 04:35 PM
  3. How to create an array of Map<String, Object>
    By lalossa in forum Collections and Generics
    Replies: 1
    Last Post: February 17th, 2013, 07:31 AM
  4. Replies: 9
    Last Post: September 1st, 2012, 07:25 AM
  5. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM

Tags for this Thread