0

So I have a panel that has an BufferedImage in it, and I would like to draw a line over that image, overlapping it.

I have tried the following example that I found from google but it doesn't seem to work:

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

}

The following code just shows a blank gui, I tried adding a separate panel to the frame but when I do that only the new panel shows and nothing else. Any ideas?

Thanks in advance.

Lehue
  • 425
  • 8
  • 26
Commongrate
  • 123
  • 1
  • 12

1 Answers1

0

Create a custom Panel that extends JPanel. Paint your image and the lines in that panel and add it to your UI.

Here's a sample to get you started

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

}
anacron
  • 6,443
  • 2
  • 26
  • 31