Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Java Swing Snake Game

 
Mona Smith
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goal is to create a simple snake game. I was expecting to get the snake to move around on the screen, but I am getting the error:



I'm not sure what this means but it has to do with private class Key implements @KeyListener at the bottom of the Screen class. I've seen that the problem could be that you have to put it in a different method, but mine already is. So here's my code:

Snake class:



Bodypart class:



Frame class:



Thanks in advance for your help!
 
Rob Camick
Rancher
Posts: 3095
24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code should be:



Read the section from the Swing tutorial on How to Write a KeyListener for more information.

Also, note you really should be using Key Bindings instead of a KeyListener. Read the Swing tutorial on How to Use Key Bindings.

For more information you can also check out Motion Using The Keyboard for example of both approaches.
 
Mona Smith
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Camick, it was a dumb mistake on my part!
 
You've gotta fight it! Don't give in! Read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!