Page 3 of 3 FirstFirst 123
Results 41 to 50 of 50
  1. #41
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    Sorry, the exact coordinates used in the fillRect should be the ones held by the Door class.

    If it's not drawing then you need to show the code that does the painting in the Board class and the Door class in its entirety.
    Please do not ask for code as refusal often offends.

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

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

    Right, I replaced those..

    Okay

    Door Class

    Java Code:
    /**
     * @author John
     */
    public class Door{
        
        public Vec2 corner_ , dim_;
        
        Door(){
            corner_ = new Vec2(0,0);
            dim_ = new Vec2(0,0);
        }
        
        Door(int x, int y, int w, int h){
            corner_ = new Vec2(x,y);
            dim_ = new Vec2(w,h);
            int lox = corner_.x_;
            int loy = corner_.y_;
            int hix = corner_.x_ + dim_.x_;
            int hiy = corner_.y_ + dim_.y_;
        
        }
        
        public void draw(Graphics2D g){
            g.setColor(new Color(205, 133, 63));
            g.drawRect(corner_.x_, corner_.y_, corner_.x_ + dim_.x_, corner_.y_ + dim_.y_);
        }
            
             public boolean mouseInRectangle( int mrx, int mry){
            return((mrx>= corner_.x_) && (mrx<= corner_.x_+dim_.x_))&&
                  ((mry>= corner_.y_) && (mry<= corner_.y_+dim_.y_));
                
            }
             
           
    }

    Board Class

    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);
    }   
         
        
        
        public Board(){     
            
        addKeyListener( new KListener());    
        addMouseListener( new Board.MListener());
        addMouseMotionListener( (MouseMotionListener)this.getMouseListeners()[0]);
        setBackground(Color.LIGHT_GRAY);
        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);
                
                for(int i=0; i < doors.length; i++){
           if( doors[i].mouseInRectangle(mx, my)){
              
                System.out.println("You are in a door!");
           }
                
           }
             }
    
        
        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;
                }
                 for(int i=0; i < doors.length; i++){
           if( doors[i].mouseInRectangle(cx, cy)){
              
                System.out.println("You clicked on a door!");
           }
                
           }
         
            }
        
        }     
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            
            
            /*
            //g.drawRect(x1, placementY, wide, tall);
            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);
            */
            
            /*
                 only here because I had them here before...but now commented out
           */
    
            for(int i=0; i < doors.length; i++){
           if( doors[i].mouseInRectangle(mx, my)){
              
               
                g.setColor(Color.yellow);
                g.drawRect(x1, placementY, wide, tall);
           }
                
           }
            repaint();
        }
    }

    So as of Right now when RUN

    Window pops up.....when the mouse is moved into an area where a door should be, system prints out "you are in a door" if clicked in that area..prints out "you clicked on a door"

    Also when moved into the area of a door, the place where door 1 would be....gets a yellow outline...no matter which door area the mouse is in...the first door area gets the yellow outline.

    And no doors are shown...we just know where the area of the doors are

  3. #43
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    This is your code in the paintComponent() method which should be drawing the doors.
    Java Code:
    for(int i=0; i < doors.length; i++){
        if( doors[i].mouseInRectangle(mx, my)){
                g.setColor(Color.yellow);
                g.drawRect(x1, placementY, wide, tall);
           }
    }
    You're not doing any drawing in here (except the highlighting).
    This is where you should be calling the draw method, passing in the Graphics object.
    (and don't call repaint() in here, that requests that the Board is repainted, except that paintComponent is called on a repaint() call)
    Please do not ask for code as refusal often offends.

  4. #44
    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
    This is your code in the paintComponent() method which should be drawing the doors.
    Java Code:
    for(int i=0; i < doors.length; i++){
        if( doors[i].mouseInRectangle(mx, my)){
                g.setColor(Color.yellow);
                g.drawRect(x1, placementY, wide, tall);
           }
    }
    You're not doing any drawing in here (except the highlighting).
    This is where you should be calling the draw method, passing in the Graphics object.
    (and don't call repaint() in here, that requests that the Board is repainted, except that paintComponent is called on a repaint() call)
    this is where I should be calling my draw method...?and passing in the Graphics object?

    Java Code:
    public void paintComponent(Graphics g){
            super.paintComponent(g);
             
            for(int i=0; i < doors.length; i++){
                
                    doors[i].draw(g);   // ?
                    
                 if( doors[i].mouseInRectangle(mx, my)){
                     
                      g.setColor(Color.yellow);
                      g.drawRect(x1, placementY, wide, tall);
           }
                
           }
    Well I know for a fact this doesn't work...putting in (g) I mean...when first called, the original *fill-in* is null....when I tried putting doors[], or doors[0] or any other door it doesn't work. It says it requires a Graphics2D, but found a door, or found a graphic. With just null, it obviously gives a null pointer exception.

    And also the reason I called repaint, was because it was the only way I was making the yellow highlight rectangle show up. Without repaint() in there, there is no yellow rectangle showing.

  5. #45
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

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

    Although now that I think about it, when I have my updateGame method... eventually... I'll probably be sticking repaint() in there.

    I still can't figure out what to put to make the doors draw however. I've tried putting everything in *as shown in previous post* but just cant seem to find what I'm supposed to be putting in

  6. #46
    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 johnweldon1993 View Post
    Although now that I think about it, when I have my updateGame method... eventually... I'll probably be sticking repaint() in there.

    I still can't figure out what to put to make the doors draw however. I've tried putting everything in *as shown in previous post* but just cant seem to find what I'm supposed to be putting in
    Nevermind, I think figured out what to put to make it show...kind of

    Here is my new paintComponent() method in Board() class

    Java Code:
    public void paintComponent(Graphics g){
            super.paintComponent(g);
             
            Graphics2D g2d = (Graphics2D)g;
            
            for(int i=0; i < doors.length; i++){
                
                    doors[i].draw(g2d);
                    
                 if( doors[i].mouseInRectangle(mx, my)){
                     
                      
                      g.setColor(Color.yellow);
                      g.drawRect(x1, placementY, wide, tall);
                      
           }
                
           }
    And here's my new Graphics2D draw() method in Door class

    Java Code:
    public void draw(Graphics2D g2d){
           g2d.setPaint(new Color(205,133,63));
           g2d.fillRect(corner_.x_, corner_.y_, corner_.x_ + dim_.x_, corner_.y_ + dim_.y_);
        }

    Well see this finally fills the rectangles (doors), but there's a problem (as can be seen in attachment of screenshot)
    The "should be" doors that are draw, are not the correct doors. There is 1 door on the far left that looks like how they should be, but has a different width than wanted

    The other 3 doors look as though they are overlapped because when filled there is just 1 giant rectangle filled with no gaps in between

    The yellow rectangle is the highlight that has been seen previously, and shows exactly what the door should be, as you can see, the door doesn't replicate it.

    What am I doing wrong here?

    I'm not sure how to get started here...game-project.jpg

  7. #47
    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

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

    anyone have a solution for this?

  8. #48
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    Print out the values used in the draw() method, so you can see what it's drawing.
    I suspect the calculation is slightly off somewhere.
    Please do not ask for code as refusal often offends.

  9. #49
    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
    Print out the values used in the draw() method, so you can see what it's drawing.
    I suspect the calculation is slightly off somewhere.
    Ahh, hello Tolls, Hope you had a good weekend :)

    And thank you for the reply...the values the program is printing out are:

    egr118.Door@5ce40
    egr118.Door@127ff0d
    egr118.Door@d9cbcb
    egr118.Door@d2b64e

    *Not sure if that's what you wanted, I just did a
    Java Code:
    System.out.println(doors[i]);

    Or if I do a

    Java Code:
    System.out.println(g2d);
    I get:
    sun.java2d.SunGraphics2D[font=java.awt.Font[family=Serif,name=Serif,style=plain,size=30],color=java.awt.Color[r=205,g=133,b=63]]

  10. #50
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

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

    No, all the values going into here:
    g2d.fillRect(corner_.x_, corner_.y_, corner_.x_ + dim_.x_, corner_.y_ + dim_.y_);
    in the draw() method.

    Also print the equivalent ones used to draw the yellow outline, so you have a comparison against a set of numbers that works.
    Please do not ask for code as refusal often offends.

Page 3 of 3 FirstFirst 123

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
  •