我有以下代码:
// 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);
生成此图像:
我想知道是否可以在同一个 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/