在 java 中的图像上画一条线
Drawing a line over an image in java
所以我有一个面板,其中有一个 BufferedImage,我想在该图像上画一条线,重叠它。
我尝试了从 google 中找到的以下示例,但它似乎不起作用:
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();
}
}
下面的代码只显示了一个空白的图形用户界面,我尝试在框架中添加一个单独的面板,但是当我这样做时,只显示新面板,没有其他内容。
有什么想法吗?
提前致谢。
创建一个扩展 JPanel 的自定义面板。在该面板中绘制您的图像和线条并将其添加到您的 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();
}
}
所以我有一个面板,其中有一个 BufferedImage,我想在该图像上画一条线,重叠它。
我尝试了从 google 中找到的以下示例,但它似乎不起作用:
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();
}
}
下面的代码只显示了一个空白的图形用户界面,我尝试在框架中添加一个单独的面板,但是当我这样做时,只显示新面板,没有其他内容。 有什么想法吗?
提前致谢。
创建一个扩展 JPanel 的自定义面板。在该面板中绘制您的图像和线条并将其添加到您的 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();
}
}