Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 50
  1. #21
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  2. #22
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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

    Java 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?

  3. #23
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #24
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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

    Java 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{

    }
    ?

  5. #25
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default Re: I'm not sure how to get started here...game

    I'm not sure what you're asking there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #26
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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?

    Java 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?

  7. #27
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #28
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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?:

    Java 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?

  9. #29
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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:
    Java 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #30
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    Oh yes.
    So you would call that method on each door in the array:
    Java 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:

    Java 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

    Java 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

    Java 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?

  11. #31
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default 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

    Java 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

    Java 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!!

  12. #32
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #33
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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

    Java 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:

    Java 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?

    again, greatly appreciate your advice :)

  14. #34
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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:
    Java 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #35
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    Loop over the doors array for drawing.
    I would be tempted to add a method to Door that does the drawing:
    Java 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

    Java 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.

  16. #36
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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 shouldn't care about them.
    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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #37
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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 shouldn't care about them.
    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

    Java 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


    Java 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

  18. #38
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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?
    Java Code:
    public void draw(Graphics2d g) {
        g.setColor(color); // color is the new attribute
        g.fillRect(x1, placementY, wide, tall); 
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #39
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #40
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Default Re: I'm not sure how to get started here...game

    Quote Originally Posted by Tolls View Post
    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?
    Java 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 shouldn't care about them.
    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

    Java 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)

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. How do I Get Started with Java Game Development?
    By gogonimago in forum New To Java
    Replies: 16
    Last Post: 03-01-2012, 01:54 PM
  2. Getting Started
    By SuperPokeunicorn in forum New To Java
    Replies: 3
    Last Post: 09-10-2011, 06:39 AM
  3. Need help getting started on a lab!
    By abatakji74 in forum New To Java
    Replies: 3
    Last Post: 01-30-2011, 01:30 AM
  4. Getting started
    By hornsby in forum NetBeans
    Replies: 1
    Last Post: 03-12-2009, 07:47 AM
  5. How do I get started?
    By programmer-in-training in forum New To Java
    Replies: 4
    Last Post: 01-11-2008, 07:45 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
  •