Search code examples
javaswinggraphics

Drawing Lines into the same JPanel


I am a java beginner and I want to try drawing lines into a existing JPanel, but when I draw it, it always creates a new JPanel and put the whole thing into my existing JPanel. I understand that I call a class that extends from JPanel and when I create a new instance of that class it creates a new JPanel. The problem is I don't know how to get the lines in the same panel without creating a new one.

Here is my main, where I create the JFrame and the JPanel inside:

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                LineTest main = new LineTest();
                JFrame window = new JFrame();
                JPanel panel = new JPanel();

                window.setTitle("Line-Test");
                window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                
                panel.setPreferredSize(new Dimension(500, 500));
                panel.setBackground(Color.RED);

                window.add(panel);
                window.pack();
                window.setLocationRelativeTo(null);
                window.setResizable(false);
                window.setVisible(true);


                main.DrawLines(panel);
            }
        });
    }

    private void DrawLines(JPanel panel)
    {
        Vector<Integer> startPoint = new Vector<Integer>();
        startPoint.add(2);
        startPoint.add(0);

        HorizontalLine lineH = new HorizontalLine(startPoint, 360);
        VerticalLine lineV = new VerticalLine(startPoint, 360);
        panel.add(lineH);
        panel.add(lineV);
        panel.repaint();
    }

And here the class where i try to paint the lines:

class Line extends JPanel
    {
        private Vector<Integer> startPoint = new Vector<Integer>();
        private Vector<Integer> endPoint = new Vector<Integer>();

        public Line(Vector<Integer> pointStart, int length, String direction)
        {
            startPoint.add(pointStart.get(0));
            startPoint.add(pointStart.get(1));
            if(direction == "horizontal")
            {
                endPoint.add(startPoint.get(0) + length);
                endPoint.add(startPoint.get(1));
            }
            else
            {
                endPoint.add(startPoint.get(0));
                endPoint.add(startPoint.get(1) + length);
            }
        }

        public void draw(Graphics g) {
            g.drawLine(startPoint.get(0), startPoint.get(1), endPoint.get(0), endPoint.get(1));
        }

        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            draw(g);
        }
    }

    class HorizontalLine extends Line
    {
        public HorizontalLine(Vector<Integer> startPoint, int length)
        {
            super(startPoint, length, "horizontal");
        }
    }

    class VerticalLine extends Line
    {
        public VerticalLine(Vector<Integer> startPoint, int length)
        {
            super(startPoint, length, "vertical");
        }
    }

And here is the outcome: Outcome


Solution

  • Your class Line extend JPanel, that's why, when you create a new Line a new JPanel appeared. You need to override paintComponent(Graphics g) method. Have a look here:

    public class LineDrawingPanel extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            // Draw lines
            g.setColor(Color.RED);
            g.drawLine(10, 10, 100, 100);
            g.drawLine(100, 100, 200, 50);
            // Add more lines as needed
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("Line Drawing Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new LineDrawingPanel());
            frame.setSize(300, 200);
            frame.setVisible(true);
        }
    }