• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Implementing repaint() to panel after mouseclick

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner with this Java GUI (comp sci has taught only basic basic things) and I am trying to get mouseclick to work on a panel but when the mouseclick method is called the drawings I make in them are not repainted to the panel. How would I go about doing this? And I do know that the mousepressed is working since I have tried doing a System.close(0) with it and it has worked, it is just the graphics that are screwy. Please help!





 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is that to do proper graphics programming in Swing you (we) first have to shed many assumptions. For instance you should not extract a JPanel's Graphic object as you're doing it and assuming that it will persist because it won't. Read a tutorial or two on Swing graphics and you'll see that you should do all of your drawing in the JPanel's paintComponent method only and use the Graphics object that has been passed as a parameter (but don't try to save it as again it won't persist).

e.g.,


Some other things you might want to check out: I'd set the preferred size (not the size -- your current JPanel is size 10 x 10) of the drawing panel, and leave the JFrame size alone -- let it decide what is the best size based on its components. I'd pack and set the JFrame visible after adding components not before. I wouldn't set the background color of the JFrame but rather its components or its content pane.

Much luck.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic