Search code examples
javaswingawtjava-2dcustom-painting

Adding path2d to a jpanel


Im having a lot of problems with this really trivial thing. I want to take a path2d object and add it to a jpanel and display it in my gui. Can I get some guidance as to what I am doing incorrectly. I used the search here and with google and couldn't find anything helpful so forgive me if this has been asked before.

pseudocode: panel being passed in is the root Pane

public void stuff(Path2D path, JPanel panel){
    JPanel inside = new JPanel();
    Graphics g2d = (Graphics2D) inside.getGraphics();

    g.draw(path);
    panel.add(inside);
} 

I probably have a really bad fundamental misunderstanding about what is going on. A little guidance would really help. Thank you


Solution

  • Graphics g2d = (Graphics2D) inside.getGraphics();
    

    Don't call getGraphics() except on a BufferedImage - it will be overdrawn next paint. Otherwise paint the path when told to do so within paintComponent(Graphics).

    See Performing Custom Painting for further details.