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

Thread: boolean values are going against my assumption on execution of events

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default boolean values are going against my assumption on execution of events

    i beg your pardon with the title of my post, but im not sure what title to give on this one, ill post the code first
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.Timer;
     
    public class NewClass2 extends JPanel implements ActionListener {
     
        private Timer timer;
        private boolean shouldFadeMiddleSquare;
        private int middleSquareAlpha;
     
        public NewClass2() {
     
            setBackground(Color.BLACK);
            setLayout(null);
            addMouseMotionListener(new MouseMotionListener());
            middleSquareAlpha = 255;
            timer = new Timer(11, this);
            timer.start();
        }
     
        public void paint(Graphics g) {
     
            Graphics2D g2d = (Graphics2D) g;
            super.paint(g2d);
     
            g2d.setColor(Color.RED);
            g2d.fillRect(30, 30, 30, 30);
     
            g2d.setColor(Color.BLUE);
            g2d.fillRect(130, 30, 30, 30);
     
            g2d.setColor(new Color(255, 255, 0, middleSquareAlpha));
            g2d.fillRect(85, 50, 20, 20);
            repaint();
        }
     
        public void actionPerformed(ActionEvent e) {
     
            if (shouldFadeMiddleSquare) {
     
                if (middleSquareAlpha > 0) {
     
                    middleSquareAlpha -=5;
                }
            }
            else {
     
                middleSquareAlpha = 255;
            }
        }
     
        private class MouseMotionListener extends MouseMotionAdapter {
     
            public void mouseMoved(MouseEvent e) {
     
                // red square
                if (e.getX() >= 30 && e.getX() <= 60 &&
                    e.getY() >= 30 && e.getY() <= 60) {
     
                    shouldFadeMiddleSquare = true;
                }
                else {
     
                    shouldFadeMiddleSquare = false;
                }
     
                // blue square
                if (e.getX() >= 130 && e.getX() <= 160 && 
                    e.getY() >= 30 && e.getY() <= 60) {
     
                    shouldFadeMiddleSquare = true;
                }
                else {
     
                    shouldFadeMiddleSquare = false;
                }
            }
        }
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame();
            f.add(new NewClass2());
            f.setSize(200, 200);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }

    when the mouse hovers on either sqaure, the middle square SHOULD FADE, given the logics of fading using the color constructor with ALPHA parameters and some boolean values, i dont get it, i was reading entirely the original code of this program, but my assumption dissapointed me, as you can see, when the mouse hovers the blue sqaure (given that there are boolean values keeping track of the event) the middle square will disappear, but why doesnt the middle sqaure dissapears when i hover the red sqaure?

    or should i say, whats going on with the boolean values?, i dont know if i really dont understand it, or i dont see where the problem is or maybe its just, im thiking too much of anything else, i need your help please.. im confused ?_?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: boolean values are going against my assumption on execution of events

    Add some println's in there to look at the boolean value after the evaluation of each square in the mouseMoved method, then move your mouse over the boxes to see what the value is after each evaluation. There is a logic problem here (think about what would happen when you hover over the each square - step through the mouseMoved method in each case) that the above technique should demonstrate

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean values are going against my assumption on execution of events

    Add some println's in there to look at the boolean value after the evaluation of each square in the mouseMoved method, then move your mouse over the box
    i already did some print statements, i still dont get why does the boolean value doesnt change when i hover the red sqaure, when the mouseMove event has the same statements both for red and blue sqaure to change the boolean value, well the thing is, base on my analyzation my expected event should happenned whether i hover the blue or red sqaure based on the statements i wrote on the mouseMoved event, ill post the print statements, that still making me wonder why does this problem occurs, (modified code with print statements)

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.Timer;
     
    public class SwingEventError1 extends JPanel implements ActionListener {
     
        private Timer timer;
        private boolean shouldFadeMiddleSquare;
        private int middleSquareAlpha;
     
        public SwingEventError1() {
     
            setBackground(Color.BLACK);
            setLayout(null);
            addMouseMotionListener(new MouseMotionListener());
            middleSquareAlpha = 255;
            timer = new Timer(11, this);
            timer.start();
        }
     
        public void paint(Graphics g) {
     
            Graphics2D g2d = (Graphics2D) g;
            super.paint(g2d);
     
            g2d.setColor(Color.RED);
            g2d.fillRect(30, 30, 30, 30);
     
            g2d.setColor(Color.BLUE);
            g2d.fillRect(130, 30, 30, 30);
     
            g2d.setColor(new Color(255, 255, 0, middleSquareAlpha));
            g2d.fillRect(85, 50, 20, 20);
            repaint();
        }
     
        public void actionPerformed(ActionEvent e) {
     
            if (shouldFadeMiddleSquare) {
     
                System.out.println("This should be printed when this boolean is" + shouldFadeMiddleSquare);
                if (middleSquareAlpha > 0) {
     
                    middleSquareAlpha -=5;
                }
            }
            else {
     
                middleSquareAlpha = 255;
            }
        }
     
        private class MouseMotionListener extends MouseMotionAdapter {
     
            public void mouseMoved(MouseEvent e) {
     
                // red square
                if (e.getX() >= 30 && e.getX() <= 60 &&
                    e.getY() >= 30 && e.getY() <= 60) {
     
                    shouldFadeMiddleSquare = true;
                    System.out.println("RED " + shouldFadeMiddleSquare);
                }
                else {
     
                    shouldFadeMiddleSquare = false;
                }
     
                // blue square
                if (e.getX() >= 130 && e.getX() <= 160 && 
                    e.getY() >= 30 && e.getY() <= 60) {
     
                    shouldFadeMiddleSquare = true;
                }
                else {
     
                    shouldFadeMiddleSquare = false;
                }
            }
        }
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame();
            f.add(new SwingEventError1());
            f.setSize(200, 130);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: boolean values are going against my assumption on execution of events

    Think about how your method works - better yet step through the code under the conditions you wish to test. Consider the scenario where your mouse is over the red square - the code first evaluates the red square, which sets your boolean to true. Next, it continues to test whether the point is in the blue square, which is false. End result is false... the way it is currently written, the only way your flag will be set to true after the end of the method call is if the last conditional test is true (eg you are over the blue square)

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

    chronoz13 (June 28th, 2011)

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean values are going against my assumption on execution of events

    now i get it, theres is no wrong with the code itself, i was the one at fault,
                // if-else statement for red square
                if (e.getX() >= 30 && e.getX() <= 60 &&
                    e.getY() >= 30 && e.getY() <= 60) {
     
                    shouldFadeMiddleSquare = true;
                    System.out.println("RED " + shouldFadeMiddleSquare);
                }
                else {
     
                    shouldFadeMiddleSquare = false;
                }
     
                // if-else statement for the blue square
                if (e.getX() >= 130 && e.getX() <= 160 && 
                    e.getY() >= 30 && e.getY() <= 60) {
     
                    shouldFadeMiddleSquare = true;
                }
                else {
     
                    shouldFadeMiddleSquare = false;
                }

    when i hover the red square, the first if-else statement will be checked, then ofcourse my boolean variable will be TRUE, unfortunately i forgot that an execution of program is somehow sequential that i forgot that the second if-else statement will be checked as well (its for the blue square), when it WAS checked after checking the first if-else statement, the boolean value will be FALSE, so thats why the middle square isnt fading , or should i say, the boolean variable is always false, because theres always a final statement that will make the boolean variable FALSE,

    its really tiring, anyways , thanks sir copeg for giving a help ,

  7. #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: boolean values are going against my assumption on execution of events

    I only see one printlns in your code that would show you what is happening.
    Add a println showing the new value just after every statement that changes the values of the variable. There are 4 that I can see.
    The output should show you what is happening.

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

    chronoz13 (June 28th, 2011)

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean values are going against my assumption on execution of events

    Think about how your method works - better yet step through the code under the conditions you wish to test. Consider the scenario where your mouse is over the red square - the code first evaluates the red square, which sets your boolean to true. Next, it continues to test whether the point is in the blue square, which is false. End result is false... the way it is currently written, the only way your flag will be set to true after the end of the method call is if the last conditional test is true (eg you are over the blue square)
    oops i think we just posted almost at the same time, i just got it hehe , and yes, thats the problem, i wasnt paying attention on ALL the if-else statement, well im a bit tired, thats why i posted the code to get some little help, but fortunately, i was still able to dig the problem ,, thanks again sir

  10. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean values are going against my assumption on execution of events

    the only solution that i only know now, is to use 2 separate boolean variables to control the logic that i need to implement

  11. #9
    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: boolean values are going against my assumption on execution of events

    Another way is to set the boolean false only at the head of the method. Then only set it to true in the following code.

  12. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean values are going against my assumption on execution of events

    another lesson learned, take some rest, before debugging

  13. #11
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean values are going against my assumption on execution of events

    Another way is to set the boolean false only at the head of the method. Then only set it to true in the following code.
    i tried it! it works easier!! ,saves more line than what i was about to do :p

Similar Threads

  1. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  2. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM
  3. [SOLVED] events log
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 17th, 2010, 05:21 AM
  4. JComboBoxes & Events
    By KevinGreen in forum AWT / Java Swing
    Replies: 3
    Last Post: April 21st, 2010, 05:11 AM
  5. Connecting events
    By Olufemi in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2010, 04:58 AM