drawString()方法上的java MouseListener
如何检测我使用drawString()
方法绘制的文本(“Resume”、“Restart”、“Quit”)是否正在被单击
到目前为止,我的代码是:
public class Pause {
public Pause() {
}
public void draw(Graphics2D g) {
g.setFont(new Font("Arial", Font.BOLD, 14));
int intValue = Integer.parseInt( "ff5030",16);
g.setColor(new Color(intValue));
g.drawString("Resume", 200, 156);
g.drawString("Restart", 200, 172);
g.drawString("Quit", 200, 188);
}
}
我希望你能帮助我。谢谢
@aioobe我试着把它写得尽可能简单。以下是SSCCE:
GameFrame.java
public class GameFrame extends JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
// dimensions
public static final int WIDTH = 448;
public static final int HEIGHT = 288;
public static final double SCALE = 2.5;
// game thread
private Thread thread;
private boolean running;
private int FPS = 60;
private long targetTime = 1000 / FPS;
// image
private BufferedImage image;
private Graphics2D g;
//displays
private Pause pauseDisplay;
public GameFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setLocationRelativeTo(null);
this.setPreferredSize(new Dimension((int)(WIDTH * SCALE), (int)(HEIGHT * SCALE)));
this.setBounds(100, 100, (int)(WIDTH * SCALE), (int)(HEIGHT * SCALE));
this.setLocationRelativeTo(null);
this.setFocusable(true);
this.requestFocus();
this.setVisible(true);
}
public void addNotify() {
super.addNotify();
if(thread == null) {
thread = new Thread(this);
thread.start();
}
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;
}
public void run() {
init();
long start;
long elapsed;
long wait;
// game loop
while(running) {
start = System.nanoTime();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if(wait < 0) wait = 5;
try {
Thread.sleep(wait);
}
catch(Exception e) {
e.printStackTrace();
}
pauseDisplay = new Pause(this);
drawToScreen();
draw();
}
}
private void draw() {
pauseDisplay.draw(g);
}
private void drawToScreen() {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, (int)(WIDTH * SCALE), (int)(HEIGHT * SCALE), null);
g2.dispose();
}
public static void main(String[] args) {
GameFrame game = new GameFrame();
}
}
Pause.java
public class Pause implements MouseListener{
private Rectangle2D resumeRect;
private Rectangle2D restartRect;
public Pause(GameFrame GameFrame) {
GameFrame.addMouseListener(this);
}
public void draw(Graphics2D g) {
g.setFont(new Font("Arial", Font.BOLD, 14));
int intValue = Integer.parseInt( "ff5030",16);
g.setColor(new Color(intValue));
g.drawString("Resume", 200, 156);
resumeRect= g.getFontMetrics().getStringBounds("Resume", g);
g.drawString("Restart", 200, 172);
restartRect = g.getFontMetrics().getStringBounds("Restart", g);
g.drawString("Quit", 200, 188);
}
public void mouseClicked(MouseEvent e) {
if (resumeRect.contains(e.getPoint())) {
System.out.println("clicked");
}
System.out.println(resumeRect);
System.out.println(restartRect);
System.out.println(e.getPoint());
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
# 1 楼答案
不要使用drawString(),而是将文本放在JLabel中。然后,您可以将侦听器附加到标签上,这要容易得多
JLabel还有一个优点,即您可以使用html进行格式化,并且它允许layoutmanager定位文本
# 2 楼答案
你必须记住你画绳子的位置。每个字符串可以有一个
Rectangle2D
字符串的矩形可以按如下方式计算:
(需要根据绘制字符串的位置调整rect位置。)
然后,您将注册一个鼠标侦听器,根据这些矩形检查单击坐标:
话虽如此,我还是建议您重新考虑GUI代码,看看是否可以使用
JLabel
或JButton
来实现此目的关于您的编辑:
您的NullPointerException是因为您可以在渲染图像之前(即,在初始化矩形之前)单击帧
除此之外,您还需要进行两次编辑:
您需要考虑
SCALE
和
您需要补偿drawString在基线上绘制字符串的事实,因此矩形应从基线向上提升到文本的左上角: