Search code examples
javaswingjpanelpaintcomponent

Draw in jPanel inside a jDialog


I have jDialog, it is in BorderLayout, in "south", "north" and "center" I have jPanel with elements (with nothing in the "center"'s jPanel. "center" jPanel is called Map.

I'm thing things like :

In the main

  Graphics t = Map.getGraphics();
    paintComponent(t);

Not in the main.

public void paintComponent(Graphics g){
 super.paintComponents(g);
 g.drawLine(0, 0, 50, 150);
}

I cannot draw anything. What is the mistake I have done?


Solution

  •      JPanel Map = new JPanel() {
                 public void paintComponent( Graphics g ) {
                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D)g;
    
                    Line2D line = new Line2D.Double(10, 10, 40, 40);
                    g2.setColor(Color.blue);
                    g2.setStroke(new BasicStroke(10));
                    g2.draw(line);
                 }
            };
            getContentPane().add(Map, java.awt.BorderLayout.CENTER);
    
            Map.add(new JLabel(pseudo));
      pack();
    

    in the dlg constructor. thanks for you nohelp it worked