Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: java oop

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default java oop

    it have 3 classes.
    main class = run every thing
    player class = create player and create his moves
    level class = render map and player

    when i run this code below it render a map and two players.

    1st player in redering in main when can move. if user hit right or left key it moves fine. (its moves bc of a method player_class.player_move(). i am calling this method in main)

    but problem is in 2nd player which is redering in level class. it does render but it doesnt move bc player_move() method is not appliet on 2nd player.

    i want some way so player_move() method is applied on 2nd player. which will make him move too.

    /*** main.java ***/
    public class main exted JApplet implement ActionListener
    {
            player player_class = new player();
        level level_class = new level();
            ....
     
         //useing timer and this is a main game loop
          public void actionPerformed(ActionEvent e)
            {
              player_class.player_move() ; 
              repaint();
            }
     
            public void paint()
            {
                super.paint(g);
                player_class.paint(g);
                level_class.paint(g);
            }
    }

    /*** player.java/
    public class player
    {
        ....
     
       public void player_move()
       {
          //this method works fine
          //it just moves the box(which is my player) left if user hit left key. right if user hit right key
       }
     
        //my player which is just a box
         public void paint(Graphics g)
        {
            g.drawIRect(x, y, width, height); 
           }
    }

    /*** level.java ***/
    public class level 
    {
        private int level01[][] =            {  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                    {0,1,0,0,0,0,0,0,0,0,0,2,0,0,0},
                                    {2,2,2,2,2,2,2,0,0,2,2,2,2,2,2} };
     
        int image_tile_size = 32;  //32 by 32 image
     
     
     
        public level()
        {
        }
     
     
        @Override
        public void paint(Graphics g)
        {
     
     
    //render map. 
    //draw green box where its '2'
    //draw player where its '1'
    //draw blue box for sky where its '0'
     
            for(int y = 0; y < level01.length; y++) //rows
            {
                for(int x = 0; x < level01[y].length; x++) //cols
                {
                    if(level01[y][x] == 0)
                    {
                        g.setColor(Color.blue);
                        g.fillRect(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size); //bc image_title_size is the image size
                    }
                    if(level01[y][x] == 1)
                    {
                        //draw player where there is '1'
                        player player_class = new player(x*image_tile_size, y*image_tile_size);
                        player_class.paint(g);
                    }
                    if(level01[y][x] == 2)
                    {
                        g.setColor(Color.green);
                        g.fillRect(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size);
                    }
                }
            }//end of main for loop
        }//end of paint method
    }//end of level class



    the problem is that player doesnt move. its bc player_move() method a applied in main method and not in level class. i want to appliy player_move method to player in level class.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java oop

    How can the code be compiled and executed for testing to see the problem?

    To do the same thing to multiple objects, have a method that does the thing and pass it each object.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. OOP and java
    By ~Kyo~ in forum Object Oriented Programming
    Replies: 6
    Last Post: January 22nd, 2013, 04:24 AM
  2. Is Java 100% OOP
    By kbbaloch in forum Object Oriented Programming
    Replies: 5
    Last Post: December 29th, 2011, 12:54 AM
  3. Free Application or Software for Java Programming focused on OOP
    By seicair in forum Java Theory & Questions
    Replies: 3
    Last Post: June 14th, 2011, 01:00 AM
  4. [SOLVED] OOP java assignment
    By enkei in forum Object Oriented Programming
    Replies: 1
    Last Post: April 27th, 2011, 11:16 AM
  5. Need Ideas For OOP(Java) Mini Project
    By rizwansheikh in forum Object Oriented Programming
    Replies: 6
    Last Post: December 13th, 2010, 01:47 PM