0

I have a task to set up a mouse dragger to move my square on a labyrinth in Java, I had set up the keys already but strugging to figure out a nice way to add the other one.

public class Player extends JPanel{
    int x, y;
    
    public Player() {
        this.setBackground(Color.getHSBColor(0.3f, 0.3f, 1));
        this.setSize(Maze.panelSize, Maze.panelSize);
    }

    public void moveLeft() {
        if(x > 0 && Maze.map[x-1][y] == 1){
            this.setLocation(this.getX()-25, this.getY());
            x--;
        }
    }

    public void moveRight() {
        if(x < Maze.columns-1 && Maze.map[x+1][y] == 1){
            this.setLocation(this.getX()+25, this.getY());
            x++;
        }
    }

    public void moveUp() {
        if(y > 0 && Maze.map[x][y-1] == 1){
            this.setLocation(this.getX(), this.getY()-25);
            y--;
        }
    }

    public void moveDown() {
        if(y < Maze.rows-1 && Maze.map[x][y+1] == 1){
            this.setLocation(this.getX(), this.getY()+25);
            y++;
        }
    }
}
  • Maybe change your code so there is only a move(dx, dy) method. Now you just provide a delta-x and delta-y parameter (keyboard would be like a digital D-PAD on a game pad, and mouse drag would be location.x - pressed.x and location.y - pressed.y –  Oct 17 '20 at 16:25
  • I'm kinda new to coding and I have no idea how to do that, isn't there a way to drag the square "player" to move it on the indicated new tile? – Jager98 Oct 17 '20 at 16:35
  • After some thought about how different game development is to application development, I think you'd have more success by researching and asking questions on the game dev Stack site - example: https://gamedev.stackexchange.com/questions/30460/how-can-i-implement-keylisteners-actionlisteners-into-the-jframe –  Oct 17 '20 at 16:47
  • Your question could benefit from more contextual information and probably more code, since it's hard to tell what you are trying to do with a snippet of a class that is part of a larger program. Consider creating and posting a valid [mre] program in your question, one that includes your mouse listeners. Also, I have to wonder if Player should not extend from JPanel. – DontKnowMuchBut Getting Better Oct 17 '20 at 16:49
  • Sorry, when I added more code I couldn't post it, because it was too much, therefore that's all I was able to fit, thank you for the replies so far :) – Jager98 Oct 17 '20 at 16:52
  • Yeah. It's tough to do simple Q and A for game development when it's not being developed on a standard platform (Unity, JMonkey, LibGDX). There are quite literally a thousand ways you can implement game code... though most would be wrong :) –  Oct 17 '20 at 16:57
  • See: https://stackoverflow.com/a/6811800/131872 for a basic example that allows you to drag a JLabel from one grid square to another. – camickr Oct 17 '20 at 17:18
  • Thank you very much, that's what I needed! – Jager98 Oct 17 '20 at 17:33
  • Jager98: if @camickr's answer is the answer to your question, then this question is likely a duplicate of the one that he answered (duplicate enough so that his solution works for you) – DontKnowMuchBut Getting Better Oct 17 '20 at 17:51

0 Answers0