所以我有一个包含BufferedImage的面板,我想在该图像上画一条线,重叠它。
我尝试过以下从谷歌发现的示例,但它似乎不起作用:
public class Main {
private JFrame frame = new JFrame();
private JLayeredPane lpane = new JLayeredPane();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
public Main()
{
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
panel1.add(image);
panel1.setBounds(0, 0, 600, 400);
panel1.setOpaque(true);
panel2.add(linedraw1);
panel2.setBounds(200, 100, 100, 100);
panel2.setOpaque(true);
lpane.add(panel1, new Integer(0), 0);
lpane.add(panel2, new Integer(1), 0);
frame.pack();
frame.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
}
以下代码只显示了一个空白的gui,我尝试在框架中添加一个单独的面板,但是当我这样做时,只显示新面板而没有其他内容。 有什么想法吗?
提前致谢。
答案 0 :(得分:0)
创建一个扩展JPanel的自定义Panel。绘制图像和该面板中的线条并将其添加到UI中。
这是一个让你入门的样本
public class Main {
private JFrame frame = new JFrame();
private JLayeredPane lpane = new JLayeredPane();
private JPanel panel1 = new MyPanel("C:\\Users\\PATH\\Pictures\\Image.png");
private JPanel panel2 = new JPanel();
public Main()
{
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
panel1.setBounds(0, 0, 600, 400);
panel1.setOpaque(true);
// panel2.add(linedraw1);
panel2.setBounds(200, 100, 100, 100);
panel2.setOpaque(false);
lpane.add(panel1, new Integer(0), 0);
lpane.add(panel2, new Integer(1), 0);
frame.pack();
frame.setVisible(true);
}
// This is your custom panel
class MyPanel extends JPanel {
private static final long serialVersionUID = -4559408638276405147L;
private String imageFile;
public MyPanel(String imageFile) {
this.imageFile = imageFile;
}
@Override
protected void paintComponent(Graphics g) {
// Add your image here
Image img = new ImageIcon(imageFile).getImage();
g.drawImage(img, 0, 0, this);
//Add your lines here
g.setColor(Color.black);
g.drawLine(0, 0, g.getClipBounds().width, g.getClipBounds().height);
g.setColor(Color.red);
g.drawLine(0, g.getClipBounds().height, g.getClipBounds().width, 0);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
}