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.

Page 2 of 2 FirstFirst 12
Results 26 to 43 of 43

Thread: A beginner needs help in oop

  1. #26
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Just another quick question:

    What should I do to be able to create a new square object with properties on a command? for example when button released.

    What I thought I would do (which does not work)

    package objectclasstrain;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
     
     
     
    public class ObjectClassTrain {
     
     
     
     
     
     
        public static void main(String[] args) {
     
     
     
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run(){
                    createAndShowGUI();
                }
            });
     
        }
     
        public static void createAndShowGUI(){
     
     
     
     
     
     
            JFrame f = new JFrame("JFrame test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setResizable(false);
            f.add(new myPanel());
            f.pack();
            f.setVisible(true);
     
        }
     
    }
     
    class myPanel extends JPanel{
     
     
         int room_width = 640;
         int room_height = 480;
     
     
     
     
        public myPanel(){
     
     
            addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
     
                     Square squareRed1 = new Square();
           squareRed1.setXposition(25); squareRed1.setYposition(25);
            squareRed1.setHspeed(1);
            squareRed1.setVspeed(-1);
     
                }
            });
     
     
            addMouseMotionListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
     
                }
            });
     
     
        }
     
     
     
     
        public int checkCollision(int x, int hspeed, int compare){
            if (x > compare)
            {hspeed = -1;}
            else
                if (x < compare)
                {hspeed = 1;}
            return hspeed;
        }
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
     
     
     
     
        @Override
        public void paintComponent(Graphics g){
            int x = (int) (Math.random() * 640);
            int y = (int) (Math.random() * 480);
     
            Square squareRed = new Square();
            if ((squareRed.getXposition() == 0) && (squareRed.getYposition() == 0))
            {squareRed.setXposition(x); squareRed.setYposition(y);
            squareRed.setHspeed(1);
            squareRed.setVspeed(-1);}
     
     
     
     
     
            //squareRed.setHspeed(checkCollision(squareRed.getXposition(), squareRed.getHspeed(), room_width));
     
     
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.getXposition(), squareRed.getYposition(), 32, 32);
            g.setColor(Color.RED);
            g.drawRect(squareRed.getXposition(), squareRed.getYposition(), 32, 32);
     
            repaint();
     
        }
     
     
     
     
    }
     
     
    class Square{
        private static int Xposition;
        private static int Yposition;
        private static int hspeed;
        private static int vspeed;
     
         public static void setXposition(int x){
                 if (Xposition == 0)
                 {Xposition = x;}
                 else
                 {Xposition += x;}
     
                 }
     
     
         public static void setYposition(int y){
     
              Yposition = y;
     
                 }
     
     
              public static void setHspeed(int x){
     
              hspeed = x;
     
                 }
     
     
         public static void setVspeed(int y){
     
              vspeed = y;
     
                 }
     
     
     
        int room_width = 640;
         int room_height = 480;
     
     
            /* public Square(){
     
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }*/
     
             //int XpositionINT = (int) Xposition;
     
             //int YpositionINT = (int) Yposition;
     
             public int getXposition(){
                 return Xposition;
             }
     
             public int getYposition(){
                 return Yposition;
             }
     
            public int getHspeed(){
                return hspeed;
            }
     
     
     
     
     
    }

    But this just change the position on the square on the screen an then move it when to press a mouse button.

    Also, what should I do to paint every new object? I can't just create a new
    g.fillRect(squareRed.getXposition(), squareRed.getYposition(), 32, 32);
    for every new object square that might be created.

  2. #27
    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: A beginner needs help in oop

    what should I do to paint every new object?
    Put the Square objects in a list and have the paint method go though the list (in a loop) and call each Square object's draw method. You should move the drawing code from the paintComponent() method to the Square class into a new draw() method in the Square class and have the paint method call it with the Graphics object so each Square can draw itself at its own location and with its own color.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Quote Originally Posted by Norm View Post
    Put the Square objects in a list and have the paint method go though the list (in a loop) and call each Square object's draw method. You should move the drawing code from the paintComponent() method to the Square class into a new draw() method in the Square class and have the paint method call it with the Graphics object so each Square can draw itself at its own location and with its own color.
    I understand when you say that there should be a loop, but what do you mean by list? how should it look like?

  4. #29
    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: A beginner needs help in oop

    By list I meant something like an ArrayList that would hold instances of the Square class that you want to draw in the paintComponent() method.
    Use it something like this:
    begin loop through items in list
    get next Square object from list
    call Square object's draw method
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

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

    kinkita (May 11th, 2013)

  6. #30
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Would a for loop be of best usage here?

    for (int i; i < 10; i ++){
    i.draw();

    }

    so then there would be 10 objects, each numbered from 1 to 10, draw in a loop calling them separately.

  7. #31
    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: A beginner needs help in oop

    You can NOT use an int to call a method: i.draw();
    You need to get the Square objects from the list and use the objects to call their draw() method.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #32
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    okey, I tried to create the draw method, I put it in the Square class:
    public static void draw(int x, int y){
                 setColor(Color.BLACK);
            fillRect(x, y, 32, 32);
            setColor(Color.RED);
            drawRect(x, y, 32, 32);
             }

    But it says it cant find the symbol: method setColor and fillRect.
    Should I keep the g.etc? but then it says it can't find the symbol: variable g.

  9. #33
    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: A beginner needs help in oop

    Look at the code in the paintComponent() method where those methods were called.
    The methods are in the Graphics class. The paintComponent() method needs to pass the Graphics object it receives to the Square class's draw() method so it can use it.

    Should the draw() method use the x and y position values that are in the Square class?
    If you don't understand my answer, don't ignore it, ask a question.

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

    kinkita (May 11th, 2013)

  11. #34
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Quote Originally Posted by Norm View Post
    Look at the code in the paintComponent() method where those methods were called.
    The methods are in the Graphics class. The paintComponent() method needs to pass the Graphics object it receives to the Square class's draw() method so it can use it.

    Should the draw() method use the x and y position values that are in the Square class?
    You're right, I should do something like this instead:

     public static void draw(Graphics g){
            g.setColor(Color.BLACK);
            g.fillRect(Xposition, Yposition, 32, 32);
            g.setColor(Color.RED);
            g.drawRect(Xposition, Yposition, 32, 32);
             }

    But to "start" the whole thing, if we just focus on one object, how do I make the the draw method start.
    In the paintComponent, none of these worked:
    squareRed.draw(g);
    squareRed.draw(Graphics g);
    squareRed.draw(Graphics);

  12. #35
    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: A beginner needs help in oop

    Don't make the draw() method static. Each Square object needs its own draw() method that will use the values of its variables. None of the Square class's variables or methods should be static.

    The g variable is the reference to the Graphics object that is passed to the paintComponent() method. Use that when calling the draw method: squareRed.draw(g);

    When you get compiler errors, you need to post them and the source that causes them.
    If you don't understand my answer, don't ignore it, ask a question.

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

    kinkita (May 11th, 2013)

  14. #36
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Ahh, I also forgott to put the squareRed.draw(g); after super.paintComponent(g);. But now i did and it works thank you

  15. #37
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    I've got another problem no :/ I've created two objects of the Square class, squareRed and squareRed2, and I'm trying to draw them each with the code
            squareRed2.draw(g);
            squareRed.draw(g);

    But only one of them are drawn, and that is squareRed.

    The full code:
    package objectclasstrain;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
     
     
     
    public class ObjectClassTrain {
     
     
     
     
     
     
        public static void main(String[] args) {
     
     
     
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run(){
                    createAndShowGUI();
                }
            });
     
        }
     
        public static void createAndShowGUI(){
     
     
     
     
     
     
            JFrame f = new JFrame("JFrame test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setResizable(false);
            f.add(new myPanel());
            f.pack();
            f.setVisible(true);
     
        }
     
    }
     
    class myPanel extends JPanel{
     
     
         int room_width = 640;
         int room_height = 480;
     
            int x = (int) (Math.random() * 640);
            int y = (int) (Math.random() * 480);
     
            int xx = (int) (Math.random() * 640);
            int yy = (int) (Math.random() * 480);
     
     
        public myPanel(){
     
     
            addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
     
     
                }
            });
     
     
            addMouseMotionListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
     
                }
            });
     
     
        }
     
     
     
     
        public int checkCollision(int x, int hspeed, int compare){
            if (x > compare)
            {hspeed = -1;}
            else
                if (x < compare)
                {hspeed = 1;}
            return hspeed;
        }
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
     
     
     
     
        @Override
        public void paintComponent(Graphics g){
     
     
     
     
                            Square squareRed2 = new Square();
            squareRed2.setXposition(1); squareRed2.setYposition(1);
            squareRed2.setHspeed(0);
            squareRed2.setVspeed(0);
     
                           Square squareRed = new Square();
            squareRed.setXposition(x); squareRed.setYposition(y);
            squareRed.setHspeed(0);
            squareRed.setVspeed(0);
     
     
            super.paintComponent(g);    
     
            squareRed2.draw(g);
            squareRed.draw(g);
            repaint();
     
     
        }
     
     
     
     
    }
     
     
    class Square{
        private static int Xposition;
        private static int Yposition;
        private static int hspeed;
        private static int vspeed;
     
         public static void setXposition(int x){
     
                 Xposition = x;
     
                 }
     
     
         public static void setYposition(int y){
     
              Yposition = y;
     
                 }
     
     
              public static void setHspeed(int x){
     
              Xposition += x;
     
                 }
     
     
         public static void setVspeed(int y){
     
              Yposition += y;
     
                 }
     
     
     
        int room_width = 640;
         int room_height = 480;
     
     
            /* public Square(){
     
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }*/
     
             //int XpositionINT = (int) Xposition;
     
             //int YpositionINT = (int) Yposition;
     
             public int getXposition(){
                 return Xposition;
             }
     
             public int getYposition(){
                 return Yposition;
             }
     
            public int getHspeed(){
                return hspeed;
            }
     
     
             public void draw(Graphics g){
            g.setColor(Color.BLACK);
            g.fillRect(Xposition, Yposition, 32, 32);
            g.setColor(Color.RED);
            g.drawRect(Xposition, Yposition, 32, 32);
             }
     
     
    }

  16. #38
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A beginner needs help in oop

    Use some printlns to verify the values of the variables and see if you can find any clues.

    A couple things I noticed in the code post #37

    Never create objects or make big calculations in the paint or paintComponent methods. When paintComponent is called, every object should already exist, know how to draw itself on a java.awt.Graphics object, and be ready to do so quickly when called. I would suggest leave it as is until you get it working the way you meant to, but know this will be fixed later on.

    I see some methods with names including the word set, like setVspeed and setHspeed, but the methods do not really do as the name suggests. Since you += the incoming value to the old value, you really don't set the value. If the current value is 20 and I call a method named setValue(-4); I expect the value to be -4 rather than 16. I would use a different name for the methods that do not "set" the value.

  17. #39
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Quote Originally Posted by jps View Post
    I see some methods with names including the word set, like setVspeed and setHspeed, but the methods do not really do as the name suggests. Since you += the incoming value to the old value, you really don't set the value. If the current value is 20 and I call a method named setValue(-4); I expect the value to be -4 rather than 16. I would use a different name for the methods that do not "set" the value.
    I see what you mean. It's just that I'm so used to the fact that you can set a hspeed to an object. Before I decided to try to learn java I used a program called game maker, and in that program you can set a variable hspeed to 1 and then the object with the variable would move in the horisontal direction of 1


    Quote Originally Posted by jps View Post
    Use some printlns to verify the values of the variables and see if you can find any clues.
    Thats weird, the system outputs returned the right x- and y-position, so both suqares should be drawn.

  18. #40
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: A beginner needs help in oop

    Your Methods and Fields are static. Static XPosition, YPosition mean that there is only 1 for the whole class. You are drawing both squares, but the second square you draw is overlaying the first square, because when you set the second square position, you are also setting the XPosition and YPosition for the first square as well. Remove all the static modifiers to get separate instances.

    --- Update ---

    Likely you will receive a warning when accessing a static method and field from and instantiated object. Static fields and methods are usually/supposed to be set by call class name rather then instance name. ie Square.setXPosition(); rather then redSquare.setXPosition(); As it changes the position for all instances of that class. It will compile both ways, but most IDE will give you a warning, underlining it in yellow for example.

  19. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    kinkita (May 12th, 2013)

  20. #41
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Quote Originally Posted by theoriginalanomaly View Post
    Your Methods and Fields are static. Static XPosition, YPosition mean that there is only 1 for the whole class. You are drawing both squares, but the second square you draw is overlaying the first square, because when you set the second square position, you are also setting the XPosition and YPosition for the first square as well. Remove all the static modifiers to get separate instances.


    Tank you, no it works perfectly


    Quote Originally Posted by theoriginalanomaly View Post
    Likely you will receive a warning when accessing a static method and field from and instantiated object. Static fields and methods are usually/supposed to be set by call class name rather then instance name. ie Square.setXPosition(); rather then redSquare.setXPosition(); As it changes the position for all instances of that class. It will compile both ways, but most IDE will give you a warning, underlining it in yellow for example.
    My IDE did not give me an error when I ran the program, but the IDE is netbeans and free, so that might be why.

    --- Update ---

    Quote Originally Posted by jps View Post
    Never create objects or make big calculations in the paint or paintComponent methods. When paintComponent is called, every object should already exist, know how to draw itself on a java.awt.Graphics object, and be ready to do so quickly when called. I would suggest leave it as is until you get it working the way you meant to, but know this will be fixed later on.
    Where do you suggest me to put the code in?
    I put it in the the class myPanel but that gives me an error, telling me it needs an identifyer and that the package squareRed2 does not exist.

    And when I put the code code in the public myPanel the squareRed/squareRed2.draw(); wont work. It says that it can't find the symbol squareRed/squareRed2.
    Do the public myPanel need some sort of returner?

  21. #42
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A beginner needs help in oop

    I don't know of an IDE that warns about accessing a static member of a class from an instance. In fact static members are able to be accessed by all instances, that is what they are for. Don't worry about not seeing a warning.

    Where to put the code depends on the structure of the program.

    Try to keep the construction of objects within the constructor or an init method. In this specific case I would consider the JPanel to be part of the ObjectClassTrain.
    You never actually create a Train object, instead you create a JFrame with this line: JFrame f = new JFrame("JFrame test"); (This works, sometimes people also extend the JFrame class, thats another story)
    Then with this line of code: f.add(new myPanel()); you are calling the myPanel constructor and after that constructor returns you are adding the newly created myPanel to your jframe, f
    This all happens within the code block that would be the constructor of your train object (had you used one). So far so good...
    Then in the myPanel constructor you have two mouse listeners. (Both of which capture mousePressed events??) This is a good place to call the constructor for the red squares. Much like you called the constructor for the panel in the "would-be-constructor" for the train class. The train needed a jpanel, and the jpanel needs the two squares it wants to draw.
    Then in your mousePressed method (and if I remember correctly the other method was mouseDragged), this is where you would call a method on the squares to set the x and y position of the square to the x and y position reported from the mouse in the MouseEvent. Something like: squareRed.setXposition(e.getX()); With a line of code like this inside the mousePressed method, when the mouse is clicked anywhere on the JPanel, the x position of the square will be updated to the x position of the mouse click.
    Finally in the paint method the only thing you would need is to call the individual paint methods for super and each square. The call to repaint within the paintComponent method will cause a repaint every time a repaint occurs. Never call repaint from within a paint or paintComponent method. repaint should be called by your program when a change has been made that will cause a change in the display. Like when you click the mouse or drag the mouse, and the square changes location, after the square updates its x and y positions, then it needs to be redrawn.



    I just want to mention, the tutorial you are following is intended for people who have learned a little about the main method, constructors, getters and setters and are ready to get into the graphics packages provided. To use this tutorial as a tool to understand the structure of a good program, is not a very good idea, as it is not a very well structured program in the tutorial. If you are getting hung up on these concepts read through some of the earlier tutorials (even if they are a bit less fun)

  22. #43
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: A beginner needs help in oop

    Quote Originally Posted by jps View Post

    Try to keep the construction of objects within the constructor or an init method. In this specific case I would consider the JPanel to be part of the ObjectClassTrain.
    You never actually create a Train object, instead you create a JFrame with this line: JFrame f = new JFrame("JFrame test"); (This works, sometimes people also extend the JFrame class, thats another story)
    Then with this line of code: f.add(new myPanel()); you are calling the myPanel constructor and after that constructor returns you are adding the newly created myPanel to your jframe, f
    This all happens within the code block that would be the constructor of your train object (had you used one). So far so good...
    Then in the myPanel constructor you have two mouse listeners. (Both of which capture mousePressed events??) This is a good place to call the constructor for the red squares. Much like you called the constructor for the panel in the "would-be-constructor" for the train class. The train needed a jpanel, and the jpanel needs the two squares it wants to draw.
    Then in your mousePressed method (and if I remember correctly the other method was mouseDragged), this is where you would call a method on the squares to set the x and y position of the square to the x and y position reported from the mouse in the MouseEvent. Something like: squareRed.setXposition(e.getX()); With a line of code like this inside the mousePressed method, when the mouse is clicked anywhere on the JPanel, the x position of the square will be updated to the x position of the mouse click.
    Finally in the paint method the only thing you would need is to call the individual paint methods for super and each square. The call to repaint within the paintComponent method will cause a repaint every time a repaint occurs. Never call repaint from within a paint or paintComponent method. repaint should be called by your program when a change has been made that will cause a change in the display. Like when you click the mouse or drag the mouse, and the square changes location, after the square updates its x and y positions, then it needs to be redrawn.
    I tried to put the code in mtPanel before but then the drawing functions messed up:

    package objectclasstrain;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
     
     
     
    public class ObjectClassTrain {
     
     
     
     
     
     
        public static void main(String[] args) {
     
     
     
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run(){
                    createAndShowGUI();
                }
            });
     
        }
     
        public static void createAndShowGUI(){
     
     
     
     
     
     
            JFrame f = new JFrame("JFrame test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setResizable(false);
            f.add(new myPanel());
            f.pack();
            f.setVisible(true);
     
        }
     
    }
     
     
     
    class myPanel extends JPanel{
     
     
     
     
         int room_width = 640;
         int room_height = 480;
     
            int x = (int) (Math.random() * 640);
            int y = (int) (Math.random() * 480);
     
            int xx = (int) (Math.random() * 640);
            int yy = (int) (Math.random() * 480);
     
     
        public myPanel(){
     
     
              Square squareRed2 = new Square();
             squareRed2.setXposition(1); squareRed2.setYposition(1);
                    System.out.println(squareRed2.getXposition());
                    System.out.println(squareRed2.getYposition());
                           Square squareRed = new Square();
            squareRed.setXposition(128); squareRed.setYposition(128);
            System.out.println(squareRed.getXposition());
            System.out.println(squareRed.getYposition());
     
            addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
     
     
                }
            });
     
     
            addMouseMotionListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
     
                }
            });
     
     
        }
     
     
     
     
        public int checkCollision(int x, int hspeed, int compare){
            if (x > compare)
            {hspeed = -1;}
            else
                if (x < compare)
                {hspeed = 1;}
            return hspeed;
        }
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
     
     
     
     
        @Override
        public void paintComponent(Graphics g) {
               super.paintComponent(g);    
     
            squareRed2.draw(g);
            squareRed.draw(g);
     
     
     
     
        }
     
        private static class squareRed2 {
     
            public squareRed2() {
            }
        }
     
     
     
     
    }
     
     
    class Square extends JPanel{
        private int Xposition;
        private int Yposition;
        private int hspeed;
        private int vspeed;
     
         public void setXposition(int x){
     
                 Xposition = x;
     
                 }
     
     
         public void setYposition(int y){
     
              Yposition = y;
     
                 }
     
     
     
     
     
     
     
     
        int room_width = 640;
         int room_height = 480;
     
     
            /* public Square(){
     
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }*/
     
             //int XpositionINT = (int) Xposition;
     
             //int YpositionINT = (int) Yposition;
     
             public int getXposition(){
                 return Xposition;
             }
     
             public int getYposition(){
                 return Yposition;
             }
     
            public int getHspeed(){
                return hspeed;
            }
     
     
             public void draw(Graphics g){
            g.setColor(Color.BLACK);
            g.fillRect(Xposition, Yposition, 32, 32);
            g.setColor(Color.RED);
            g.drawRect(Xposition, Yposition, 32, 32);
     
                    repaint();
             }
     
     
    }

    And I dont know how I should gain acces to the objects :/


    Quote Originally Posted by jps View Post

    I just want to mention, the tutorial you are following is intended for people who have learned a little about the main method, constructors, getters and setters and are ready to get into the graphics packages provided. To use this tutorial as a tool to understand the structure of a good program, is not a very good idea, as it is not a very well structured program in the tutorial. If you are getting hung up on these concepts read through some of the earlier tutorials (even if they are a bit less fun)
    You're probably right. It's just that it's much more interesting to be able to see the resaults in a window other than terminal

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Not getting the OOP thing!!!
    By AffiliateOwen in forum Java Theory & Questions
    Replies: 7
    Last Post: April 16th, 2013, 09:44 AM
  2. java oop
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 06:50 AM
  3. OOP and java
    By ~Kyo~ in forum Object Oriented Programming
    Replies: 6
    Last Post: January 22nd, 2013, 04:24 AM
  4. OOP help!
    By imaznumkay in forum Object Oriented Programming
    Replies: 3
    Last Post: July 11th, 2011, 01:43 PM
  5. OOP
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 29th, 2009, 10:10 PM