Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in one of my classes

 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • 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:
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sorry, missed the line in your constructor where you add the listener ... too quick at reading
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest this thread be moved to the AWT / Swing forum.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • 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").
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic