0

I cannot draw a line in a panel. Every time i tried it shows a blank white window and i can't figure out what's wrong. I'm really sorry if the my problem has a simple solution and that i have wasted your time. (if something isn't understandable i will try to explain it better | English isn't my native language)

Code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class AU19d extends JPanel {
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.drawLine(10, 10, 110, 110);
    }
    public static void main(String[] args) {
        JFrame f = new JFrame("AU19d");
        JPanel p = new JPanel();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p.setPreferredSize(new Dimension(200,50));
        p.add(new AU19d());
        f.add(p);
        f.pack();

        f.setVisible(true);
    }

}
D. Kufert
  • 1
  • 1
  • Possible duplication of this [question](https://stackoverflow.com/questions/16206417/trying-to-draw-lines-with-jpanel). Yo can use `drawLine()` method. – whatthefish Apr 23 '18 at 19:17
  • Why are you adding your panel into a different panel? `p.add(new AU19d());` – takendarkk Apr 23 '18 at 19:17
  • You never set a preferred size for your AU19d, so it doesn't have one, so you can't see it. – azurefrog Apr 23 '18 at 19:19
  • Might perhaps because you didn't set the preferred size of the panel? A JPanel with nothing in it is probably 0 size. (Also, a JPanel is for putting other components inside, and makes a terrible general Swing class. Derive from `JComponent` or `Canvas`, it's much cleaner.) – markspace Apr 23 '18 at 19:19

0 Answers0