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
Code :
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 ?_?
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
Re: boolean values are going against my assumption on execution of events
Quote:
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)
Code :
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);
}
}
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)
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,
Code :
// 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 , :)
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.
Re: boolean values are going against my assumption on execution of events
Quote:
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 , :D 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 :)
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
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.
Re: boolean values are going against my assumption on execution of events
another lesson learned, take some rest, before debugging :D :D :D :D
Re: boolean values are going against my assumption on execution of events
Quote:
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