gpt4 book ai didi

java - 形状未绘制在正确的位置,在其他位置绘制 20-40 像素(使用 MouseListener)

转载 作者:行者123 更新时间:2023-12-02 11:48:33 25 4
gpt4 key购买 nike

当我尝试在鼠标单击的位置绘制类似正方形的东西时,该正方形会在距离鼠标单击几个像素的地方绘制。不仅如此,当窗口第一次打开时,它似乎并没有显示为 1080 x 720(黑色背景实际上被遮挡,你必须调整窗口大小),这可能与此有关吗?

我尝试比较鼠标点击的位置和方 block 的位置,但它们是相同的。单击某个点会使方 block 移动到那里...或者它应该...(在 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);
}
}

最佳答案

改变

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 使用的坐标将相对于 的坐标空间

就个人而言,我会在 Core 的构造函数中注册 MouseListener,但您明白了

关于java - 形状未绘制在正确的位置,在其他位置绘制 20-40 像素(使用 MouseListener),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com