# Making a pacman game having some problems with drawing

• June 1st, 2011, 06:06 AM
Matta
Making a pacman game having some problems with drawing
So im using a simple grid[][] ={
{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,1},
{1,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,1},
{1,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,1},
{1,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,1},
{1,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,1},
{1,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,1},
{1,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,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

And im going to be using the 1's as walls and the 0's as dots or open space. Yeah the maze will change and appear different then what it is there. Im just wondering how do i make draw in the walls and the dots?

I tried with this code :
Code :

```public void lagLabyrint(){ int x,y;   for (x=0; x<15; x+=blokkStr) { for (y=0; y<15; y+=blokkStr) { { if(brett[x][y]==0) { g.drawOval(x, y, getWidth(), getHeight()); g.setColor(dotFarge); //tegn dots } else if(brett[x][y]==1) { g.drawRect(x, y, getWidth(), getHeight()); g.setColor(veggFarge); //tegn vegger }         } } } }```

and lagLabyrint means makeLabyrinth or drawLabyrinth*

then i smashed it into my paint method
Code :

```public void paint(Graphics g) { super.paint(g);   Graphics2D g2d = (Graphics2D) g;   g2d.setColor(Color.black);   lagLabyrint();   g.dispose(); }```

But all i get now is a pump message displaying somehing is wrong and it cant draw it at all i just get lots of errors after i filled in the color statement above for dots and walls.

And yeah i have declared everything for it.

Edit: I found the MAIN PROBLEM, which is the drawing of the rectangle ... thats when im getting the stack overflow null pointer exception, and thats when my program crashes, i guess there is nothing wrong with all the variables as they work for the dots though they arent drawn onto the board. Anyway I really hope you guys can help me out with this one.

Thanks again!
• June 1st, 2011, 07:08 AM
Norm
Re: Making a pacman game having some problems with drawing
Quote:

i just get lots of errors
can you copy and paste the full text of the error messages here?
They contain information about what was wrong which is needed to fix the problem.

One problem I see, you need to pass a reference to the graphics object to the drawer:
lagLabyrint(g);
• June 1st, 2011, 07:18 AM
Matta
Re: Making a pacman game having some problems with drawing
This is the whole wall of text of the problem, so you suggest that i just add the g statement inside of it? I have created a graphics g; inside the board class or the board that contains the array list.

Code :

```Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Spillbrett.lagLabyrint(Spillbrett.java:85) at Spillbrett.paint(Spillbrett.java:120) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager\$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager\$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at java.awt.GraphicsCallback\$PaintCallback.run(Unknown Source) at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) at java.awt.Container.paint(Unknown Source) at java.awt.Window.paint(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities\$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access\$000(Unknown Source) at java.awt.EventQueue\$1.run(Unknown Source) at java.awt.EventQueue\$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Spillbrett.lagLabyrint(Spillbrett.java:85) at Spillbrett.paint(Spillbrett.java:120) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager\$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager\$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at java.awt.GraphicsCallback\$PaintCallback.run(Unknown Source) at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) at java.awt.Container.paint(Unknown Source) at java.awt.Window.paint(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities\$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access\$000(Unknown Source) at java.awt.EventQueue\$1.run(Unknown Source) at java.awt.EventQueue\$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)```
• June 1st, 2011, 07:21 AM
Norm
Re: Making a pacman game having some problems with drawing
Quote:

at Spillbrett.lagLabyrint(Spillbrett.java:85)
What variable is null at line 85 in Spillbrett.java?
Backtrack in your code to see why the variable does NOT have a value.

Quote:

I have created a graphics g
You should use the one passed to the paint method.
• June 1st, 2011, 07:24 AM
Matta
Re: Making a pacman game having some problems with drawing
Double Posting here but thanks man i got it working once i used ur statement how silly of me to not come up with that, well another problem appears i now only get a small square in the middle of the screen did i do something wrong in the code then? Because its all working and launching no error message so you can look away from my early post.

But im using a Dimension d; to make the boards or screensize maybe i should remove it and just use the array as the screensize? or something ?

Screen: {
{b,b,b,b,b,b,b,b,b,b,b,b,b},
{b,b,b,b,b,b,b,b,b,b,b,b,b},
{b,b,b,b,w,w,w,w,w,b,b,b},
{b,b,b,b,w,b,b,b,b,w,b,b,b},
{b,b,b,b,w,b,b,b,b,w,b,b,b},
{b,b,b,b,w,w,w,w,w,w,b,b,b},
{b,b,b,b,b,b,b,b,b,b,b,b,b},
{b,b,b,b,b,b,b,b,b,b,b,b,b},}

looks like tht but in a larger scale with dimension 600,500 and a smaller white frame
• June 1st, 2011, 08:40 AM
Norm
Re: Making a pacman game having some problems with drawing
Quote:

i now only get a small square in the middle of the screen
Is that what your method draws?
Try debugging your code by adding println statements to show the values of the variables used to control where the drawing takes place.

Hard to say what the problem is without seeing the code. Make a small program to demonstrate your problem, don't post a lot of code.
• June 1st, 2011, 09:50 AM
Matta
Re: Making a pacman game having some problems with drawing
well the problem lies within the code i allready posted it seems, i use this 2D-arraylist which described and i want it to draw lines at the number 1 numbers so i get a line all around the game if not possible thats okei ill just make the screen outer lines so that its not possible to move beyond em. I want the number 1 to draw lines which makes squares or just drawRect(); One of either so i get some sort of map/maze for the pacman to move about. That way its easier to make colission detection without having alot of objects instead you got one variable to add the colission to.

My method now draws a small screen in the middle of the program(which is a black screen); and its just a small square in the middle of it. Thats not what i want it to be i want the grid to fill the whole screen ofcourse. So that i can map out where i want the walls etc etc. but as of now it seems my grid does not apply to the whole screen or atleast thats what im getting at.. ill write some quick code her

Code :

```public class View extends JFrame{ public View{   add(new Spillbrett(); this.setSize(int,int); // with numbers ofc this.setLocationRelativeTo(null); this.setTitle(Pacman.Beta); this.setResizeable(false); this.setVisible(true);     } }   public class Spillbrett implements JPanel   Ghost ghost; //outer class in its own window(describing movement and creation) Pacman pacman; // oute class in its own window(describing movement and creation) int blocksize = 20;   int [][] board = { {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,1}, {1,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,1}, {1,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,1}, {1,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,1}, {1,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,1}, {1,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,1}, {1,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,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};   public void makeBoard(Graphics2D g){ int x,y;   for (x=0; x<16; x+=blokkStr) { for (y=0; y<brett.length; y+=blokkStr) { { if(brett[x][y]==1) { g2d.drawLine(x+16*blokkStr,y+16*blokkStr,blokkStr, blokkStr); g2d.setColor(veggFarge); //tegn vegger } } }   public void paint(Graphics g) { super.paint(g);   Graphics2D g2d = (Graphics2D) g;   lagLabyrint(g2d);   g.dispose(); } }```

this is a small part of the code :)
• June 1st, 2011, 10:05 AM
Norm
Re: Making a pacman game having some problems with drawing
You need to post code that will compile and execute. That helps seeing what the problem is.

The code you posted has several syntax errors in it.
• June 1st, 2011, 10:46 AM
Matta
Re: Making a pacman game having some problems with drawing
Okei here we go then ;)

this will be somewhat big but, thanks alot in advance norm ur helping out alot ;)!

Code :

```import javax.swing.JFrame;     public class View extends JFrame {   public View(){   add(new Board()); this.setSize(500,500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setResizable(false); this.setVisible(true);   }   public static void main (String[] arg0){ new View(); } }```

Code :

```import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D;   import javax.swing.JPanel;     public class Board extends JPanel {   Pacman pacman; Ghost ghost;   int blockSize = 24; Color wallColor = Color.blue;     int [][] board ={ {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,1}, {1,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,1}, {1,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,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1}, {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1}, {1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,1}, {1,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,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};   public Board() {   setBackground(Color.BLACK); //addKeylistener(keyListener);   }   public void makeMaze(Graphics g){   int x,y;   for (x=0; x<16; x+=blockSize) { for (y=0; y<board.length; y+=blockSize) { { if(board[x][y]==1) { g.drawRect(x+16*blockSize,y+16*blockSize,blockSize, blockSize); g.setColor(wallColor); //draw the walls as lines first (maybe rects not sure yet) }         } } } } public void playGame(){ //if (dead){ //pacman.death();(pacman is dead display how he died i.e by hitting ghosts!) intersection //}else { //pacman.MovePacman(); (Moves pacman around with the directions of the board); //pacman.DrawPacman(); (draws pacman as a drawOval(10,10,10,10)) in the pacman class //ghost.MoveGhost(); (move the ghosts in random directions) //checkMaze(); (checks wether dots are eaten or not) //} } public void paint(Graphics g) { super.paint(g);   Graphics2D g2d = (Graphics2D) g;   makeMaze(g2d);   g.dispose(); } }```
Code :

```public class Ghost {   }```
Code :

```public class Pacman {   }```

okei there it is easy and simple:)
• June 1st, 2011, 12:17 PM
Norm
Re: Making a pacman game having some problems with drawing
I don't see any comments in the code so I don't know what its supposed to do.

The contents of the window is all black.
• June 1st, 2011, 12:58 PM
dlorde
Re: Making a pacman game having some problems with drawing
That makeMaze nested loop is screwy from the start - you're looping while x is less than 16, but you increment x each time by blockSize which is 24... guess how many times that's going to loop?

I suggest you get some graph paper and a pencil, and figure out your maze drawing algorithm by hand. Once you understand it and it's working, then translate it into code.

Hint: increment x and y by 1 each time, don't use blockSize in the loop control. Oh, and it will probably work better if the board is square (height == width). Oh, and you're drawing it sideways - you need:

if (board[y][x] == 1) {.
• June 3rd, 2011, 06:31 AM
Matta
Re: Making a pacman game having some problems with drawing
Quote:

Originally Posted by dlorde
That makeMaze nested loop is screwy from the start - you're looping while x is less than 16, but you increment x each time by blockSize which is 24... guess how many times that's going to loop?

I suggest you get some graph paper and a pencil, and figure out your maze drawing algorithm by hand. Once you understand it and it's working, then translate it into code.

Hint: increment x and y by 1 each time, don't use blockSize in the loop control. Oh, and it will probably work better if the board is square (height == width). Oh, and you're drawing it sideways - you need:

if (board[y][x] == 1) {.

Yeah i have allready done so from the start belive me i have tried to get the loop to work tried with the increment 1 and the blockSize, they paint the same image, nothing is changing. And removed all the blockSize things from the other code aswell then i get some slight changes but i belive the main Issue is the "If" method which outprints/draws it wrong or cant get the right numbers from the Board[][]. Doesnt matter if the board/ dimension is square or not, atleast not when you execute it, it seems.

The drawing is still fucked up, thanks for the help though and the [y][x] instead of [x][y] didnt do much either .. there gotta be me not figuring out how to draw that line/rect correctly as it doesnt fit into where the numbers 1 are placed ...

Maybe its better to go with the standard method and just draw in all the rects and then place them around on the map i.e set Coordinates for all of them.
• June 3rd, 2011, 06:44 AM
Norm
Re: Making a pacman game having some problems with drawing
One problem is that the board is not square. The for loop tests have the wrong end of loop test. I think it should be:
for (y=0; y<board[x].length; y++)

You don't say if your code is working or if you are having problems and what they are.
• June 3rd, 2011, 07:11 AM
Matta
Re: Making a pacman game having some problems with drawing
Edit:

The problem is atleast what i can comprehend from the code is the tileMap board map or 2d array list isnt putting itself inside the JFrame Correctly since it draws a wierd small retangular object where i place the coordinate if i keep it

Code :

```if(board[y][x]==1){ g.drawRect(x,y,blockSize,blockSize); g.setColor(wallColor); }```
if I use this i will get it where the x and y coordinates is which is 0,0 at that time yes, then it will draw a rectangular unit right there with the apropriate block size it doesnt seem to place it at the 1s i have made in the 2d arraylist.
• June 3rd, 2011, 07:18 AM
Norm
Re: Making a pacman game having some problems with drawing
Quote:

it doesnt draw em up like it should its just a big blue line + a rectangle around it with nothing more in it.
Can you screen print and post it.
Here's what I get with my version:
• June 3rd, 2011, 08:05 AM
Matta
Re: Making a pacman game having some problems with drawing
My scrnPrnt doesnt work... -_- on this comp.

So i used my phone..
Attachment 577Attachment 578

Thats what i have ..
• June 3rd, 2011, 08:50 AM
Norm
Re: Making a pacman game having some problems with drawing
You need to figure out where the x,y locations are for each square you want to print.
Take a piece of paper and draw what you want the output to look like. Just the outer border for now.
Then extract the x,y location of the upper left corner for each square. Then think about how to write a loop that will generate the x,y locations for all the squares across a row and down a column.

This has already been suggested by dlorde.

As an exercise, post here the x,y values for the first 4 squares on the top row.
• June 3rd, 2011, 10:02 AM
dlorde
Re: Making a pacman game having some problems with drawing
Quote:

Originally Posted by Matta
Yeah i have allready done so from the start belive me i have tried to get the loop to work tried with the increment 1 and the blockSize, they paint the same image, nothing is changing. And removed all the blockSize things from the other code aswell then i get some slight changes but i belive the main Issue is the "If" method which outprints/draws it wrong or cant get the right numbers from the Board[][]. Doesnt matter if the board/ dimension is square or not, atleast not when you execute it, it seems.

The drawing is still fucked up, thanks for the help though and the [y][x] instead of [x][y] didnt do much either ..

OK, so maybe I don't know what you actually want to display; because when I made the changes I suggested to the code you posted, I got the image Norm links to above, but the right way up - and it follows the maze array layout exactly.
• June 4th, 2011, 08:26 AM
Tjstretch
Re: Making a pacman game having some problems with drawing
Ok not sure if you fixed it but this looks a little funky.

Code Java:

```public void paint(Graphics g) { super.paint(g); // You tell the super class JPanel to paint all the things done to g, but Graphics2D g2d = (Graphics2D) g; // then create Graphics 2d from graphics g and makeMaze(g2d); // use that to draw. }```
Try using
Code Java:

```public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; super.paint(g2d); makeMaze(g2d); }```
• June 4th, 2011, 10:21 AM
copeg
Re: Making a pacman game having some problems with drawing
Quote:

Ok not sure if you fixed it but this looks a little funky.
What makes you think this looks funky? Looks perfectly legit to me (aside from the fact that the programmer should override paintComponent rather than paint)
• June 4th, 2011, 06:49 PM
dlorde
Re: Making a pacman game having some problems with drawing
Quote:

Originally Posted by Tjstretch
Ok not sure if you fixed it but this looks a little funky.

<snip code>

Try using

<snip code>

Whut? Your change makes no difference at all - and why should it? it's the same object and the superclass paint method takes a Graphics argument anyhow...

I'd do it like this (but that's just me):
Code :

```public void paint(Graphics g) { super.paint(g); makeMaze((Graphics2D)g); }```
• June 9th, 2011, 07:51 AM
Matta
Re: Making a pacman game having some problems with drawing
Thanks guys i fixed it after some searching throug my head so as of now that problem is fixed actually did it a few days ago :) so i got the same screen output as you got. The problem is if you fit in more into the table( f.eks add a nr 3 for pacman) then ull get some strange results in there the wall behind will turn yellow if you color pacman yellow!
• June 9th, 2011, 06:18 PM
Norm
Re: Making a pacman game having some problems with drawing
Quote:

the wall behind will turn yellow
Is that the result of on too many setColor()?
Or perhaps not enough calls to setColor()?