javauser-interfacemousemotionlistener

Is it possible to draw on a Java gui canvas and track what the mouse drew on the canvas?


I am making a game where the user is given a letter on screen and they have to trace over the letter, what I want to know is when the user is drawing can I trace what they are drawing to see if they are drawing the letter correctly.

I was thinking of using an if statement saying if the mouse is been dragged along the coordinates of the letter, as in where the letter is on screen, then it is correct, otherwise it is wrong.

Would that work?


Solution

  • Well, yes. What you said is technically right - that would work.

    Normally for this kind of game, you would have some sort of matrix that represents the canvas' pixels. You should know which ones of those pixels belong to the letter (because it is covering parts of the canvas). As the player begins the session, you can have another matrix of the same size (but empty), and as the player drags the mouse, some of the pixels in the matrix would get flagged.

    You should compare both matrixes to see how "right" was the player (how many flagged pixels are inside the letter). Maybe if there is a 85% match rate, it means the player drew the letter. You also need to account for cheating (like the player trying to paint the entire screen), but that will depend on your game's mechanics.