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

Thread: need help with a new way of source code

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default need help with a new way of source code

    Hello everybody! I recently started with programming in java! I learned from a dutch book written by David J. Barnes en Michael Kölling. The book is translated form the english version: Objects First With Java a parctical introduction using BlueJ. Now that I'm finally through the book i was looking for a task! That's when I stumbled upon a task which was from my brother in 2004! I first asked him but he couldn't answer me (he mostly is programming in LabView, like 9 years)
    First I want to apologize for my bad English typing I from Belgium and I am trying really hard!!

    the task: I need to create a maze in which a player is moving! He can only be moved by the air of a ventilator or by the attraction of a magnet. So by clicking with the mouse on those items, the ventilator or magnet need the become an active element. Whether it affects the player depends on the position of the player and ventilator/magnet! By performing those clicks you have to show the player to the exit of the maze.

    I first started with the development of an algorithm for generating a maze, after 8 hours I finally gave up. Now I've created a maze by defining a 2D-array in which I declared which position is a wall and which is a path, probably not the right way but good enough for the time being.
    Than I made one head Class 'Vak' from that class I extended 'a Path', 'a Wall', 'a Ventilator', 'a Magneet' and finally 'a player'. Those are all the items I guess I need for solving this problem. I created a play area 'Speelbord'. Here I Stored all of my logic (source code)! Here is also where my problem starts. Specifically in this piece of source code:
        public void beweeg([B]Speler s, Vak vk, Ventilator vr, Magneet mt[/B])
        {
     
            if (gameover == false && s.getX() == vk.getX())//we gaan nu VERTICAAL bewegen
            {
                //voor een Ventilator zien de bewegings mogelijkheden er als volgt uit:
                if (s.getY() > vk.getY() && vk.getKleur() == vr.getKleur())
                {
                    int dy = s.getY() + vr.getKracht();//de Ventilator ligt boven de speler -> hij moet dus naar onder bewegen (het aantal vakjes volgens de kracht) = optellen
                    y = y + dy;// is dy == kracht?
                }
                if (s.getY() < vk.getY() && vk.getKleur() == vr.getKleur())
                {
                    int dy = s.getY() - vr.getKracht();//de Ventilator ligt onder de speler -> hij moet dus naar boven bewegen (het aantal vakjes volgens de kracht) = aftrekken
                    y = y - dy;// is dy == kracht?
                }
     
                //voor een Magneet zien de bewegings mogelijkheden er als volgt uit:
                if (s.getY() > vk.getY() && vk.getKleur() == mt.getKleur())
                {
                    int dy = s.getY() - mt.getKracht();// de Magneet ligt boven de speler -> hij moet dus naar boven bewegen (het aantal vakjes volgens de kracht) = aftrekken
                    y = y - dy;// is dy == kracht?
                }
                if (s.getY() < vk.getY() && vk.getKleur() == mt.getKleur())
                {
                    int dy = s.getY() + mt.getKracht();// de Magneet ligt onder de speler -> hij moet dus naar onder bewegen (het aantal vakjes volgens de kracht) = optellen
                    y = y - dy;// is dy == kracht?
                }
            }
     
            if (gameover == false && s.getY() == vk.getY())// we gaan nu HORIZONTAAL bewegen
            {
                //voor een Ventilator zien de bewegings mogelijkheden er als volgt uit: 
                if (s.getX() > vk.getX() && vk.getKleur() ==  vr.getKleur())
                {
                    int dx = s.getX() + vr.getKracht();//de Ventilator ligt links van de speler -> hij moet dus naar rechts bewegen (het aantal vakjes volgens de kracht) = optellen
                    x = x + dx; //is dx == kracht?
                }
                if (s.getX() < vk.getX() && vk.getKleur() ==  vr.getKleur())
                {
                    int dx = s.getX() - vr.getKracht();//de Ventilator ligt rechts van de speler -> hij moet dus naar links bewegen (het aantal vakjes volgens de kracht) = aftrekken
                    x = x - dx; //is dx == kracht?
                }
     
                //voor een Magneet zien de bewegings mogelijkheden er als volgt uit:
                if (s.getX() > vk.getX() && vk.getKleur() ==  mt.getKleur())
                {
                    int dx = s.getX() - mt.getKracht();//de Magneet ligt rechts van de speler -> hij moet dus naar links bewegen (het aantal vakjes volgens de kracht) = aftrekken
                    x = x - dx;//is dx == kracht?
                }
                if (s.getX() < vk.getX() && vk.getKleur() == vr.getKleur())
                {
                    int dx = s.getX() + mt.getKracht();// de Magneet ligt links van de speler -> hij moet dus naar rechts bewegen (het aantal vakjes volgens de kracht) = optellen
                    x = x + dx;//is dx == kracht?
                }
     
            }
     
        }
    the piece in Bold is which I did to collect the positions and see whether the player can or cannot move. This will still compile in BlueJ. Now I've created an other class 'controller' Here I make a mouseListener and I am trying to declare it. There for I wrote the following source code:

        /**
         * MouseEvents vanaf hier. 
         */
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mousePressed(MouseEvent e)  {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseClicked(MouseEvent e, Magneet mt, Ventilator vr, Speler s) //deze heb ik nodig voor het permanent actief maken van Magneet/Ventilator
        {
            x = e.getX();//x-coördinaat van de click
            y = e.getY();//y-coördinaat van de click
            //Wanneer zowel x en y coördinaat hetzelfde zijn mag de beweging worden uitgevoerd zoals beschreven in speelbord
            if (e.getX() == mt.getX() && e.getY() == mt.getY() && s.getX() == e.getX())
            {
                bord.beweegVerticaal();//deze methode staat in speelbord bord
                view.repaint();
            }
            else if (e.getX() == vr.getX() && e.getY() == vr.getY() && s.getY() == e.getY())
            {
                bord.beweegHorizontaal();
                view.repaint();
            }
     
        }
    If I compile now I got the following error: cannot find symbol- method beweegVerticaal. Which is wired because I wrote it (prove above) Now after trying to solve it and reading on the internet I guess I know what the problem might be. My guess is he is expecting some class of those in bold (Speler, Vak, Ventilator, Magneet)
    But I cannot put that class there because when I perform a click there need to be a move... So my guess is I need to change my logic, I need the change the way I am thinking but how, how can I do it?

    I've also uploaded my program so far! So if my explanation is wrong and/or a little bit vague you can go and look!
    MagnetischDoolhof_theGame.rar download - 2shared

    thx in advance!!
    Last edited by Balger; August 23rd, 2012 at 10:34 AM.


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

    Default Re: need help with a new way of source code

    got the following error: cannot find symbol- method beweegVerticaal.
    Please post the full text of the error message.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Where is the definition for the variable: bord and for its class?
    Last edited by Norm; August 22nd, 2012 at 04:53 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with a new way of source code

    I am sorry I did it the wrong way! But I managed to solve some problems I've been having! Now I guess it is a really easy question for java-programmers but I cannot solve it so here it goes:

    public void mouseClicked(MouseEvent e) //deze heb ik nodig voor het permanent actief maken van Magneet/Ventilator
        {
            x = e.getX();//x-coördinaat van de click
            y = e.getY();//y-coördinaat van de click
            //Wanneer zowel x en y coördinaat hetzelfde zijn mag de beweging worden uitgevoerd zoals beschreven in speelbord
             if (e.getX() == mt.getX() && e.getY() == mt.getY() && s.getX() == e.getX())
             {
                 bord.beweeg();//deze methode staat in speelbord bord
                 view.repaint();
             }
             else if (e.getX() == vr.getX() && e.getY() == vr.getY() && s.getY() == e.getY())
             {
                 bord.beweeg();
                 view.repaint();
             }
     
        }

    In the code you see I ask java to give me the coördinats of the grid where I click and where the coördinates of a mt are, so I can compare them. If I compile know I got the following error message: "cannot find symbol variable mt" which is logic because I didn't declare it! But if I do declare, which you can see in this code:

    public void mouseClicked(MouseEvent e, Magneet mt, Ventilator vr, Speler s) //deze heb ik nodig voor het permanent actief maken van Magneet/Ventilator
    	//public void mouseClicked(MouseEvent e) //deze heb ik nodig voor het permanent actief maken van Magneet/Ventilator
        {
            x = e.getX();//x-coördinaat van de click
            y = e.getY();//y-coördinaat van de click
            //Wanneer zowel x en y coördinaat hetzelfde zijn mag de beweging worden uitgevoerd zoals beschreven in speelbord
            if (e.getX() == mt.getX() && e.getY() == mt.getY() && s.getX() == e.getX())
            {
                bord.beweeg();//deze methode staat in speelbord bord
                view.repaint();
            }
            else if (e.getX() == vr.getX() && e.getY() == vr.getY() && s.getY() == e.getY())
            {
                bord.beweeg();
                view.repaint();
            }
     
        }

    it compiles perfectly! However it is nog possible for java to do because the mouseListener is a implemented code from java itself so I cannot add parameters! But I need those parameters to withdraw the coördinates!
    How might I be able to solve this problem?
    thx in advance

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

    Default Re: need help with a new way of source code

    You can not change the arguments passed to a listener method like mouseClicked(). You will have to make the mt variable a class variable so the mouseClicked() method can have access to it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: need help with a new way of source code

    Yes I noticed I cannot add parameters to a method which is already been written by JAVA itself. But my question is how can I make this mt variable a class variable! Because in my program that I'v written I made a class Vak. This is the main class from here I made extensions to Magneet, Ventilator, Speler, ... But to see whether my mouseClicked can go through I performed an if-statement in which I ask to compare the coördinates of the mouseClick and the coördinates magneet mt. I cannot perform that unless I add the parameter 'Magneet mt' to the methode mouseClicked written by JAVA and thus make an error!
    thx in advance!!

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

    Default Re: need help with a new way of source code

    how can I make this mt variable a class variable!
    Define it in the class that the mouseClicked() method is defined in.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with a new way of source code

    thanks a lot for helping me out here!! Now I have a mayor problem, at least I think it is a mayor problem!! I finally succeded with compiling all of my code. Now when I want to start my application nothing happens. Just a white screen pops up, in stead of a maze! The maze which I created by hand in the class 'Speelbord'. I thinks my problem is in my class 'View'
    import java.awt.*;
     
    /**
     * Write a description of class View here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class View extends Canvas
    {
        private Speelbord b;
        private int grootte;
        private Image buffer;
     
        /**
         * constructor for objects of class view
         * 
         * @param bord het speelbord dat de view moet laten zien
         */
        public View(Speelbord bord){
            b = bord; //we plaatsen in bord het model
            grootte = 200;// de grootte waarmee we gedurende dit gaan werken
        }
        //de verplichte methode anders witte canvas
        //zo kunnen we kleuren mengen. we geven een getal op tussen 0 en 255
     
        /**
         * view van het speelbord
         * 
         * @param graphics: geef alle grafische toepassingen weer van het speelbord
         */
        public void paint(Graphics g){
           if (buffer == null                   // als er nog geen buffer bestaat
                ||
               buffer.getWidth(null) != getWidth() //als het venster van breedte veranderd is
                ||
               buffer.getHeight(null) != getHeight() // als het venster van hoogte veranderd is 
              ) 
           {
             buffer = createImage(getWidth(), getHeight()); // maak een nieuwe buffer met de breedte en hoogte van het venster
           }
     
           //tekenOpBuffer(buffer.getGraphics());
           g.drawImage(buffer, 0,0, null);
        }
     
        public void update(Graphics g)
        {
            paint(g);
        }
     
     
        /**
         * tekenen op buffer
         */
        public void tekenOpBuffer(Graphics g)
        {
            //grootte = b.getGrootte(); // grootte van het model dus maw van het speelbord
            int breedte = 140;
            int hoogte = 140;
     
            g.setColor(new Color(120,200,80) ); // het speelbord heeft als kleur (R,G,B)
            g.fillRect(0,0,b.getBreedte()*grootte, b.getHoogte()*grootte);//hiermee fill(vullen we de rechthoegk van het speelbord)
     
            /*java.util.Iterator<Vak> vakken = b.getVakken();//zolang er nog vakken zijn teken de volgende vak op je Graphics g
     
            while (vakken.hasNext()){
                tekenVak(vakken.next(), g);
            }*/
     
        }
     
     
        /**
         * view van de vakken op het speelbord
         * 
         * @param tekenVak: geef een vak op en de graphics
         */
        public void tekenVak(Vak v, Graphics g){
            g.setColor(v.getKleur()); //met set randomize Color en we geven vragen deze kleur
            g.fillRect(v.getX()*grootte, v.getY()*grootte, b.getBreedte()*grootte, b.getHoogte()*grootte); // we vullen deze vakken met oval 2 maal dezelfde dimensie te geven krijgen we een cirkel
        }
     
    }

    maybe I made another mistake somewhere else. If you'd be so kind to look you can download my full program by following this link: MagnetischDoolhof_theGame.rar download - 2shared

    I programmed it in blueJ!
    thx in advance!!

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

    Default Re: need help with a new way of source code

    Can you make a small program that compiles, executes and shows the problem? Not the whole project, just the minimum to show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Balger (August 23rd, 2012)

  10. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with a new way of source code

    I am sorry but I don't know how I possibly could do that! I started on programming but the way I see it I cannot delet any of those classes! I can make a picture of the maze I am trying to display... Maybe I need to translate my program in English? (I am from Belgium so it is all written in dutch)
    I still think the problem is in the class 'view' there I putted in comment an "Iterator<Vak> vakken..." so the program would compile. if on the other hand you take it out of commentary the program display a fault: cannot find symbol - method getVakken() But on the internet I found that an Iterator is used with a simple array,I assume a 1D array! But in the class 'Speelbord' you can see I used a 2D array to perform my maze. Maybe the code is correct written but I am using the wrong types together to get my maze displayed...
    I will keep working on a smaller program but don't know whether I will succeed...
    thx in advance!!

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

    Default Re: need help with a new way of source code

    A small program that shows the problem would be best. The rest of the code just gets in the way for working on the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: need help with a new way of source code

    I might be able to help, I've done tons with graphics in Java. I'll try to translate the class names, but as Norm said, a small program to demonstrate the problem would be nice.

    EDIT: Just because you made this canvas doesn't mean it just works. You've got to plug it into a system, like a graphics library. I couldn't help you with this as this is very library specific, and I'm used to using LWJGL only.
    Last edited by lightOfDay; August 26th, 2012 at 02:52 AM.

  13. #12
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with a new way of source code

    @ Norm: I really was trying to make a smaller program in which I not succeeded but while doing that it occured to me what I needed to change to my own program to let it display the maze. And let the logic work my player towards the end. thank you very much!!

    @ lightofDay: There is no need for it any more! I finally succeeded as I told Norm already! But thanks anyway!

    Maybee you guys Can help me with another problem I've been having. I am trying to upload an image for my player, instead of a rectangle. Now I don't know how I need to do that and whether it will fit in a rectangle or not... You see the player can't be bigger then the red rectangle I choose to be my player right now!!
    Also if you succeed and lead your player towards the end, there pups-up "Congrats" in yellow. I was wondering if it is possible to let it turn 360° ...
    And last but not least I was wondering if it is possible to let a music play during the game?
    Can you help me to solve those questions.

    I also uploaded my new program by the following link: MagnetischDoolhof_theGame_part2.rar download - 2shared
    thx in advance, and sorry for programming in dutch, I will never do that again!!
    Last edited by Balger; August 26th, 2012 at 07:40 AM.

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

    Default Re: need help with a new way of source code

    Again a small program for testing would be helpful.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: need help with a new way of source code

    Glad you fixed it My first theory was you weren't actually displaying the canvas. As for sound, this is also implementation specifc, but many times requires complex setup or an audio codec, which is way out of my league. The problem with a lot of stuff is it's implementation specific. Certain libraries allow easy ways to do things, some don't. I know with LWJGL there was no good way to load images. You had to read straight from the raw file...that was always a pain... Now once you get your player image, with an alpha channel and all that complex stuff if you want, it's very simple to draw. If "g" is your Graphics object, g.drawImage(imageObject, imageWidth, imageHeight, canvas). So the implementation would be g.drawImage(playerImage, playerHeight, playerWidth, this), where this code is put in your canvas "paint" function, making "this" the canvas you want to draw to

  16. #15
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with a new way of source code

    Maybee I've found a way of making a small program in which I can play the music but this time I used eclipse instead of BlueJ!
    - on Eclipse I'm getting the following error: "Description Resource Path Location Type
    Access restriction: The constructor AudioStream(InputStream) is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar Sound_1.java /Sound_1/src line 44 Java Problem" the line is irrelevant here because it shows up at different lines.

    >>Solution???: on youtube I read I needed to go to window->preferences->java->compiler->errors/warnings-> deprecated and restricted API: everything to ignore or warning.
    Do I also need to check any boxes there if that is the solution?

    - therefor I also tried my code in BlueJ and there pop-up a box on my screen with the following:
    show() in java.awt.Window has been deprecated
    sun.audio.AudioPlayer is internal proprietary API and maybe removed in a future release
    sun.audio.AudioPlayer is internal proprietary API and maybe removed in a future release
    sun.audio.AudioStream is internal proprietary API and maybe removed in a future release
    sun.audio.AudioData is internal proprietary API and maybe removed in a future release
    sun.audio.ContinuousAudioDataStream is internal proprietary API and maybe removed in a future release
    sun.audio.AudioStream is internal proprietary API and maybe removed in a future release
    sun.audio.ContinuousAudioDataStream is internal proprietary API and maybe removed in a future release
    >>Solution???: for blueJ I cannot find a solution on the internet do you know any?

    thx in advance!
    Last edited by Balger; August 26th, 2012 at 10:42 AM.

  17. #16
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with a new way of source code

    I am so sorry, you can completely ignore my last post!! I managed to solve it my self because google is my friend too (I was completely forgotten about that)
    But now I want to implement this Sound to the program I already made! But does anybody know whether it is possible or not and where I could best write/add that code? To class Speelbord or should I make another class Sound and just call it in my game Magnetisch doolhof?

    the program with the sound has the following code (which I found on the internet) to let it work you have to add a wav file in the map where your code is stored, this code is working so now error here:
    import javax.swing.*;
    import sun.audio.*;
    import java.awt.event.*;
    import java.io.*;
     
    /**
     * Write a description of class Sound here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Sound
    {
        public static void main(String [] args)
        {
            JFrame frame = new JFrame();
            frame.setSize(200,200);
            JButton button = new JButton("Click me");
            frame.add(button);
            button.addActionListener(new AL());
            frame.show();
        }
     
        public static class AL implements ActionListener
        {
            public final void actionPerformed (ActionEvent e)
            {
                music();
            }
        }
     
        public static void music()
        {
            AudioPlayer MGP = AudioPlayer.player; //BackGroundPlayer
            AudioStream BGM; //BackGroundMusic
            AudioData MD;
            ContinuousAudioDataStream loop = null;
     
     
            try
            {
     
                BGM = new AudioStream (new FileInputStream("50.wav"));
                MD = BGM.getData();
                loop = new ContinuousAudioDataStream(MD);
            }
            catch(IOException e){}
     
            MGP.start(loop);
        }
     
    }


    And I'm working on a small program to let somthing move but not a succes yet!!

    thx in advance!
    Last edited by Balger; August 26th, 2012 at 02:03 PM.

Similar Threads

  1. Java Source Code to UML Diagrams in NetBeans
    By sarkuzi in forum Threads
    Replies: 3
    Last Post: February 7th, 2012, 11:34 AM
  2. Getting Value from Website source code.
    By Blackbird94 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2011, 07:16 AM
  3. How can i add an image into my source code?
    By joelmeler in forum Java Theory & Questions
    Replies: 14
    Last Post: August 1st, 2011, 06:15 PM
  4. source code for CD BURNER
    By zeerussia in forum Java Theory & Questions
    Replies: 0
    Last Post: October 23rd, 2010, 02:23 AM
  5. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM