• 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

I can't override paintComponent...???

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having real trouble getting a simple paint program to work. My goal is to draw in a JPanel and save the drawing to hdd; It's only a small panel in a larger JFrame, not a drawing program, so there are only a few simple colour buttons and a clear function.

My problem is that I cannot figure out for the life of me how to override the paintComponent method; I've read the sun tutorials here: tutorial gone through literally dozens of threads like this one: paint problem and come back blank, because everytime I try to implement the code in my project there is no paintComponent method to override;

Here:


Using netbeans, I should get an "add override" message pointing to the paintComponent method shouldn't I? This is in a JFrame and I also override paint, which I've commented out for the moment as I wondered if that affected it.

General program structure:
Main class instantiates JFrame. JFrame has controls and stuff on it, including a JPanel (not paintPanel at the moment)

On the JFrame, you can draw in the JPanel as I've set it up, but I know my code is shaky and I want to sort it our by overriding the correct way; There's nothing there to override though - what am I missing???
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your paintComponent method signature is wrong as it is missing the Graphics object parameter. I'd try something like so:
 
Tau Slater
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou!!! It's that syntax that got me. I'll post details later but now I can make a start on this at last! The understanding of this has come in gradual fits and starts - your piece was the final part of the puzzle I think! - well, final so far...

thank you very much
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool. I'm glad it helped. Good luck with your project and hope to see you in here again.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you make paintComponent public? It's protected by default, and that's all the visibility it needs.
 
Tau Slater
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah I was just trying anything at that point to make it work - it's not like anything else needs to use it, but at the moment, I'm more concerned with getting a functional program than dotting the Is and crossing the Ts. I do try and observe good OO design methodology, but the truth is, I've been teaching myself Java for 10 - 12 weeks or so, not every day at that, and am very fresh in that respect. Tips always appreciated though
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Why did you make paintComponent public? It's protected by default, and that's all the visibility it needs.


yes, you're right. protected is preferred here.
 
Tau Slater
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to check on this really, but I can override paintComponent multiple times can't I? Say for instance I have my JFrame class, and in that class, are two nested classes, DrawingPanel and WritingPanel, both extending JPanel.

For my purposes, I want to draw in DrawingPanel (can do) but I also want to do a bit of g.drawstring-ing in WritingPanel. So, if I override paintComponent in both, is that bad? I'm just having probs trying to draw anything in WritingPanel at the moment. I can't set the background colour in the same way I can on PaintPanel, for instance. Do I need to do anything different? I would have thought not nesting the classes was a good start but meh.... Sorry for newbie type questions. I'm still trying to understand this;
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless a class makes paintComponent final, you can override the method in any indirect JComponent subclass. DrawingPanel and WritingPanel each have their own paintComponent method and both can be overridden independant of the other.
 
Tau Slater
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I thought, but I was just checking. I've got past that now and am stuck on the text I'm displaying in the writing panel...

Edit: I've got flickering text in the writingpanel... I'm trying to debug my code but can't see what is causing the flicker - I've taken everything down to the bare minimum to get it working, as following:

The string is flickering in the JPanel - and consuming about 10% CPU (i920 Qcore, lol) so I'm doing something pretty fundamentally wrong there... Any ideas?

 
Tau Slater
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha! Victory is mine again... Fixed it after reading this:
buffered draw without flicker

Like I say, I'm still at an early stage with all this...

 
reply
    Bookmark Topic Watch Topic
  • New Topic