0

So I have been researching Applets and using Canvas and BufferStrategy on the applet. I have gotten it to buffer and show correctly but I'm thinking that it is somehow interfering with the MouseListener. When I run the Applet and re-size the screen, the MouseListener works outside my specified canvas. So when I move the mouse over where it is drawing, the mouse coordinates freeze. Then when I move the mouse off the canvas, it works. So it is nothing wrong with the MouseListener. I will include it anyway.

MouseMotion class:

public class MouseMotion implements MouseMotionListener{
    private int x;
    private int y;
    public MouseMotion()
    {
        x = 0;
        y = 0;
        System.out.println("MouseMotion initialized");
    }

    public void mouseDragged(MouseEvent e) {}

    public void mouseMoved(MouseEvent e)
    {
        x = e.getX();
        y = e.getY();
    }

    //Return methods...
}

Mouse class:

public class Mouse implements MouseListener{
    private int clickedX;
    private int clickedY;
    private int clickedButton;

    private boolean pressed;
    private int pressedX;
    private int pressedY;
    private int pressedButton;

    public Mouse()
    {
        //Instantiates all the variables
        System.out.println("Mouse initialized");
    }

    public void mouseClicked(MouseEvent e) {
        clickedX = e.getX();
        clickedY = e.getY();
        clickedButton = e.getButton();
    }

    public void mousePressed(MouseEvent e) {
        pressed = true;
        pressedX = e.getX();
        pressedY = e.getY();
        pressedButton = e.getButton();
    }

    public void mouseReleased(MouseEvent e) {
        pressed = false;
        pressedX = 0;
        pressedY = 0;
        pressedButton = 0;
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

    //Methods that return each variable...
}

The AppletMain class:

    public class AppletMain extends Applet implements Runnable{

    //Constants
    private final int WIDTH = 720;
    private final int HEIGHT = WIDTH / 16 * 9;

    //Applet objects
    private BufferStrategy bs;
    private Canvas canvas;

    //Game loop variables
    ...

    //Engine objects
    private Mouse mouse;
    private MouseMotion mouseMotion;

    public void init()
    {
        canvas = new Canvas();

        mouse = new Mouse();
        mouseMotion = new MouseMotion();
        addMouseListener(mouse);
        addMouseMotionListener(mouseMotion);
    }

    public void render()
    {
        //Setup graphics
        Graphics2D g = (Graphics2D)bs.getDrawGraphics();

        g.clearRect(0,0,WIDTH,HEIGHT);

        //Just prints out the debugging stuff
        g.setColor(Color.BLACK);
        g.drawRect(0,0,WIDTH-1,HEIGHT-1);
        g.drawString("TPS: " + Integer.toString(tps) + " FPS: " + Integer.toString(fps), 2,20);
        g.drawString("MouseMotion: X - " + Integer.toString(mouseMotion.getX()) + " Y - " + Integer.toString(mouseMotion.getY()), 2,40);
        g.drawString("Mouse(PRESSED): X - " + Integer.toString(mouse.getPressedX()) + " Y - " + Integer.toString(mouse.getPressedY()) + " button - " + Integer.toString(mouse.getPressedButton()), 2,60);
        g.drawString("Mouse(CLICKED): X - " + Integer.toString(mouse.getClickedX()) + " Y - " + Integer.toString(mouse.getClickedY()) + " button - " + Integer.toString(mouse.getClickedButton()), 2,80);

        //Cleanup graphics
        g.dispose();
        bs.show();
    }

    public void tick()
    {
        //Ticks the game
    }

    public void start()
    {
        if(bs == null)
        {
            setIgnoreRepaint(true);
            canvas.setIgnoreRepaint(true);
            canvas.setSize(WIDTH,HEIGHT);
            setLayout(null);
            add(canvas);
            canvas.setLocation(0,0);

            canvas.createBufferStrategy(3);
            bs = canvas.getBufferStrategy();
        }

        new Thread(this).start();

        canvas.requestFocus();
    }

    public void stop()
    {
        running = false;
    }

    public void destroy()
    {
        if(running)
            running = false;
    }

    public void run()
    {
        init();
        //Game loop that calls tick() and render() 60 times per second
    }
}

In my code I drew black box around the canvas, and as I said before, when my mouse enters the black box the coordinates stop changing. The "fps" and "tps" keep going which are incremented by the game loop so I know that it is still running. When mouse exits the box, though, the coordinates again start changing as I move it around the window.

I tried to follow examples of other people and I believe I have. Maybe my approach to this is all wrong, I don't know, but any help, answers, comments, tips anything are greatly appreciated.

tyty5949
  • 1,480
  • 5
  • 14
  • 17

0 Answers0