java - 在 JFrame Java 上在图像上方追踪一条线

标签 java swing jframe

我有以下代码:

// set up the GUI for display the PgmImage object
    JFrame f = new JFrame("PGM Image:");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });     
    f.add(this.drawImage);      
    f.pack();
    f.setVisible(true);

生成此图像:PGM image on JFrame

我想知道是否可以在同一个 JFrame 上的图像上方画一条线。 我用 A* 算法发现了一条路径并在此图像中追踪它。

坎普

最佳答案

你问:

I wonder if it's possible to draw a line above the image on the same JFrame.

  • 是的,有可能。
  • 与大多数 Swing 图形一样,您不会直接在 JFrame 中绘制,而是在派生自 JComponent 的对象的 paintComponent(Graphics g) 方法中绘制,例如 JPanel由 JFrame 保存并显示在其中。 Tutorial Link .
  • 您的解决方案的详细信息将完全取决于您问题的详细信息。

你问:

So if i draw in two paintComponet i can join the two together?

是的,您可以在 JLabel 持有的 ImageIcon 中显示图像,覆盖 JLabel 的 paintComponent(Graphics g),并在图像顶部绘制线条或其他内容。我有代码可以在这个网站的某个地方作为答案......去看看。

<小时/>

编辑
请在这里查看我的答案和代码:repaint() not calling PaintComponent to use Graphics2D .

关于java - 在 JFrame Java 上在图像上方追踪一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790048/

相关文章:

java - 如何将两个 Jtable 合并到第三个 Jtable 中?

java - 在 swing 中关闭父窗口

java - JDialog 的 Modality 设置为 Dialog.ModalityType.MODELESS : optionPane. getValue() 不再工作

java - JLayeredPane 与容器分层

java - 我可以将 REST 请求转发到其他资源吗?

java - 如何在java中的jPanel中绘图时删除所有线条?

Java - 我的 Action 事件不起作用

java - 代码以不同顺序执行,Jframe、Button 和 TextArea

java - 结果集不在 JSP 中显示数据

java - Rabbitmq 连接限制?