Results 1 to 5 of 5
Thread: Drawing a map
- 11-06-2008, 03:04 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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:
A test drawer: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); } }
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.Java Code:public class DrawScreen extends JPanel{ public void paintComponent( Graphics g ){ g.setColor( Color.RED ); g.drawOval(0, 0, 100, 50); } }Last edited by Karp; 11-06-2008 at 03:24 PM.
- 11-06-2008, 04:11 PM #2
You could pass the map in DrawScreen constructor:I need to pass the map to the drawing class
= new DrawScreen(map);
- 11-06-2008, 04:23 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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.
- 11-07-2008, 08:37 AM #4
- 11-07-2008, 12:26 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Problem with 3D drawing.
By Supamagier in forum Advanced JavaReplies: 0Last Post: 08-31-2008, 12:39 PM -
Using Piccolo for Drawing
By rstepler in forum Java 2DReplies: 0Last Post: 07-01-2008, 12:51 AM -
Demonstration of drawing an Arc in SWT
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:24 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM -
Help with Drawing a line
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks