Need some help with a basic java maze game!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 6228 Views - Last Post: 24 May 2012 - 11:37 AM Rate Topic: -----

#1 buffalo2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Need some help with a basic java maze game!

Posted 21 May 2012 - 03:52 PM

Ok so ive been working on a basic maze game. This is my first year with java so I am only going on what Ive learned in my class. What I am struggling with is drawing the maze. In my class we learned how to draw shapes new rectangle... and then filling it. So what I want to do is use rectangles to make a maze. However when I place the rectangles on the JFrame the rectangle is printed but the object I am trying to move i overwritten. I need help please....

This is the class Ive used to create the JFrame:

import javax.swing.JFrame;
import javax.swing.*;

public class macheads{
public static void main(String[] args){
{
   JFrame f = new JFrame();
   
   

   second object = new second();
   f.add(object);
   f.setVisible(true);
   mazeclass component = new mazeclass();
   f.add(component);
   f.setVisible(true);
   f.setSize(500,400);
   f.setLocationRelativeTo(null);
   f.setTitle("Maze Game");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   
  
}
}
}



This is the class that I am using to create the player piece (with key listeners to move it)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;


public class second extends JPanel implements ActionListener, KeyListener
{
   Timer t = new Timer(5, this);
   double x = 0, y = 0, velx = 0, vely = 0;
   
   public second(){
      t.start();
      addKeyListener(this);
      setFocusable(true);
      setFocusTraversalKeysEnabled(false);
   }
   
   //Draws/Paints 
   public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.fill(new Rectangle.Double(x, y, 40, 40));
      
   }
   
   public void actionPerformed(ActionEvent e){
   if(x > 500)
    {
       velx = 0;
       x = 500;
    }
    if(y<0)
    {
       vely = 0;
       y = 0;
    }
    if(x > 460)
    {
       velx = 0;
       x = 460;
    }

    
    
    x = x + velx;
    y = y + vely;
    repaint();
 }
   //the keys (up is negative down is positive)
   public void up(){
      vely = -1;
      velx = 0;
}
   public void down(){
      vely = 1;
      velx = 0; 
   }
   public void left(){
      velx = -1;
      vely = 0;
   }
   public void right(){
      velx = 1;
      vely = 0;
   }
   //this sets up how you know which key is used
   public void keyPressed(KeyEvent e){
      int code = e.getKeyCode();
      if (code == KeyEvent.VK_UP){
         up();
         
      }
      if (code == KeyEvent.VK_DOWN){
         down();
         
   }
      if (code == KeyEvent.VK_RIGHT){
         right();
}
      if (code == KeyEvent.VK_LEFT){
         left();
      }
   }
   public void keyTyped(KeyEvent e){}
   public void keyReleased(KeyEvent e) 
   {
      velx = 0;
      vely = 0;
   }
}




This is the class ive used to create the maze(the painted rectangles)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JComponent;



public class mazeclass extends JComponent{
   
   public void paintComponent(Graphics g)
{
   //Level One Graphics
      //Right wall 
      Graphics2D g2 = (Graphics2D) g;
   Rectangle rim2 = new Rectangle (650,0,5,900);
   g2.fill(rim2);
   g2.setColor(Color.BLACK);
   g2.draw(rim2);
   g2.fill(rim2);
   
 
}
}



Ive talked to my teacher about the problem ive been having but he has not worked with graphics much and had no idea. Ive tried everything that I can think of. Also is this possible because he did mention he wasnt sure if I could do it like that, but this is the only way I could think of...Thanks for you help!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with a basic java maze game!

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 310
  • Joined: 12-January 11

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 04:06 PM

I am a little rusty on my Java, but if you draw the moving object on the frame prior to drawing the maze itself, it (the maze) will cover up the player piece (assuming the player piece fits within the maze). Have you tried changing the order in which you add the objects?

This post has been edited by Psyguy: 21 May 2012 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 buffalo2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 04:14 PM

View PostPsyguy, on 21 May 2012 - 04:06 PM, said:

I am a little rusty on my Java, but if you draw the moving object on the frame prior to drawing the maze itself, it (the maze) will cover up the player piece (assuming the player piece fits within the maze). Have you tried changing the order in which you add the objects?



Hey So i tried what You said many times. However when I place the object I am moving below the rectangle boundary I does not print the boundary.

This is what it looks like when I keep the object in first before the rectangle. Notice the attachment and how the rectangle is cut off in the upper left corner...

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 230
  • Joined: 29-February 12

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:30 PM

first, the rectangle is cut off because you set the origins at 0,0, which (logically) should work, but truth is, you need to move it some to perhaps... 10,10 or 15,15 for it to start where you would think 0,0 should be (I'm not exactly sure why it does this, but I've had this problem before)
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:36 PM

Your design really doesn't make sense. Why is your player a JPanel and the maze a JComponent? You could simplify things a lot using JLabels and a GridLayout. The JLabels that are barriers can be one Color or display a specific ImageIcon. The main character is another ImageIcon, and spaces that can be traversed just blank. Then simply update the Icons of the JLabels rather than dealing with painting.
Was This Post Helpful? 1
  • +
  • -

#6 buffalo2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:42 PM

View PostBenzoate, on 21 May 2012 - 05:30 PM, said:

first, the rectangle is cut off because you set the origins at 0,0, which (logically) should work, but truth is, you need to move it some to perhaps... 10,10 or 15,15 for it to start where you would think 0,0 should be (I'm not exactly sure why it does this, but I've had this problem before)



When i change the starting location the object becomes even more hidden. :\

View Postmacosxnerd101, on 21 May 2012 - 05:36 PM, said:

Your design really doesn't make sense. Why is your player a JPanel and the maze a JComponent? You could simplify things a lot using JLabels and a GridLayout. The JLabels that are barriers can be one Color or display a specific ImageIcon. The main character is another ImageIcon, and spaces that can be traversed just blank. Then simply update the Icons of the JLabels rather than dealing with painting.



Like i said I am new to java. I also am doing this for a class so I cant really create a game with a topic we didn't cover :\. I would however appreciate any help you can give me with my current build! :D
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:44 PM

In this case, still simplify your design. A JPanel for your sprite doesn't make sense. Methods make a lot of sense here. I would use one JPanel and override the paintComponent() method (remember to invoke super.paintComponent() first). Then have a drawMaze() method to handle drawing your maze. After that, draw the sprite at the appropriate position.
Was This Post Helpful? 0
  • +
  • -

#8 buffalo2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:48 PM

View Postmacosxnerd101, on 21 May 2012 - 05:44 PM, said:

In this case, still simplify your design. A JPanel for your sprite doesn't make sense. Methods make a lot of sense here. I would use one JPanel and override the paintComponent() method (remember to invoke super.paintComponent() first). Then have a drawMaze() method to handle drawing your maze. After that, draw the sprite at the appropriate position.


Sorry for my noobyness but let me make sure I am understanding you correctly. First you are saying that I should get rid of my paintComponent() method and replace it with super.painComponent (not 100% sure what that is). Then you suggest I make a drawMaze() class. Now with that class how would suggest I go about it. Should I stick with the painting rectangles or is there an easier way I am overlooking? Appreciate the help
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:50 PM

Not saying get rid of yours. When you override the paintComponent() method, you are saying that your method should be called rather than the one inherited from JPanel. The super.paintComponent() call invokes the JPanel paintComponent() method to handle key superclass functionality.
class MazePanel extends JPanel{

    public void paintComponent(Graphics g){
         super.paintComponent(g);
         drawMaze(g);
         //now draw your sprite
    }

    private void drawMaze(Graphics g){}
}



In terms of drawing rectangles, I'd start by drawing lines to create your grid. Then fill in with rectangles where necessary.
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:51 PM

Java maze games are done like this.

int[][] map = {{2,0,0,1,0},
	       {0,0,0,1,0},
	       {0,0,0,1,0},
	       {0,0,0,1,0},
	       {0,0,0,1,0}};



You loop through the 2d array in paintComponents, painting your wall objects at 1. 2 is your player object. ah, here's a tutorial...

Maybe this is easier to understand.

This post has been edited by farrell2k: 21 May 2012 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#11 buffalo2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:53 PM

View Postmacosxnerd101, on 21 May 2012 - 05:50 PM, said:

Not saying get rid of yours. When you override the paintComponent() method, you are saying that your method should be called rather than the one inherited from JPanel. The super.paintComponent() call invokes the JPanel paintComponent() method to handle key superclass functionality.
class MazePanel extends JPanel{

    public void paintComponent(Graphics g){
         super.paintComponent(g);
         drawMaze(g);
         //now draw your sprite
    }

    private void drawMaze(Graphics g){}
}



In terms of drawing rectangles, I'd start by drawing lines to create your grid. Then fill in with rectangles where necessary.



The code above. It should go in the mazeclass with the paintComponent correct. Also. How can I make lines I am only familiar with rectangles and other basic shapes...
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Need some help with a basic java maze game!

Posted 21 May 2012 - 05:58 PM

See the documentation for the Graphics class.
http://docs.oracle.c...t/Graphics.html
Was This Post Helpful? 0
  • +
  • -

#13 buffalo2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: Need some help with a basic java maze game!

Posted 22 May 2012 - 10:39 AM

View Postmacosxnerd101, on 21 May 2012 - 05:58 PM, said:

See the documentation for the Graphics class.
http://docs.oracle.c...t/Graphics.html


based on the code you gave above. I am assuming that I need to make both the sprite and the barriers in the same class. So would I have to implement all the same things (like the key listeners). Like I said our teacher hasn't taught us much of this so a tutorial or step by step help would be really appreciated.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Need some help with a basic java maze game!

Posted 22 May 2012 - 03:25 PM

The KeyListener is the next step. Focus on drawing your maze first. It comes down to invoking a different method in the Graphics class (perhaps multiple times). Same for drawing the Sprite. Get that working first. If you know how to use some methods in the Graphics class, you can figure out how to use the others easily.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need some help with a basic java maze game!

Posted 23 May 2012 - 06:30 PM

View Postbuffalo2511, on 22 May 2012 - 01:39 PM, said:

based on the code you gave above. I am assuming that I need to make both the sprite and the barriers in the same class.

Not sure how you deduct that or you missed the concept. The same JPanel can display different Sprite and Barriers that do not have to be in the same class at all. Actually if Sprite move and Barrier do not this major difference just by itself should qualify them for different classes.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2