Search code examples
javaswingjpanelpaintrunnable

mouseMoved Event not working


I've been trying to work this code, it's like when you hover over the start button it should change its color to gray, but whenever i hover over it. nothing happens, can somebody tell me why? i didn't get any error and it seems like my mousemoved listener isn't recognized by the compiler, sorry for my english. I haven't finish it yet but here is the code:

class Contents extends JFrame implements Runnable {
    private Image dbi;
    private Graphics dbg;
    private boolean isStarted, isHovered;
    private int x,y,xDir,yDir,bx,by,timer,life,my,mx,mhx,mhy;
    private Rectangle startgame = new Rectangle(80,100,150,40);
    Contents()
    {
    super();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    if(isStarted)
    setSize(600,600);
    else
    {    
    setSize(300,300);
    setBackground(Color.BLUE);
    }
    setLocationRelativeTo(null);
    isStarted = false;
    isHovered = false;
    addMouseListener(new MouseAdapter()
    {
        public void mousePressed(MouseEvent e)
        {
            mx = e.getX();
            my =  e.getY();
            if(mx > startgame.x && mx < startgame.x+startgame.width &&
               my > startgame.y && my < startgame.y+startgame.height)
            {
                isStarted = true;
            }
        }
        public void mouseMoved(MouseEvent e)
        {
            mhx = e.getX();
            mhy =  e.getY();
            if(mhx > startgame.x && mhx < startgame.x+startgame.width &&
               mhy > startgame.y && mhy < startgame.y+startgame.height)
               isHovered = true;
            else
               isHovered = false;

        }
    });
    }

    public void  paint(Graphics g)
    {
        dbi = createImage(getWidth(), getHeight());
        dbg = dbi.getGraphics();
        draw(dbg);
        g.drawImage(dbi,0,0,this);
        repaint();
    }
    public void draw(Graphics g)
    {
        if(!isStarted)
        {
                if(!isHovered)
                g.setColor(Color.GRAY);
                else
                g.setColor(Color.GREEN);
                    g.fillRect(startgame.x, startgame.y, startgame.width, startgame.height);
                    g.setFont(new Font("Serif",Font.BOLD,24));
                    g.setColor(Color.WHITE);
                    g.drawString("Start game", startgame.x+20, startgame.y+25);
                    g.drawString(String.format("hoverx: %d hovery: %d",mhx,mhy), 50,200);
        }
        else
        {
        }
    }
    public void run()
    {
    } }

public class Game  {
    public static void main(String[] args)
    {
        Contents c = new Contents();
    } }

Solution

  • Just use Rectangle.contains(Point) to check if the point from the MouseEvent is inside the Rectangle. Here is an example

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.Rectangle2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class PaintedButton extends JPanel {
        private static final Color HOVER_COLOR = Color.BLUE;
        private static final Color NON_HOVER_COLOR = Color.GREEN;
        private static final Rectangle2D RECTANGLE = new Rectangle2D.Double(50, 50,
                200, 100);
    
        private Color color = NON_HOVER_COLOR;
    
        public PaintedButton() {
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    Point p = e.getPoint();
                    if (RECTANGLE.contains(p)) {
                        color = HOVER_COLOR;
                    } else {
                        color = NON_HOVER_COLOR;
                    }
                    repaint();
                }
            });
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(color);
            g2.fill(RECTANGLE);
    
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 200);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new PaintedButton());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }