You overwrite each component added to the contentPane in successive calls to add(..) .
I want both of them to be in the center, because I want to draw lines from point to point
You should use the approach that one DrawingPanel , to which objects can be attached. You may also need an array (s) to hold the shapes you need to draw, and therefore they can be easily retrieved and processed by paintComponent(..) .
Please remember to do all Swing creation and manipulation of the Dispatch Thread Event via SwingUtilities.inovkeLater(..)
don't call setSize() on the JFrame rather call pack() before setting the JFrame visible, rather override getPrefferedSize or setPrefferedSize from JPanel and return a dimension that matches all the drawings in it.
There is no need to getContentPand.add(..) just call add(..) on the JFrame , as the call will be directed to contentPane
Here is an example (basically you are the code with the corrections above)

import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.setTitle("DrawMatrix"); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawPanel()); frame.pack(); frame.setVisible(true); } }); } } class DrawPanel extends JPanel { final static int DIST = 20; final static int MAX_ROW = 11; final static int MAX_COL = 11; int i = 1; @Override public void paintComponent(Graphics g) { super.paintComponent(g);
source share