#How can I modify my mouseDragged method to prevent smearing?

8 messages · Page 1 of 1 (latest)

vast seal
#

public void mouseDragged(MouseEvent e) {
Image pieceImage = null;
int x = e.getX();
int y = e.getY();

    if (selectedPiece != "") {
        // Get the image for the selected piece
        switch (selectedPiece) {
            case "K":
                pieceImage = pieceImages[6];
                break;
            case "Q":
                pieceImage = pieceImages[7];
                break;
            case "B":
                pieceImage = pieceImages[10];
                break;
            case "N":
                pieceImage = pieceImages[9];
                break;
            case "R":
                pieceImage = pieceImages[8];
                break;
            case "P":
                pieceImage = pieceImages[11];
                break;

        }

    }
    // Draw the selected piece image if there is one
    if (pieceImage != null) {
        Graphics2D g2d = (Graphics2D) getGraphics();
        g2d.drawImage(pieceImage, x - 25, y - 25, 50, 50, null);
    }
}
quaint sequoiaBOT
#

This post has been reserved for your question.

Hey @vast seal! 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.

fierce zenith
#

By not drawing outside of a paint() call

vast seal
fierce zenith
#

What do I mean by that?
Err, instead of painting inside a mouse move event, like you do, instead you would not do that

vast seal
#

Well so I moved it into my reload board method ( seen below ) and it prevents the smearing but since the board and the piece are being redrawn over and over it isnt perfectly smooth

fierce zenith
#

We're getting out of my knowledge domain, but you should try making your component not extend JPanel, as... It is not a panel