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 ?_?