0

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

4
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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • How would I edit my code? I don't understand....paintComponent takes a graphics object. Where do I get that graphics object.... –  Oct 10 '12 at 19:30
  • Im new to java development. I just googled it and dont really know where to start.... I just want to know what to do with my Path2D object to add it to a jpanel. For future questions I will definitely try to stick to this SSCCE format. –  Oct 10 '12 at 19:34
  • 1
    There's a related example [here](http://stackoverflow.com/a/6697773/230513) using the `Path2D` subclass, `GeneralPath`. – trashgod Oct 10 '12 at 19:49