Jump to content

Java: Get() and Set() return default values

We are programming a game called Pinball.

 

Our problem is that the Values from the class Brick() are not given over to the class Game() correctly (they contain the default values).

 

 

Here are the important parts of the code:

public class Game implements KeyListener, MouseMotionListener{    //Declaration of the variables    //<editor-fold defaultstate="collapsed" desc=" Variables ">    //<editor-fold defaultstate="collapsed" desc=" Scale Variables ">[...]        //<editor-fold defaultstate="collapsed" desc=" Movement Variables">[...]        //<editor-fold defaultstate="collapsed" desc=" Paddle Variables">[...]        //<editor-fold defaultstate="collapsed" desc=" Brick Variables">    /** @[member=paramvir1320]    brick_positionX   Postition of Brick on the X-Coordinate */    private int brick_positionX = -1;        /** @[member=paramvir1320]    brick_positionY   Postition of Brick on the Y-Coordinate */    private int brick_positionY = -1;        /** @[member=paramvir1320]    brick_lenght   Lenght of the Brick */    private int brick_lenght = -1;        /** @[member=paramvir1320]    brick_height   Height of the Brick */    private int brick_height = -1;        /** @[member=paramvir1320]    brick_isVisible   Returns visibility */    private boolean brick_isVisible = true;    //</editor-fold>
    public void drawFrame(int frame_drawSizeX, int frame_drawSizeY)     {        System.out.println("Entering drawFrame();");                //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JFrame ">[...]        //<editor-fold defaultstate="collapsed" desc=" JFrame Dynamic Scaling ">[...]                //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JMenu ">[...]                //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of Brick List/Level ">        Level level = new Level();        //</editor-fold>
int delay = 40; //Milliseconds for delay                ActionListener taskPerformer = new ActionListener()         {            //Run through this with delay            @[member=override2299]            public void actionPerformed(ActionEvent evt)            {                             //Move the ball              ball_posX += ball_deltaX;              ball_posY += ball_deltaY;                            Brick brick = new Brick();              brick_positionX = brick.getBrick_positionX();              brick_positionY = brick.getBrick_positionY();              brick_lenght = brick.getBrick_lenght();              brick_height = brick.getBrick_height();              brick_isVisible = brick.getIsVisible();                            //<editor-fold defaultstate="collapsed" desc=" *Brick Variables SOUT ">[...]                            game.repaint();                            //<editor-fold defaultstate="collapsed" desc=" Ball: Frame Collision ">[...]                                      }
    // Drawing objects    class paintObjects extends JPanel     {                @[member=override2299]        protected void paintComponent(Graphics g)         {                        super.paintComponent(g);                        g.setColor(Color.BLACK);                        //g draws ball            g.fillOval( (int) ball_posX, (int) ball_posY, ball_radius*2, ball_radius*2);                        //g draws paddle            g.fillRect( (int) paddle_posX, (int) paddle_posY, paddle_width, paddle_height);                        //g draws bricks            //<editor-fold defaultstate="collapsed" desc=" *Brick Variables SOUT ">[...]                          if(brick_isVisible == true)            {                //<editor-fold defaultstate="collapsed" desc=" *Brick Variables SOUT ">[...]                g.drawRect(brick_positionX, brick_positionY, brick_lenght, brick_height);            }                    }    }

In the class Level():

// For more information about this project visit the documentation folder!package Game;import java.util.LinkedList;public class Level {    private int level_counter = 0;    public void Level()    {        System.out.println("Entering the Level();");                LinkedList level_bricklist = new LinkedList();                Brick brick = new Brick();                switch(level_counter)        {            case 0:{                         System.out.println("Adding Bricks to Level 0...");                        level_bricklist.add(brick.addBrick(400, 30, 50, 50, true));                         level_bricklist.add(brick.addBrick(500, 30, 50, 50, true));                         level_bricklist.add(brick.addBrick(600, 30, 50, 50, true));                         break;                   }                    }    }    public void setLevel_counter(int level_counter) {        this.level_counter = level_counter;    }    }

And in the class Brick():

// For more information about this project visit the documentation folder!package Game;public class Brick{       private int brick_positionX, brick_positionY, brick_lenght, brick_height;    private boolean isVisible;        public boolean addBrick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        System.out.println("Brick.addBrick();");        this.brick_positionX = brick_positionX;        this.brick_positionY = brick_positionY;        this.brick_lenght = brick_lenght;        this.brick_height = brick_height;        this.isVisible = isVisible;        return true;    }    public int getBrick_positionX()     {        return brick_positionX;    }    public int getBrick_positionY()     {        return brick_positionY;    }    public int getBrick_lenght()     {        return brick_lenght;    }    public int getBrick_height()     {        return brick_height;    }        public boolean getIsVisible()     {        return isVisible;    }    }

Here is the whole code in one file: http://pastebin.com/DQLGNNcj

 

Our question is why it cant give us the needed values and how we can fix the code, thanks in advance :)

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

i just gave it a quick read, so i might have missed something, but is this right?

              Brick brick = new Brick();              brick_positionX = brick.getBrick_positionX();              brick_positionY = brick.getBrick_positionY();              brick_lenght = brick.getBrick_lenght();              brick_height = brick.getBrick_height();              brick_isVisible = brick.getIsVisible();

you're getting the values from a not defined brick

Link to comment
Share on other sites

Link to post
Share on other sites

You are indeed calling the addBrick() method in the level class and adding these to a LinkedList. But these brick-objects are not the same as the one's you are using from line 169 (from the pasebin document) and onwards. Because those line's use the brick object created in line 168. And that one hasn't any values assigned to it (the addBrick method isn't called on it). 

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

i just gave it a quick read, so i might have missed something, but is this right?

              Brick brick = new Brick();              brick_positionX = brick.getBrick_positionX();              brick_positionY = brick.getBrick_positionY();              brick_lenght = brick.getBrick_lenght();              brick_height = brick.getBrick_height();              brick_isVisible = brick.getIsVisible();

you're getting the values from a not defined brick

You are indeed calling the addBrick() method in the level class and adding these to a LinkedList. But these brick-objects are not the same as the one's you are using from line 169 (from the pasebin document) and onwards. Because those line's use the brick object created in line 168. And that one hasn't any values assigned to it (the addBrick method isn't called on it). 

 

Then how can we recode it to import the given list?

We tried it and we coundn't access the list from game() :(

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

Then how can we recode it to import the given list?

We tried it and we coundn't access the list from game() :(

 

The level_bricklist isn't in class Game, it's in Level.

You could move it to Game and create a method in the Game class that add's bricks to that list. Then you can call that method from Level and give it the just created brick.

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

The level_bricklist isn't in class Game, it's in Level.

You could move it to Game and create a method in the Game class that add's bricks to that list. Then you can call that method from Level and give it the just created brick.

 

We tried to move the content from level() to game() but it still didn't work properly :(

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

We tried to move the content from level() to game() but it still didn't work properly :(

You also should remove line 168. You need to get the already existing bricks that are in the level_bricklist and perform the methods at line 169 and onwards on them.

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

We tried to move the content from level() to game() but it still didn't work properly :(

 

We have managed to display one of them, we'll report back to you after we finished displaying multiple ones!

Thanks for the help! :lol:

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

We have managed to display one of them, we'll report back to you after we finished displaying multiple ones!

Thanks for the help! :lol:

 

The new code: http://pastebin.com/DQLGNNcj

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Glad I could help   :P .

If you still encounter problems feel free to contact me.

And if this solves your problem could you please mark this as solved? 

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

Glad I could help   :P .

If you still encounter problems feel free to contact me.

And if this solves your problem could you please mark this as solved? 

 

We definetly will mark it solved ;)

However, we still encounter one more problem:

 

We can't call the single variables out of one brick with .get(i).

// Drawing objects    class paintObjects extends JPanel     {                @[member=override2299]        protected void paintComponent(Graphics g)         {                        super.paintComponent(g);                        g.setColor(Color.BLACK);                        //g draws ball            g.fillOval( (int) ball_posX, (int) ball_posY, ball_radius*2, ball_radius*2);                        //g draws paddle            g.fillRect( (int) paddle_posX, (int) paddle_posY, paddle_width, paddle_height);                        //g draws bricks            //<editor-fold defaultstate="collapsed" desc=" *Brick Variables SOUT ">              System.out.format("brick_positionX = %d \nbrick_positionY = %d \nbrick_lenght = %d \nbrick_height = %d \nbrick_isVisible = %b\n\n", brick_positionX, brick_positionY, brick_lenght, brick_height, brick_isVisible);              //</editor-fold>                          System.out.println("SIZE: " + (int) level_bricklist.size());                          if(brick_isVisible == true)            {                for(int i=0; i<level_bricklist.size(); i++)                {                    g.drawRect( (int) level_bricklist.get(i), (int) level_bricklist.get(i), brick_lenght, brick_height);                }                            }                    }    }

We get an exception on the console (by the .get()?):

run:Entering drawFrame();Before Scaling:ball_posX = -1,000000ball_posY = -1,000000paddle_posX = -1,000000paddle_posY = -1,000000X: 1920,000000  ...  Y: 1080,000000After Scaling:ball_posX = 1010,000000ball_posY = 702,000000paddle_posX = 960,000000paddle_posY = 864,000000X: 1920,000000  ...  Y: 1080,000000brick_positionX = -1 brick_positionY = -1 brick_lenght = -1 brick_height = -1 brick_isVisible = trueSIZE: 0Entering the Level();Adding Bricks to Level 0...addBrick();addBrick();addBrick();brick_positionX = 600 brick_positionY = 30 brick_lenght = 50 brick_height = 50 brick_isVisible = truebrick_positionX = 600 brick_positionY = 30 brick_lenght = 50 brick_height = 50 brick_isVisible = trueSIZE: 3Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer	at Game.Game$paintObjects.paintComponent(Game.java:332)	at javax.swing.JComponent.paint(JComponent.java:1054)	at javax.swing.JComponent.paintChildren(JComponent.java:887)	at javax.swing.JComponent.paint(JComponent.java:1063)	at javax.swing.JComponent.paintChildren(JComponent.java:887)	at javax.swing.JComponent.paint(JComponent.java:1063)	at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)	at javax.swing.JComponent.paintChildren(JComponent.java:887)	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5226)	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)	at javax.swing.RepaintManager.paint(RepaintManager.java:1249)	at javax.swing.JComponent.paint(JComponent.java:1040)	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)	at java.awt.Container.paint(Container.java:1967)	at java.awt.Window.paint(Window.java:3877)	at javax.swing.RepaintManager$3.run(RepaintManager.java:819)	at javax.swing.RepaintManager$3.run(RepaintManager.java:796)	at java.security.AccessController.doPrivileged(Native Method)	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)	at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)	at java.awt.EventQueue.access$200(EventQueue.java:103)	at java.awt.EventQueue$3.run(EventQueue.java:694)	at java.awt.EventQueue$3.run(EventQueue.java:692)	at java.security.AccessController.doPrivileged(Native Method)	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)	at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

The newest version: http://pastebin.com/DQLGNNcj

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

We definetly will mark it solved ;)

However, we still encounter one more problem:

 

We can't call the single variables out of one brick with .get(i).

The newest version: http://pastebin.com/DQLGNNcj

 

 

Our intention is to draw every brick stored in the list...

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

Our intention is to draw every brick stored in the list...

because you're adding to the list the result of the addBrick() call

but that is a boolean, not a pointer to the Brick object

Link to comment
Share on other sites

Link to post
Share on other sites

because you're adding to the list the result of the addBrick() call

but that is a boolean, not a pointer to the Brick object

 

What pointer should we use then?

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

What pointer should we use then?

The addBrick method should be the constructor of the Brick class. That way you create a new brick and immidiately assign it's variables. Because now you are assigning the variables of the game class in the addBrick method.

You would get something like this: 

 

level_bricklist.add(new Brick(400, 30, 50, 50, true));

 

It would also be a good idea to set the kind of object's the level_bricklist can contain:

 

LinkedList<Brick> level_bricklist = new LinkedList<>();

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

The addBrick method should be the constructor of the Brick class. That way you create a new brick and immidiately assign it's variables. Because now you are assigning the variables of the game class in the addBrick method.

You would get something like this: 

 

level_bricklist.add(new Brick(400, 30, 50, 50, true));

 

It would also be a good idea to set the kind of object's the level_bricklist can contain:

 

LinkedList<Brick> level_bricklist = new LinkedList<>();

 

 

We'll do that. However, we won't finish it before friday/saturday this week.

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

What pointer should we use then?

The addBrick method should be the constructor of the Brick class. That way you create a new brick and immidiately assign it's variables. Because now you are assigning the variables of the game class in the addBrick method.

yeah, keep in mind that you're using Java, which is Object Oriented, so you should code your classes so that they can handle themselves with as much independency as they can

so, the class Brick should be the one dealing with bricks, and so on

even declaring the ActionListener like that is kinda ghetto, if you try to take it out and declare it as a seperate class you will have a really, really big gain in code clearity and debug ease

 

just read through your code and see what can be handled in a dedicated class (an already existing one, or a new one)

edit: it's just a suggestion, but keep in mind that when a project gets bigger and bigger, coding it "the good way" respecting the OOP principles will really save you from a lot of bad headaches

 

and just one little thing, i think that

LinkedList<Brick> level_bricklist = new LinkedList<>();

should be

LinkedList<Brick> level_bricklist = new LinkedList<Brick>();
Link to comment
Share on other sites

Link to post
Share on other sites

should be

LinkedList<Brick> level_bricklist = new LinkedList<Brick>();

 

You could do that, but it isn't neccesary in Java SE7. Maybe only if you want to be on the extra extra save side  :P

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

You could do that, but it isn't neccesary in Java SE7. Maybe only if you want to be on the extra extra save side  :P

oh didn't know that

but without specifying the Brick type in the constructor, which type will the .get() method return? Object or Brick?

Link to comment
Share on other sites

Link to post
Share on other sites

oh didn't know that

but without specifying the Brick type in the constructor, which type will the .get() method return? Object or Brick?

It will return Brick. (But a brick is actually just an object so it would return a object of type Brick)

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

It will return Brick. (But a brick is actually just an object so it would return a object of type Brick)

i didn't explain well enough what i meant:

i didn't mean "object", i meant "Object", the big class that contains all objects. for instance, a LinkedList is a child of Object, Brick is a child of Objects, and inherits all of its public methods and properties

 

so, the advantage (one of them) of declaring the type that the LinkedList will work with is that the .get() method will return a Brick object instead of an Object object, which translates into "you don't have to cast the returned value of .get()"

 

is this what you meant too?

Link to comment
Share on other sites

Link to post
Share on other sites

i didn't explain well enough what i meant:

i didn't mean "object", i meant "Object", the big class that contains all objects. for instance, a LinkedList is a child of Object, Brick is a child of Objects, and inherits all of its public methods and properties

so, the advantage (one of them) of declaring the type that the LinkedList will work with is that the .get() method will return a Brick object instead of an Object object, which translates into "you don't have to cast the returned value of .get()"

is this what you meant too?

Yes. And this way you can only add Brick objects to the list and not just every object that extends the big Object class.

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

Yes. And this way you can only add Brick objects to the list and not just every object that extends the big Object class.

oh

that looked really odd to me, but it looks like the compiler is just guessing the type to pass to the constructor

i'll leave the link to the official documentation here just in case someone is curious and doesn't want to lose time googling and googling and googling

Link to comment
Share on other sites

Link to post
Share on other sites

So, we are back!

Our plan is to complete the basic program until tomorrow evening.

 

What we'll do until then:

  • Redo the code for OOP
  • Try getting every single brick to output

 

If you have any more suggestions on how we can manage to code this the best way possible, feel free to leave a comment :rolleyes:

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

So, we are back!

Our plan is to complete the basic program until tomorrow evening.

 

What we'll do until then:

  • Redo the code for OOP
  • Try getting every single brick to output

 

If you have any more suggestions on how we can manage to code this the best way possible, feel free to leave a comment :rolleyes:

prepare your coffee

 

the only thing i can suggest is, seeing that you're not used to code the OOP way, to prepare a list of all the classes you will need and the methods that you will need them to have, before actually diving into code

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×