Results 1 to 5 of 5

Thread: Drawing a map

  1. #1
    Karp is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Drawing a map

    Hi

    I'm making an application, a little game, and I need to draw the map on the screen. The map is just a 2D array and I want to draw it using lines and such. I've managed to draw something on the screen but I need to pass the map to the drawing class and that I don't know how to do.

    Here are some of the main parts of the code (stripped and modified, but the important parts are there).

    The main program:
    Java Code:
    public class Game extends JFrame{
    
        public static void main(String[] args){
    
            Game game = new Game();
    
            Map map = new Map(20, 8);
    
            game.setSize(800, 600);
            DrawScreen ds = new DrawScreen();
            game.getContentPane().add(ds);
            game.setVisible(true);
        }
    
        public Game(){
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    A test drawer:
    Java Code:
    public class DrawScreen extends JPanel{
        public void paintComponent( Graphics g ){
            g.setColor( Color.RED );
            g.drawOval(0, 0, 100, 50);
        }
    }
    Now, this works just fine: it draws the oval. If I want to draw the map, I'd need to give the map to paintComponent as a parameter but I'm clueless about how to do it. I've looked around the net but haven't found a similar scenario. I understand it's possible to somehow add objects to the frame or something but that seemed unnecessarily complex for me, I just need a couple of for loops to draw the primitives. If you know the solution or an easier way, let me know. Thanks.
    Last edited by Karp; 11-06-2008 at 04:24 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    I need to pass the map to the drawing class
    You could pass the map in DrawScreen constructor:
    = new DrawScreen(map);

  3. #3
    Karp is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the answer.

    That works, though it seems like a bit of a waste to store the map data in DrawScreen as well, but that's not a big issue.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by Karp View Post
    Thanks for the answer.

    That works, though it seems like a bit of a waste to store the map data in DrawScreen as well, but that's not a big issue.
    Unless you clone the map, you're not duplicating any data anywhere, just creating another variable that references the same map.

    Brush up on the difference between objects and references.

    db

  5. #5
    Karp is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Ah yes, of course.

    I'm a bit rusty, it's been a few years since my last Java course.

Similar Threads

  1. Using Piccolo for Drawing
    By rstepler in forum Java 2D
    Replies: 2
    Last Post: 08-20-2013, 11:03 AM
  2. Problem with 3D drawing.
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 08-31-2008, 01:39 PM
  3. Demonstration of drawing an Arc in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:24 PM
  4. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 AM
  5. Help with Drawing a line
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •