#Mouse not working

1 messages · Page 1 of 1 (latest)

stiff lantern
#

I dont know why but my code does not enter the mouseDragged and mouseMoved subprograms what change do i need to make to fix this?

here is my code -

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class Main {
static JFrame frame = new JFrame("Monkey Throw");
static int WINDOWWIDTH, WINDOWHEIGHT = 500;
public static void main(String[] args) {
EventTracker tracker = new EventTracker();

    HomePage home = new HomePage(tracker);
    Screen screen = new Screen(home);
    home.passScreen(screen);


    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(WINDOWWIDTH,WINDOWHEIGHT);
    frame.setVisible(true);
    frame.setLayout(null);

    frame.setContentPane(screen);
}

}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class EventTracker extends JPanel implements ActionListener, MouseMotionListener{

JButton playButton;
HomePage homePage;

private int mouseX,mouseY;

EventTracker() {
    addMouseMotionListener(this);

}
public void homePageSetUp (JButton playButton, HomePage homePage) {
    this.playButton = playButton;
    this.homePage = homePage;
    playButton.addActionListener(this);
}

public void actionPerformed (ActionEvent e) {
    if (e.getSource() == playButton) {
        homePage.enterGame();
    }
}
@Override
public void mouseDragged(MouseEvent e) {
    mouseY = e.getY();
    mouseX = e.getX();
    System.out.println("moo");

    
}

@Override
public void mouseMoved(MouseEvent e) {
    System.out.println("moo");
}

public int getMouseX() {
    return mouseX;
}
public int getMouseY() {
    return mouseY;
}

}

calm condorBOT
#

This post has been reserved for your question.

Hey @stiff lantern! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

calm condorBOT
#
I dont know why but my code does not enter the mouseDragged and mouseMoved subprograms what change do i need to make to fix this?

here is my code - 

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class Main {
    static JFrame frame = new JFrame("Monkey Throw");
    static int WINDOWWIDTH, WINDOWHEIGHT = 500;
    public static void main(String[] args) {
        EventTracker tracker = new EventTracker();
        
     
        HomePage home = new HomePage(tracker);
        Screen screen = new Screen(home);
        home.passScreen(screen);

    
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(WINDOWWIDTH,WINDOWHEIGHT);
        frame.setVisible(true);
        frame.setLayout(null);

        frame.setContentPane(screen);
    }
   
}






import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class EventTracker extends JPanel implements ActionListener, MouseMotionListener{

    JButton playButton;
    HomePage homePage;

    private int mouseX,mouseY;

    EventTracker() {
        addMouseMotionListener(this);

    }
    public void homePageSetUp (JButton playButton, HomePage homePage) {
        this.playButton = playButton;
        this.homePage = homePage;
        playButton.addActionListener(this);
    }

    public void actionPerformed (ActionEvent e) {
        if (e.getSource() == playButton) {
            homePage.enterGame();
        }
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        mouseY = e.getY();
        mouseX = e.getX();
        System.out.println("moo");

        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("moo");
    }

    public int getMouseX() {
        return mouseX;
    }
    public int getMouseY() {
        return mouseY;
    }

}
stuck wasp
#

What's HomePage and Screen?

#

The EventTracker will only register it when you drag over the EventTracker panel

stiff lantern
#

Solved it now thanks to your comment i didn't realise it was using panel so i just removed that and added it to the original frame and fixed it fixed it all, thanks for pushing me in the right direction!

calm condorBOT