• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[Java] Getting stuff drawn on a swing component

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've drawn some things on a JPanel. I'd now like to retrieve the graphics (images, shapes, etc) I've drawn on the panel and pass this data to another component to add to it.
If I use the getGraphics() method of the JPanel however I'm returned a NullPointerException. What am I doing wrong?
Here's an example of what I want to do:

I hope this makes sense.
I realise one workaround is to call the paintComponent method of myPanel2 from the paintComponent method of myPanel, passing the graphics down directly, however this ends up getting messy and forces me to store myPanel2 in a location which makes it accessible from myPanel's paintComponent method.
Any help much appreciated.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the end result you are trying to achieve with this? There are several ways you could multiple layers in a component -

Add the component you are currently painting on to another container that has an overridden paint() method that inserts drawing operations after the paintChildren() method.

Make each layer of the final product a separate image. Draw to each of these images separately and then put them all together in the paintComponent() method of your component.
 
reply
    Bookmark Topic Watch Topic
  • New Topic