Phil
Phil

Reputation: 76

Shapes not painting in proper spots, paints somewhere else by 20-40 pixels (using MouseListener)

When I try painting something like a square at where my mouse clicked the square is painted several pixels away from the mouse click. Not only that but when the windows first opens up it doesn't seem to show up as 1080 by 720 (the black background is actually obscured and you have to resize the window), Could that have something to do with it?

I tried comparing where my mouse clicked and the position the square got but they were the same. Clicking on a spot makes the square move there... Or it should... (Running this on Windows 10). Any help is appreciated thank you.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Core extends JComponent implements MouseListener, ActionListener
{
private Timer timer;
private Player player;


public Core()
{
    timer = new Timer(3, this);
    timer.start();
    player = new Player(500, 500, 30, 30, 1);
}

public static void main(String[] args)
{
    JFrame window = new JFrame();
    Core game = new Core();
    window.add(game);
    window.addMouseListener(game);
    window.pack();
    window.setSize(1080, 720);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    g2.fillRect(0,0,1080,720);

    if(timer.isRunning())
        player.paint(g2);


}


@Override
public void mouseClicked(MouseEvent e)
{

     player.setNewPosition(e.getX(), e.getY());


}

@Override
public void mousePressed(MouseEvent e)
{

}

@Override
public void mouseReleased(MouseEvent e)
{

}

@Override
public void mouseEntered(MouseEvent e)
{

}

@Override
public void mouseExited(MouseEvent e)
{

}

@Override
public void actionPerformed(ActionEvent e)
{
    repaint();
}
}

import java.awt.*;

public class Player
{
private int x,y,width,length,speed;
private int newX, newY;

public Player(int x, int y, int width, int length, int speed)
{
    this.x = x;
    this.y = y;
    this.width = width;
    this.length = length;
    this.speed = speed;
    newX = x;
    newY = y;
}

public void setNewPosition(int newX, int newY)
{
    this.newX = newX;
    this.newY = newY;
}

public void move()
{
    if(x > newX)
        x -= speed;
    else if(x < newX)
        x += speed;
    if(y > newY)
        y -= speed;
    else if(y < newY)
        y += speed;
}


public void paint(Graphics2D g2) //paint method
{
    g2.setColor(Color.blue);
    move();
    g2.fillRect(x, y, width, length);
}
}

Upvotes: 0

Views: 42

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Change

public static void main(String[] args)
{
    JFrame window = new JFrame();
    Core game = new Core();
    window.add(game);
    window.addMouseListener(game);
    window.pack();
    window.setSize(1080, 720);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

To something more like...

public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            JFrame window = new JFrame();
            Core game = new Core();
            game.addMouseListener(game);
            window.add(game);
            window.pack();
            window.setSize(1080, 720);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
        }
    });
}

The coordinates used by the MouseEvent will be relative to the source's coordinate space

Personally, I'd register the MouseListener in the Cores constructor, but you get the idea

Upvotes: 1

Related Questions