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 4 of 4

Thread: 2d tile map

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2d tile map

    hello, i am currentlly trying to learn how to make 2d tile map in java. i have the code but for some reason it draws the map rotated to the right. Heres the code:
    package test;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
     
    /**
     *
     * @author User
     */
    public class Test extends JFrame{
     
    public int mapy=19;
     
     
    public static int[][] 
    map =               {{1, 1, 1, 1, 1, 1, 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},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 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, 1, 1, 1, 1}};
     
        public Test(){
     
           setVisible(true);
           setDefaultCloseOperation(EXIT_ON_CLOSE);
           setSize(500,500);
           getContentPane().setBackground(Color.white);;
        }
         public void paint(Graphics g){
     
            super.paint(g);
    Graphics2D g2d = (Graphics2D)g;
     
     
    for (int y = 0; y <= mapy; y++){
        for (int x = 0; x <= mapy; x++){
     
            int L = x * 64;
            int U = y * 64;
            int R = 64;
            int D = 64;
               /// g2d.rotate(Math.toRadians(-90));
     
            if (map[x][y] == 1){
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L+64, U, 8, 64);
     
            }
     
        }
     
    }
     
         }
        public static void main(String[] args) {
           new Test();
        }
    }
    Could someone explain whye did this happened?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 2d tile map

    Can you explain how the 1s in the array are supposed to control what is drawn?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 2d tile map

    Take a look at how you're accessing your 2d array. Plug in some values for x and y and see what value you're actually looking at.

    map[x][y]

    What value are you accessing if x is 3 and y is 2? What if x is 2 and y is 3? Does this jive with how you're mapping it to the screen?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d tile map

    Quote Originally Posted by Norm View Post
    Can you explain how the 1s in the array are supposed to control what is drawn?
    I dont understand what you mean?

    --- Update ---

    Ok i understood it now, but how should i fix it?
    Ok nevermind. just fixed it by writing map[y][x]

Similar Threads

  1. [SOLVED] Tile puzzle game getting tiles to move (help)
    By thatguy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 5th, 2013, 06:18 PM
  2. Replies: 1
    Last Post: November 27th, 2012, 08:55 AM
  3. Replies: 9
    Last Post: September 1st, 2012, 07:25 AM
  4. Tile Collision
    By Tim_Wilson in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 07:40 PM
  5. tile map help
    By chuckgod53 in forum Collections and Generics
    Replies: 1
    Last Post: February 10th, 2010, 12:17 PM