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 1 of 2 12 LastLast
Results 1 to 25 of 43

Thread: A beginner needs help in oop

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

    Post A beginner needs help in oop

    Hello everyone!

    I've tried to create objects from classes and it worked until I wanted to draw the new objects I created.

    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;
     
     
     
     
    public class ObjectClassTrain {
     
     
     
     
     
     
        public static void main(String[] args) {
     
            square squareRed = new square();
            System.out.println(squareRed.returnXposition());
            System.out.println(squareRed.returnYposition());
     
            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(){
     
     
     
        }
     
     
     
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.returnXposition(), 0, room_width, room_height);
     
        }
     
     
    }
     
     
    class square{
     
        int room_width = 640;
         int room_height = 480;
     
        double Xposition;
        double Yposition;
     
             public square(){
     
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }
     
             public double returnXposition(){
                 return Xposition;
             }
     
             public double returnYposition(){
                 return Yposition;
             }
     
     
    }

    I don't really know how I could get access to the squareRed's Xposition variable.

    oh, and another problem is that there is only one line of code which will paint the objects, and I want to be able, for example,
    to click on the screen to create an object which is automatically be painted on the screen. And to do that I can't just create new lines
    of code for every object that could be created.

    What should I do?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: A beginner needs help in oop

    Small point, but classes should begin with a capital letter like Square.

    You can't access local variables in main() - or other methods - from different places in the code. So make what you want an instance variable. You can initialise it within the createAndShowGui() method and access it elsewhere.

    If your intention is to have a whole bunch of squares the instance variable would most naturally be a list of squares rather than a single square. The painting code can run through the list and paint each one and other code can add/remove individual squares to taste. This is a separate question: get a single square created and accessible first.

  3. #3
    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 pbrockway2 View Post
    You can't access local variables in main() - or other methods - from different places in the code. So make what you want an instance variable. You can initialise it within the createAndShowGui() method and access it elsewhere.
    Like this?
    public static void createAndShowGUI(){
     
     
            Square squareRed = new Square();
     
     
     
            JFrame f = new JFrame("JFrame test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setResizable(false);
            f.add(new myPanel());
            f.pack();
            f.setVisible(true);
     
        }

    That works but how can I get access to Xposition in the paintComponent?
    This gives me an error:
        public void paintComponent(Graphics g){
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.returnXposition(), 0, room_width, room_height);
     
        }

    Could you explain a small point(I only find floating-point in google)

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

    Default Re: A beginner needs help in oop

    That is a lot of classes. Usually you wouldn't want that many classes that didn't do too much, I'm not sure if it is an assignment where you are supposed to have that many classes or not, but it can make it a bit difficult. One way of looking at it is that your myPanel class is dependent upon information from the Square class. Usually you take care of the dependencies while you initialize it, or construct it, as it is a necessary part of the construction process.

  5. #5
    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
    That is a lot of classes. Usually you wouldn't want that many classes that didn't do too much, I'm not sure if it is an assignment where you are supposed to have that many classes or not, but it can make it a bit difficult. One way of looking at it is that your myPanel class is dependent upon information from the Square class. Usually you take care of the dependencies while you initialize it, or construct it, as it is a necessary part of the construction process.
    I've only been using java for a short period of time. I program stuff, like creating a window, the way the tutorials says I should do it. So I dont know how to create, for example the code in the first post, but with less classes. Could you explain please?

    Quote Originally Posted by theoriginalanomaly View Post
    I'm not sure if it is an assignment where you are supposed to have that many classes or not, but it can make it a bit difficult.
    I'm not taking any classes of any sort, I'm just a person who likes to learn and enjoys Java

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

    This gives me an error:
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    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

    The error message:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at objectclasstrain.myPanel.paintComponent(ObjectClas sTrain.java:85)
    at javax.swing.JComponent.paint(JComponent.java:1037)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:870)
    at javax.swing.JComponent.paint(JComponent.java:1046)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:870)
    at javax.swing.JComponent.paint(JComponent.java:1046)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 67)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:870)
    at javax.swing.JComponent.paintToOffscreen(JComponent .java:5139)
    at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(RepaintManager.java:1523)
    at javax.swing.RepaintManager$PaintManager.paint(Repa intManager.java:1454)
    at javax.swing.RepaintManager.paint(RepaintManager.ja va:1257)
    at javax.swing.JComponent.paint(JComponent.java:1023)
    at java.awt.GraphicsCallback$PaintCallback.run(Graphi csCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGra phicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraph icsCallback.java:97)
    at java.awt.Container.paint(Container.java:1778)
    at java.awt.Window.paint(Window.java:3390)
    at javax.swing.RepaintManager$3.run(RepaintManager.ja va:825)
    at javax.swing.RepaintManager$3.run(RepaintManager.ja va:802)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:802)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:745)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Re paintManager.java:725)
    at javax.swing.RepaintManager.access$1000(RepaintMana ger.java:46)
    at javax.swing.RepaintManager$ProcessingRunnable.run( RepaintManager.java:1668)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 642)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

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

    java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    Can you execute the javac compiler and get its error messages. What you posted is missing important details about the error.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    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

    Did you mean this?
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\src\objectclasstrain\ObjectClassTrain.jav a:85: cannot find symbol
    symbol : variable squareRed
    location: class objectclasstrain.myPanel
    g.fillRect(squareRed.returnXposition(), 0, room_width, room_height);
    1 error
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\nbproject\build-impl.xml:949: The following error occurred while executing this line:
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\nbproject\build-impl.xml:268: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 1 second)
    I'm using netbeans. If that is not the right error message then I do not know what I should do.

  10. #10
    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

    Did you mean this?
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\src\objectclasstrain\ObjectClassTrain.jav a:85: cannot find symbol
    symbol : variable squareRed
    Yes, that has a good error message.

    Where is the variable: squareRed defined? The compiler can NOT find a definition for it where it is used.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    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

    squareRed is defined in (Probably not a very good choice now when I think of it.)"createAndShowGUI()":
    public static void createAndShowGUI(){


    Square squareRed = new Square();



    JFrame f = new JFrame("JFrame test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.add(new myPanel());
    f.pack();
    f.setVisible(true);

    }

  12. #12
    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

    That definition is ONLY known within the createAndShowGUI() method.
    It is NOT known anywhere else in the ObjectClassTrain class.
    The code in the paintComponent() method in the myPanel class has no way to get to that variable.

    Several ways to allow code in another class to access the contents of a variable in a class:
    the variable must be defined as a class variable
    or a copy of the contents of that variable could be passed to the class that wants to access it
    or the other class could be an inner class of the class with the variable's definition
    or the class that has the variable could have a getter method that would return the value of the variable
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    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

    So which way is the most efficient way do you think? (I'm sorry if you find me to lazy to google it) I moved "Square squareRed = new Square();" to inside the "class Square" place but after that? should I move the whole paintComponent inside class square
    Quote Originally Posted by Norm View Post
    the other class could be an inner class of the class with the variable's definition
    but that did not work either :/

  14. #14
    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

    that did not work
    Post the full text of the error messages and the code.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    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

    The whole 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;
     
     
     
     
    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(){
     
     
     
        }
     
     
     
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
     
     
     
     
     
     
    }
     
     
    class Square{
     
        int room_width = 640;
         int room_height = 480;
     
        double Xposition;
        double Yposition;
     
             public Square(){
     
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }
     
             public double returnXposition(){
                 return Xposition;
             }
     
             public double returnYposition(){
                 return Yposition;
             }
     
     
             Square squareRed = new Square();
     
     
     
                 @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.returnXposition(), 0, room_width, room_height);
     
        }
     
     
    }



    The error message:
    Compiling 3 source files to C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\build\classes
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\src\objectclasstrain\ObjectClassTrain.jav a:116: cannot find symbol
    symbol : method paintComponent(java.awt.Graphics)
    location: class java.lang.Object
    super.paintComponent(g);
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\src\objectclasstrain\ObjectClassTrain.jav a:118: fillRect(int,int,int,int) in java.awt.Graphics cannot be applied to (double,int,int,int)
    g.fillRect(squareRed.returnXposition(), 0, room_width, room_height);
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\src\objectclasstrain\ObjectClassTrain.jav a:114: method does not override or implement a method from a supertype
    @Override
    3 errors
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\nbproject\build-impl.xml:926: The following error occurred while executing this line:
    C:\Users\BLANK\Documents\NetBeansProjects\ObjectCl assTrain\nbproject\build-impl.xml:268: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 2 seconds)
    Last edited by Norm; May 10th, 2013 at 01:13 PM. Reason: Change quote to code tags

  16. #16
    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

    Please use code tags, NOT quote tags for the code.
    super.paintComponent(g);
    The class Square does NOT have a super class (except Object). It would have a super class if it extended another class.
    That also means there is no paintComponent() method to override in the Square class.

    Why create an instance of the Square class inside the Square class?
    The methods in the Square class can be called directly from other methods in the Square class. There is no need to create another instance of the Square class.
    If you don't understand my answer, don't ignore it, ask a question.

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

    kinkita (May 10th, 2013)

  18. #17
    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

    Please use Code tags, not quote tags, when posting code.


    Square squareRed = new Square(); <--This is inside the square class. This makes it visible only within the square class. from public class Square to the closing brace. If you want to use this variable in the train class, define it in the train class instead of the square class.

    java.awt.Graphics does not have a method that takes a double and 3 integers. You are using Xposition = (int) (Math.random() * room_width); to get an integer, but still storing it inside a variable of type double. So there is an automatic .0 added to the end so to speak. Make the variables hold integers, or change the value from double to integer when drawing it.

    public class Square { ... } does not extend a class (specifically). So super() would refer to java.lang.Object, which does not have a method named paintComponent(int, int, int, int). So when you try to "override" this method in the Square class, it is not there to be overridden.

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

    kinkita (May 10th, 2013)

  20. #18
    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 think I got it to work now!

    The problem was that g.fillRect(squareRed.Xposition, 0, 32, 32); required four ints but I had instead given it a double, an int, an int and another int. (I saw jps's comment right after this achievement)
    So I converted the double Xposition into an integer, which worked perfectly!

    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;
     
    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(){
        }
     
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(room_width, room_height);
        }
     
        Square squareRed = new Square();
     
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.XpositionINT, squareRed.YpositionINT, 32, 32);
     
        }
    }
     
     
    class Square{
     
        int room_width = 640;
        int room_height = 480;
     
        double Xposition = 64;
        double Yposition = 64;
     
             public Square(){
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }
     
             int XpositionINT = (int) Xposition;
             int YpositionINT = (int) Yposition;
     
             public double returnXposition(){
                 return XpositionINT;
             }
     
             public double returnYposition(){
                 return YpositionINT;
             }
    }

    But what I'm wondering now is, how can I change a value inside the object when declaring an it.

    something similar to this?:
    Square squareRed = new Square();
    squareRed.Xposition = 64;

  21. #19
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: A beginner needs help in oop

    getters and setters are what they are usually called. Instead of using a constructor, you have a setMethod() and a getMethod(). At least that is one way. squareRed.setXposition(64), or setXYPosition(x, y)

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

    kinkita (May 10th, 2013)

  23. #20
    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

    Do you mean something like this?:

    The whole 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;
     
     
     
     
    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(){
            double x = 64;
     
     
     
        }
     
     
     
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
     
         Square squareRed = new Square();
         squareRed.Xposition(55);
     
     
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.returnXposition(), squareRed.YpositionINT, 32, 32);
     
        }
     
     
     
     
    }
     
     
    class Square{
     
        int room_width = 640;
         int room_height = 480;
     
        double Xposition = 333;
        double Yposition = 64;
     
             public Square(){
     
                 Xposition = (int) (Math.random() * room_width);
                 Yposition = (int) (Math.random() * room_height);
                 }
     
             int XpositionINT = (int) Xposition;
             int YpositionINT = (int) Yposition;
     
             public int returnXposition(){
                 return XpositionINT;
             }
     
             public int returnYposition(){
                 return YpositionINT;
             }
     
     
    }


    The important part I did:
         Square squareRed = new Square();
         squareRed.Xposition(55);

    but that does not work either

  24. #21
    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

    squareRed.Xposition(55);
    That is the syntax for calling the Xposition() method and passing it the arg: 55
    The class needs to define the method: Xposition(int x)

    Normally methods that set the values of class variables have names that start with: set
    So the method should be named: setXposition(int x)
    If you don't understand my answer, don't ignore it, ask a question.

  25. #22
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: A beginner needs help in oop

    Could you explain a small point(I only find floating-point in google)
    Sorry for the late post. I only meant that class names should start with a big (uppercase) letter: like MyPanel, Square. ("small" == a minor thing that is not the cause of your problem).

  26. #23
    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
    That is the syntax for calling the Xposition() method and passing it the arg: 55
    The class needs to define the method: Xposition(int x)

    Normally methods that set the values of class variables have names that start with: set
    So the method should be named: setXposition(int x)
    So where should I initialize "int x" for it to function?

    And how should the setXposition look like, I did this inside class Square, I want squareRed.setXposition(double x); to give the square its new x-position in a double format:
         public static void setXposition(double x){
     
             double Xposition = x;
     
                 }

  27. #24
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: A beginner needs help in oop

    That's close. But you shouldn't declare Xposition again. It is already part of the Square class, so you just say:

    public static void setXposition(double x){
        Xposition = x;
    }

    Then you can say:

    Square squareRed = new Square();
    squareRed.setXposition(64);

  28. The Following User Says Thank You to pbrockway2 For This Useful Post:

    kinkita (May 10th, 2013)

  29. #25
    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 think I got it to work now

    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;
     
     
     
     
    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(){
     
     
     
        }
     
     
     
     
     
     
        @Override
        public Dimension getPreferredSize(){
     
            return new Dimension(room_width, room_height);
     
        }
     
         Square squareRed = new Square();
     
     
     
        @Override
        public void paintComponent(Graphics g){
            int x = 55;
            int y = 125;
            squareRed.setXposition(x); 
            squareRed.setYposition(y); 
            super.paintComponent(g);        
            g.setColor(Color.BLACK);
            g.fillRect(squareRed.returnXposition(), squareRed.returnYposition(), 32, 32);
     
        }
     
     
     
     
    }
     
     
    class Square{
        private static int Xposition;
        private static int Yposition;
     
         public static void setXposition(int x){
     
              Xposition = x;
                 System.out.println(Xposition);
                 }
     
     
         public static void setYposition(int y){
     
              Yposition = y;
                 System.out.println(Xposition);
                 }
     
     
        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 returnXposition(){
                 return Xposition;
             }
     
             public int returnYposition(){
                 return Yposition;
             }
     
     
     
     
     
    }

    Thanks for the help everyone! I really appreciate it

Page 1 of 2 12 LastLast

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