Unable to draw lines on existing panel

I am writing my first Java interface with Swing. I am trying to create a panel with 10X10 points and draw lines on these points.

The problem is that I can only create one of the above. This means that I can create a matrix of 10X10 points or create rows.

Here is my code:

public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("DrawMatrix"); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawRectPanel()); frame.setSize(240, 260); frame.setVisible(false); frame.getContentPane().add(new DrawLine()); frame.setVisible(true); } } public class DrawRectPanel 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); { // Points matrix int x = 0; for(int row = 1; row < MAX_ROW; row++) { x = row * DIST; for(int col = 1; col < MAX_COL; col++) { int y = col * DIST; g.setColor(Color.blue); g.drawLine(x,y,x,y+1); } } } } } public class DrawLine extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(20,20,40,40); } } 

Any idea why? should setVisible be used differently?

+4
source share
1 answer

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)

enter image description here

 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); // Points matrix int x = 0; for (int row = 1; row < MAX_ROW; row++) { x = row * DIST; for (int col = 1; col < MAX_COL; col++) { int y = col * DIST; g.setColor(Color.blue); g.drawLine(x, y, x, y + 1); } } g.drawLine(20, 20, 40, 40); } @Override public Dimension getPreferredSize() { return new Dimension(300, 300); } } 
+3
source

Source: https://habr.com/ru/post/1448083/


All Articles