• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Error in one of my classes

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening everyone!

Ive made an object class called Rectangle.java
and im playing around with it, just trying to get things to work.
Ive put this method into it


but It gives me this error:



Can anyone help me understand this?
Many thanks!
K Pillay
 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry the name of the class is Rectangles.java and not Rectangle.java as I stated in the above post.
 
Sheriff
Posts: 22656
126
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
Have you imported java.awt.Graphics or java.awt.*?
 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehe thanks for the reply, I worked out that I had forgotten to import awt maybe 2 mins before checking this thread. However I have another problem now...



My code has to switch from the title screen to the other section of the paintcomponent method when a key is pressed. Can anyone work out why it isnt?
 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By sticking in a bunch of System.out.println statements into all my methods to narrow where my error is.
And ive worked out that the problem is that when I press a key, the keyPressed method isnt called at all. I cant seem to work out why.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KeyEvents always come from some source, most usually some sort of text field. A KeyListener receives KeyEvents from that source only after it has been registered to the source via the addKeyListener() method. A class that does not have this method most likely never issues key events, maning that it is somewhat unaware of keyboard imput. In your case, however, it's possible: A JPanel can receive keyboard interactions and has an addKeyListener() method - inherited from java.awt.Component, by the way. So you just have to add the KeyListener to your JPanel, in case of your (pretty un-clean) way of implementing JPanel and KeyListener in one class, this would be one line in your constructor:
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tutorial located here says:

Note: To fire keyboard events, a component must have the keyboard focus.



I would suggest checking to ensure the component has focus.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, missed the line in your constructor where you add the listener ... too quick at reading
 
Rob Spoor
Sheriff
Posts: 22656
126
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
I suggest this thread be moved to the AWT / Swing forum.
 
Marshal
Posts: 75877
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right, Rob, here goes . . . to the Swing Forum!
 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops sorry for posting in the wrong forum. My lecturer suggested (Through email) that I put requestFocusInWindow in my constructor method, but this hasn't worked either.
 
Campbell Ritchie
Marshal
Posts: 75877
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keshan Pillay:
My lecturer suggested (Through email) that I put requestFocusInWindow in my constructor method, but this hasn't worked either.

Not something I am familiar with myself, but I would have thought requesting focus goes inside a Listener associated with a particular component, not the constructor. Read this.

BTW: It's constructor or method, not both ("constructor method").
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic