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 8 of 8

Thread: problem with including contructor in method

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with including contructor in method

    Hi
    I am making a sokoban game following the tutorial by ChicoTutoProg on youtube.
    He says that he forgot to add the function ChargerLevel() in the constructor. the problem is that I added the method chargerlevel() but but my program is not running as it should be. can someone please guide me to what may be the problem.

    I've attached the file that I am working on.
    Capture3.JPGCapture1.JPG


    ps: in capture1, the game should look like that.
    capture 2 is my code, where I added the method chargerlevel();
    capture 3 is what my game looks like
    package marioSokobanGame;
    02	 
    03	import java.awt.Color;
    04	import java.awt.Font;
    05	import java.awt.Frame;
    06	import java.awt.Graphics;
    07	import java.awt.Graphics2D;
    08	import java.awt.Rectangle;
    09	import java.awt.event.KeyEvent;
    10	import java.awt.event.KeyListener;
    11	import java.io.FileReader;
    12	import java.util.ArrayList;
    13	 
    14	import javax.swing.JPanel;
    15	 
    16	import marioSokoban.MenuFrame;
    17	 
    18	public class GameBoard extends JPanel implements KeyListener{
    19	     
    20	    String Game[][] = new String[12][12];
    21	    int Level = 1;
    22	    private static ArrayList<Mur> Murs;
    23	    private static ArrayList<Objectif> Objectifs;
    24	    private static ArrayList<Caisse> Caisses;
    25	    Mur mur;
    26	    Mario mario;
    27	    Objectif objectif;
    28	    Caisse caisse;
    29	    Font levelFont = new Font("SansSherif", Font.BOLD, 30);
    30	    FileReader fr;
    31	    Frame Gframe;
    32	        
    33	     
    34	    public void ChargerLevel(){
    35	        try{
    36	            fr = new FileReader("Maps/level" + Level + ".level");
    37	            int x = 0, y = 0, i =0;
    38	             
    39	            Murs = new ArrayList<Mur>();
    40	            Caisses = new ArrayList<Caisse>();
    41	            Objectifs = new ArrayList<Objectif>();
    42	             
    43	            while ((i = fr.read()) != -1){
    44	                char strImg = (char) i;
    45	                 
    46	                if (strImg == '0'){
    47	                    Game[x][y] = "MUR";
    48	                    mur = new Mur(x*34,y*34);
    49	                    Murs.add(mur);
    50	                     
    51	                }
    52	                else if (strImg == '1'){
    53	                    Game[x][y] = "MARIO";
    54	                    mario = new Mario(x*34,y*34);
    55	                }
    56	                else if (strImg == '2'){
    57	                    Game[x][y] = "CAISSE";
    58	                    caisse = new Caisse(x*34,y*34);
    59	                    Caisses.add(caisse);
    60	               }
    61	                else if (strImg == '3'){
    62	                    Game[x][y] = "OBJECTIF";
    63	                    objectif = new Objectif(x*34,y*34);
    64	                    Objectifs.add(objectif);
    65	               }
    66	                else if (strImg == ' '){
    67	                    Game[x][y] = null;
    68	                }
    69	                 
    70	                else if (strImg == '\r'|| strImg == '\n'){
    71	                    x--;
    72	                }
    73	                if(x==11){
    74	                    y++;
    75	                    x =0;
    76	                }
    77	                else{
    78	                    x++;
    79	                }
    80	                 
    81	            }
    82	        }
    83	                     
    84	        catch (Exception ex){}
    85	        repaint();
    86	         
    87	    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: problem with including contructor in method

    Well, you don't even have a constructor...

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with including contructor in method

    Hi
    What do you mean? where/what should I type?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with including contructor in method

    May not be directly related to your problem, but one should never allow Exceptions to fall through the cracks. At the very least print out the stack trace.
    try{
    //do something
     
    }catch (Exception ex){
        ex.printStackTrace();
    }

    And to answer your question above, I recommend reading the following:
    http://docs.oracle.com/javase/tutori...structors.html

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with including contructor in method

    hi it gives the same result.

    can you tell me how i can add the constructor.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with including contructor in method

    how i can add the constructor.
    See the link in post#4
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with including contructor in method

    Hi
    i ve the constructor added, right?
    public GameBoard(Frame gf){
            ChargerLevel();
            Gframe = gf;
     
     
            setFocusable(true);
            addKeyListener((KeyListener) this);
     
        }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with including contructor in method

    That looks like a constructor. Does it compile and execute without errors?
    You should not have to cast the KeyListener.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Problem with split() method
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 21st, 2012, 06:15 PM
  2. Looping method problem.
    By Swen in forum What's Wrong With My Code?
    Replies: 24
    Last Post: December 29th, 2011, 09:49 AM
  3. Method access problem
    By hello_world in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2011, 03:39 PM
  4. Replies: 4
    Last Post: July 20th, 2010, 07:15 AM
  5. Problem with a JFrame method
    By Lakmini Kuruppu in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 08:40 AM

Tags for this Thread