# I'm not sure how to get started here...game

Show 40 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 05-15-2013, 05:21 PM
Tolls
Re: I'm not sure how to get started here...game
Sounds good to me.
Or just have a Door[] of size 4 (since IIRC you only have 4 doors). That'll make the mouse checking easier as you can simply loop.
• 05-15-2013, 09:13 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
Sounds good to me.
Or just have a Door[] of size 4 (since IIRC you only have 4 doors). That'll make the mouse checking easier as you can simply loop.

See, now we never worked with arrays other than 1 time where it was just used and not entirely explained. So basically from what I'm reading it is just like storing a list of things. So this makes sense to use.

So from examples I'm seeing, it would be something like

Door doors[] = new Door[4];?

Should this be in my new class, or still in my Door constructor. No wait, if it's in my Door constructor it would be an infinite loop wouldn't it.

Then to make my separate doors it would be something like *now in my Board() class

Code:

```    int x1 = 20;     int x2 = 250;     int x3 = 480;     int x4 = 710;     int placementY = 371;     int wide = 200;     int tall = 350;         Door[] doors = new Door[4];{         doors[0] = new Door(x1, placementY, wide, tall);     doors[1] = new Door(x2, placementY, wide, tall);     doors[2] = new Door(x3, placementY, wide, tall);     doors[3] = new Door(x4, placementY, wide, tall);```
Look about right? or is there a better way to approach this?
• 05-16-2013, 09:50 AM
Tolls
Re: I'm not sure how to get started here...game
Yep.
Your Board has the array of Doors (each Door doesn't, or shouldn't, care about the other Doors).
• 05-16-2013, 12:20 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
Yep.
Your Board has the array of Doors (each Door doesn't, or shouldn't, care about the other Doors).

Alright great! So now I've made my mouseListener and mouseMotionListener...this is what my Board class looks like now

Code:

```/**  * @author John  */ public class Board extends JPanel {       int x1 = 20;     int x2 = 250;     int x3 = 480;     int x4 = 710;     int placementY = 371;     int wide = 200;     int tall = 350;         Door[] doors = new Door[4];{         doors[0] = new Door(x1, placementY, wide, tall);     doors[1] = new Door(x2, placementY, wide, tall);     doors[2] = new Door(x3, placementY, wide, tall);     doors[3] = new Door(x4, placementY, wide, tall);     }         public Board(){             addKeyListener( new KListener());        addMouseListener( new Board.MListener());     addMouseMotionListener( (MouseMotionListener)this.getMouseListeners()[0]);     }         private class KListener extends KeyAdapter{         public void keyPressed(KeyEvent e){             switch( e.getKeyCode()){                 case KeyEvent.VK_ESCAPE:                 System.exit(0);             }         }     }         private class MListener extends MouseAdapter {         public void mouseMoved( MouseEvent e){             int mx;             int my;                         mx = e.getX();             my = e.getY();               System.out.println("x = " + mx + " y = " + my);         } }         public void mouseClicked(MouseEvent e){             int cx;             int cy;             switch(e.getID()){                 case MouseEvent.MOUSE_CLICKED:                                         cx = e.getX();                     cy = e.getY();                                         System.out.println("You've clicked here " + cx + " " + cy);                     break;             }             }         }```
Should I have not created a // public Board(){
mouse listeners/motion listeners
}
?

Should I have just kept those under where the array is in the public class Board() extends JPanel{

}
?
• 05-16-2013, 01:46 PM
Tolls
Re: I'm not sure how to get started here...game
I'm not sure what you're asking there.
• 05-16-2013, 05:31 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
I'm not sure what you're asking there.

I'll just ignore it for now because there are no errors as it is so I'll see if it gives me a problem afterward.

Anyways...so that's my Board class now...with my mouseListener and mouseMotionListener

Now I remember you saying it would make it easier to make an array because I could simply loop to see if the mouse would be in the doors.
How would I go about doing that? like a "for" loop?

Code:

```for(int i = 0; i < doors.length; i++){ }```
and then inside that for loop, would I have an "if" statement? ie if(mx >= x1 && mx <= x1 + wide) ...etc...

or is there a better way to do it?
• 05-16-2013, 05:42 PM
Tolls
Re: I'm not sure how to get started here...game
I would put the checking code in the Door class, just because the Door already knows where it is.
Pass in the mouse coords, return true if it's in the Door, false if not.
• 05-16-2013, 06:18 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
I would put the checking code in the Door class, just because the Door already knows where it is.
Pass in the mouse coords, return true if it's in the Door, false if not.

Isn't that what this code, that is in my door class, is?:

Code:

```public boolean mouseInRectangle( int mx, int my){         return(mx>= corner_.x_ && mx<=(corner_.x_+dim_.x_))&&               (my>= corner_.y_ && my<=(corner_.y_+dim_.y_));```
i'm not sure on this part....

The mouse coordinates are determined in the Board class because that is where the mouseMotionListener is, how do I bring over the mouse coordinates?

can you please explain a little more on this step?
• 05-16-2013, 06:27 PM
Tolls
Re: I'm not sure how to get started here...game
Oh yes.
So you would call that method on each door in the array:
Code:

```for(int i = 0; i < doors.length; i++){     if (doors[i].mouseInRectangle(mx, my)) {         // do whatever     } }```
'mx' and 'my' are the mouse coordinates, so replace as necessary with whatever you've called them in the listener.
• 05-16-2013, 07:07 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
Oh yes.
So you would call that method on each door in the array:
Code:

```for(int i = 0; i < doors.length; i++){     if (doors[i].mouseInRectangle(mx, my)) {         // do whatever     } }```
'mx' and 'my' are the mouse coordinates, so replace as necessary with whatever you've called them in the listener.

I've actually now made it so in my mouseListener, the variables are mx and my...

so now, this for loop is in my Board class correct? because that is where my array and my Listener are, so it would have to go there correct?

So now my board class looks like:

Code:

```/**  * @author John  */ public class Board extends JPanel {       int x1 = 20;     int x2 = 250;     int x3 = 480;     int x4 = 710;     int placementY = 371;     int wide = 200;     int tall = 350;     int mx;        //mouse moved x position     int my;        //mouse moved y position     int cx;        //mouse clicked x position     int cy;        //mouse clicked y position         Door[] doors = new Door[4];{         doors[0] = new Door(x1, placementY, wide, tall);     doors[1] = new Door(x2, placementY, wide, tall);     doors[2] = new Door(x3, placementY, wide, tall);     doors[3] = new Door(x4, placementY, wide, tall);   for(int i=0; i < doors.length; i++){       if( doors[i].mouseInRectangle(mx, my)){                     System.out.println("You are in a door!");       }                   } }                  public Board(){                addKeyListener( new KListener());        addMouseListener( new Board.MListener());     addMouseMotionListener( (MouseMotionListener)this.getMouseListeners()[0]);         }         private class KListener extends KeyAdapter{         public void keyPressed(KeyEvent e){             switch( e.getKeyCode()){                 case KeyEvent.VK_ESCAPE:                 System.exit(0);             }         }     }         private class MListener extends MouseAdapter {         public void mouseMoved( MouseEvent e){                         mx = e.getX();             my = e.getY();             // System.out.println("x = " + mx + " y = " + my);         } }         public void mouseClicked(MouseEvent e){                         switch(e.getID()){                 case MouseEvent.MOUSE_CLICKED:                                         cx = e.getX();                     cy = e.getY();                                         System.out.println("You've clicked here " + cx + " " + cy);                     break;             }             }         }```
Now when I run this code as-is.....my window shows up *obviously nothing on it* but when I move my mouse to where the doors will be placed, it just shows the coordinates...because I have

Code:

`// System.out.println("x = " + mx + " y = " + my);`
to show where the mouse is...but for now...this code so far is not picking up where the doors will be placed, because I have added a

Code:

`// System.out.println("You are in a door!");`
any idea what I'm doing wrong here? Am I putting this all in the wrong place?
• 05-17-2013, 04:18 AM
johnweldon1993
Re: I'm not sure how to get started here...game
After checking some other things, I've fixed a problem I was having with my mouseClicked method. simply enough it was just outside the scope of my mouseListener. So now that is fixed. My Board class now looks like

Code:

```/**  * @author John  */ public class Board extends JPanel {       int x1 = 20;     int x2 = 250;     int x3 = 480;     int x4 = 710;     int placementY = 371;     int wide = 200;     int tall = 350;     int mx;        //mouse moved x position     int my;        //mouse moved y position     int cx;        //mouse clicked x position     int cy;        //mouse clicked y position         Door[] doors = new Door[4];{         doors[0] = new Door(x1, placementY, wide, tall);     doors[1] = new Door(x2, placementY, wide, tall);     doors[2] = new Door(x3, placementY, wide, tall);     doors[3] = new Door(x4, placementY, wide, tall);       for(int i=0; i < doors.length; i++){       if( doors[i].mouseInRectangle(mx, my)){                       System.out.println("You are in a door!");       }                   } }                  public Board(){                addKeyListener( new KListener());        addMouseListener( new Board.MListener());     addMouseMotionListener( (MouseMotionListener)this.getMouseListeners()[0]);     setBackground(Color.WHITE);     setFocusable(true);     }         private class KListener extends KeyAdapter{         public void keyPressed(KeyEvent e){             switch( e.getKeyCode()){                 case KeyEvent.VK_ESCAPE:                 System.exit(0);             }         }     }         private class MListener extends MouseAdapter {         public void mouseMoved( MouseEvent e){                         mx = e.getX();             my = e.getY();             System.out.println("x = " + mx + " y = " + my);         }         public void mouseClicked(MouseEvent e){                         switch(e.getID()){                 case MouseEvent.MOUSE_CLICKED:                                         cx = e.getX();                     cy = e.getY();                                         System.out.println("You've clicked here " + cx + " " + cy);                     break;             }             }         }    }```
The only problem I'm still dealing with, is the fact that my loop doesn't work. I feel like it is in the wrong place or something, however, when I move it into another scope, it presents an error and wont run

Code:

```for(int i=0; i < doors.length; i++){       if( doors[i].mouseInRectangle(mx, my)){                       System.out.println("You are in a door!");       }                   }```
My mouseListener variables are indeed 'mx' and 'my' ... I just wanted to make sure this worked and made it so it would print out "You are in a door!" when the mouse enters the area where the door should be....too bad, it doesn't print out.

I look forward to your advice on this new problem. And also I want to thank you much for the help you have given me thus far!! :)-:
• 05-17-2013, 09:49 AM
Tolls
Re: I'm not sure how to get started here...game
That loop should be in your mouse listener, not in the block used to populate your doors array.
• 05-17-2013, 12:32 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
That loop should be in your mouse listener, not in the block used to populate your doors array.

YOU'RE THE BEST! I tried moving it there before ...but I didn't have it directly in my mouseMoved or mouseClicked methods of my mouseListener. THANK YOU!

Okay so just to make sure, I have put that loop, in both my mouseMoved AND mouseClicked methods...now whenever I am in a door it will print ("you are in a door!) and whenever I click on a door, it prints ("you clicked on a door!")

so that's 1 more part the I can put a *CHECK!* for...next part

-draw the doors, and
-when mouse passes over the door, it highlights the door

by highlight, I think I'm just going to make it really generic and just draw a faint yellow rectangle surrounding the doors when the mouse passes over.

so for this part, would I just make my paintComponent() method, and inside do something like

Code:

```g.drawRect(x1, placement, wide, tall); //or g.fillRect(x1, placement, wide, tall);```
and repeat that for each of the doors? Or would I be able to use my array again for this? Because I don't exactly see a thing that will let you draw an object...like g.drawObject(doors[]); or something like that.

And for the highlighting, I believe I would be inside my loop again, because I want to check to make sure I am IN a door in order to highlight it....and basically, if the loop shows I am in a door, could I just do:

Code:

`g.drawRect(x1, placementY, wide, tall);`
and repeat that for each of the doors? the only problem with that is, wouldn't it make the whole door be yellow once the mouse passes over it?

• 05-17-2013, 12:50 PM
Tolls
Re: I'm not sure how to get started here...game
Loop over the doors array for drawing.
I would be tempted to add a method to Door that does the drawing:
Code:

```public void draw(Graphics2D g) { // draw the door onto g }```
Now, if your mouseInRectangle method also set a flag (a simple boolean) in Door then you could use that in draw() to decide if a particular door needs to be highlighted because the mouse is over the door.
• 05-17-2013, 01:10 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
Loop over the doors array for drawing.
I would be tempted to add a method to Door that does the drawing:
Code:

```public void draw(Graphics2D g) { // draw the door onto g }```
Now, if your mouseInRectangle method also set a flag (a simple boolean) in Door then you could use that in draw() to decide if a particular door needs to be highlighted because the mouse is over the door.

hmm, not QUITE understanding that but lets see...

so yes use the same loop for drawing, okay

add a method to my Door class that does the drawing

Code:

```public void draw(Graphics2D g) {         g.setColor(Color.RED);                    //door 1 is red         g.fillRect(x1, placementY, wide, tall);                 g.setColor(Color.BLUE);                  //door 2 is blue         g.fillRect(x2, placementY, wide, tall);                 g.setColor(new Color(34, 139, 34));      //door 3 is green         g.fillRect(x3, placementY, wide, tall);                 g.setColor(Color.BLACK);                  //door 4 is black         g.fillRect(x4, placementY, wide, tall); }```
Is that what you meant?

And by "Now, if your mouseInRectangle method also set a flag (a simple boolean) in Door then you could use that in draw() to decide if a particular door needs to be highlighted because the mouse is over the door"

the mouseInRectangle method, is a Boolean...I'm not too sure what you mean here.
• 05-17-2013, 01:27 PM
Tolls
Re: I'm not sure how to get started here...game
The method should only draw the Door that it is part of.
Think of the Door class as representing a single door.
That single door is not going to draw all the other doors, is it?
It will only draw itself.

This does mean that the Door class needs to know what colour it is as well as location, so that's another attribute to add.
• 05-17-2013, 02:23 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
The method should only draw the Door that it is part of.
Think of the Door class as representing a single door.
That single door is not going to draw all the other doors, is it?
It will only draw itself.

This does mean that the Door class needs to know what colour it is as well as location, so that's another attribute to add.

Oh, so then I should do basically what I did before...so something like

Code:

```public void draw(Graphics2D g){               g.drawRect(corner_.x_, corner_.y_, corner_.x_ + dim_.x_, corner_.y_ + dim_.y_);     }```
Right? Seeing as how that is the guideline of each door that I am drawing.

now, the door class needs to know what color it is? that confuses me. I know I cant just use setBackground(); because it doesn't work here.....but the fact that I want 4 different colored doors,

is it the fact that this class will only be drawing an outline of a door here? So then if I just want to make the outline of the doors black, I should attribute the doors to have color black? Am I understanding that correctly? So just

Code:

```public void draw(Graphics2D g){         g.setColor(Color.BLACK);         g.drawRect(corner_.x_, corner_.y_, corner_.x_ + dim_.x_, corner_.y_ + dim_.y_);     }```
??

Setting the location of the class? Well I know that setLocationRelativeTo(); doesn't work because it is being seen as a method. I don't get this part either.

Sorry for being so blind with this part
• 05-17-2013, 02:32 PM
Tolls
Re: I'm not sure how to get started here...game
If you want the doors to be a particular colour then the Door class should know what colour that is.
So give it a Color attribute and set it in the constructor (pass it in with the coordinates and size).

Your earlier code (post #35) uses fillRect, and supplies a color to fill, so why not stick with that?
Code:

```public void draw(Graphics2d g) {     g.setColor(color); // color is the new attribute     g.fillRect(x1, placementY, wide, tall); }```
• 05-17-2013, 02:34 PM
Tolls
Re: I'm not sure how to get started here...game
Oh yes, the location bit.
Sorry, probably the way I wrote that post. You already know the location of the door, that's what the x, width, etc attributes are. I was just suggesting adding a new color attribute. So then a Door will know where it is, and what color it is.
• 05-17-2013, 02:53 PM
johnweldon1993
Re: I'm not sure how to get started here...game
Quote:

Originally Posted by Tolls
If you want the doors to be a particular colour then the Door class should know what colour that is.
So give it a Color attribute and set it in the constructor (pass it in with the coordinates and size).

Your earlier code (post #35) uses fillRect, and supplies a color to fill, so why not stick with that?
Code:

```public void draw(Graphics2d g) {     g.setColor(color); // color is the new attribute     g.fillRect(x1, placementY, wide, tall); }```

Right...so then I need to have the 4 different doors here then since each color is to be different?

When you stated
"Think of the Door class as representing a single door.
That single door is not going to draw all the other doors, is it?
It will only draw itself." I got that

but now it seems like "quoting your code from this post" the method will fill the rectangle located at x1, placement etc... and make it a certain color....That, in my mind, talks of only a single door with a single color attributed to it.

I think I'm just going to make it so all of the doors are a lighter brown for now, just to "simplify this" I guess, and if I want to make the doors a different color later, I'll work with it then.

So now

Code:

```public void draw(Graphics2d g) {     g.setColor(new Color(205, 133, 63)); // color is the new attribute     g.fillRect(x1, placementY, wide, tall); }```
See now this code.....sets a color (lighter brown) and then fills 1 rectangle with that color correct? *and also noted that this will not display on the program at the moment)
Show 40 post(s) from this thread on one page
Page 2 of 3 First 123 Last