Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: [Beginner Java] Returning a variable from mothod problem

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

    Post [Beginner Java] Returning a variable from mothod problem

    So, I've tried to learn how to use methods so I dont have to use the same script to handle something in vain. But
    what I'm having problems with is returning a variable.

    package javagametest;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Color;
     
     
     
     
     
     
    public class JavaGameTest{
     
        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.add(new myPanel());
            f.pack();
            f.setVisible(true);
     
     
        }
     
    }
     
    class myPanel extends JPanel{
     
        //Global
        static int Room_Width = 966;
        static int Room_Height = 600;
     
        //The red box variables
        private double RedBoxX = (int) (Math.random() * 640);
        private double RedBoxY = (int) (Math.random() * 480);
        private int RedBoxW = 32;
        private int RedBoxH = 32;
        private int RedBoxVert = 1;
        private int RedBoxHoro = 1;
        private double RedBoxSpeed = 0.3;
     
     
        public myPanel(){
     
     
     
     
     
     
        }
     
     
        private void MoveObject(){
     
                switch (RedBoxVert)
                {
                    case 0: RedBoxY-=RedBoxSpeed;
                        break;
                    case 1: RedBoxY+=RedBoxSpeed;
                        break;
                }
     
                switch (RedBoxHoro)
                {
                    case 0: RedBoxX-=RedBoxSpeed;
                        break;
                    case 1: RedBoxX+=RedBoxSpeed;
                        break;
                }
     
     
            }
     
     
        static int checkCollisionHOROSONTAL(double x, double y, int horo, int width){
     
                    if ((x + width >= Room_Width))
                    {horo = 0;}
     
                    if ((x  <= 0))
                    {horo = 1;}
     
                    return horo;
     
                }
     
     
                static int checkCollisionVertical(double x, double y, int vert1, int height){
     
                    if ((x + height >= Room_Height))
                    {vert1 = 0;}
     
                    if ((x  <= 0))
                    vert1 = 1;
     
                    return vert1;
     
                }
     
     
     
     
     
     
     
     
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(Room_Width,Room_Height);
        }
     
     
     
     
     
     
        @Override
        public void paintComponent(Graphics g){
     
            super.paintComponent(g);
            {
             super.paintComponent(g);
                    MoveObject();
                    checkCollisionHOROSONTAL(RedBoxX, RedBoxY, RedBoxHoro, RedBoxW);
                    checkCollisionVertical(RedBoxX, RedBoxY, RedBoxVert, RedBoxH);
     
                    int RedBoxXINT = (int)RedBoxX;
                    int RedBoxYINT = (int)RedBoxY;
     
             g.setColor(Color.RED);
             g.fillRect(RedBoxXINT, RedBoxYINT, RedBoxW, RedBoxH);
             g.setColor(Color.BLACK);
             g.drawRect(RedBoxXINT, RedBoxYINT, RedBoxW, RedBoxH);
     
     
             repaint();
     
            }
     
        }
     
     
    }


    I want the "Return vart1" to indicate if the variable RedBoxVert should be on or off (as you can see in the paramaters " checkCollisionVertical(RedBoxX, RedBoxY, RedBoxVert, RedBoxH);" ), but it's not working.

    I've also tryed to use public int instead of static int, but I have no clue on what I should do.

    I would really appreciate if someone could help!


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: [Beginner Java] Returning a variable from mothod problem

    It should be returning just fine. But to me it looks like you are not storing that value that is returned and using it.

    int myInt = myFunction();

    Regards,

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    kinkita (May 8th, 2013)

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

    Default Re: [Beginner Java] Returning a variable from mothod problem

    Ah, now it's working, thank you. I also had to fix a few missplaced variables

    static int checkCollisionHOROSONTAL(double x, double y, int horo, int width){

    if ((x + width >= Room_Width))
    {horo = 0;}

    if ((x <= 0))
    {horo = 1;}

    return horo;

    }


    static int checkCollisionVertical(double x, double y, int vert1, int height){

    if ((x + height >= Room_Height))
    {vert1 = 0;}

    if ((x <= 0))
    vert1 = 1;

    return vert1;

    }
    but now it works, thanks!

Similar Threads

  1. Variable question from a beginner
    By JimmyJ1776 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 12:55 PM
  2. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  3. returning variable and saving to file
    By ravencrest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 09:04 AM
  4. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  5. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM