形状不在适当的位置绘制,在其他地方绘制20-40像素(使用MouseListener)

问题描述 投票:0回答:1

当我尝试在鼠标点击的地方画一个像正方形的东西时,正方形被绘制成距离鼠标点击几个像素。不仅如此,当Windows首次打开时,它似乎没有显示为1080×720(黑色背景实际上是模糊的,你必须调整窗口大小),这可能与它有关吗?

我试着比较我的鼠标点击的位置和广场的位置,但它们是相同的。单击一个点使广场移动到那里......或者它应该......(在Windows 10上运行)。任何帮助表示赞赏谢谢。

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);
}
}
java swing mouselistener
1个回答
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 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);
        }
    });
}

MouseEvent使用的坐标将相对于source的坐标空间

就个人而言,我在MouseListeners构造函数中注册了Core,但是你明白了

© www.soinside.com 2019 - 2024. All rights reserved.