drawString()方法上的MouseListener

时间:2015-05-08 11:24:23

标签: java graphics2d mouselistener

如何检测我使用{绘制的文字(“恢复”“重新启动”“退出”)正在点击{1}}方法?

到目前为止我的代码:

drawString()

我希望你能帮助我。感谢

@aioobe我试着尽可能简单地写。这里是SSCCE:

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); } }

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

2 个答案:

答案 0 :(得分:4)

您必须记住您绘制字符串的位置。每个字符串可以有一个Rectangle2D

String的矩形可以按如下方式计算:

Rectangle2D r = g.getFontMetrics().getStringBounds(str, g);

(您需要根据绘制字符串的位置调整矩形位置。)

然后,您将注册一个鼠标侦听器,用于检查这些矩形的点击坐标:

if (resumeRect.contains(mouseEvent.getPoint())) {
    ...
}

话虽如此,我建议您重新考虑您的GUI代码,看看您是否为此目的使用JLabelJButton

关于你的编辑:

您的NullPointerException是由于您可以在渲染图像之前(即在矩形初始化之前)单击框架。

除此之外,你需要进行两次编辑:

  1. 您需要考虑SCALE

    if (resumeRect.contains(e.getPoint().getX() / GameFrame.SCALE,
                            e.getPoint().getY() / GameFrame.SCALE)) {
    

  2. 你需要补偿drawString在基线上绘制字符串的事实,因此矩形应该从基线提升到文本的左上角:

    g.drawString("Resume", 200, 156);
    resumeRect= g.getFontMetrics().getStringBounds("Resume", g);
    
    // Add this:
    resumeRect.setRect(200,
                       156 - g.getFontMetrics().getAscent(),
                       resumeRect.getWidth(),
                       resumeRect.getHeight());
    

答案 1 :(得分:0)

不要使用drawString(),而是将文本放在JLabel中。然后,您可以将侦听器附加到标签上,这样更容易。

JLabel还具有以下优点:您可以使用html进行格式化,并允许布局管理器定位文本。

相关问题