Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java make object move like a real snake PLEASE HELP!!!

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please help me with some suggestions of what I am doing wrong
I want to make the snake move its parts separately like a  real snake but mine moves together up and down which is not really what I was trying to achieve
Here is my code, thank you!









 
Rancher
Posts: 3280
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not directly related to your question but:

1. Class names should start with an upper case character. That is it should be "Snake".

2. Custom painting is done by overriding the paintComponent(...) method, not paint(...) and the first statement should be super.paintComponent(...) to clear the background

3. The Snake object should NOT extend GameObject. It is NOT a JPanel. It is a class that has a draw(...) method to paint the snake.

4. The snake head Icon should be read in the constructor of the class. You don't want to read it every time you paint the snake.

I want to make the snake move its parts separately like a  real snake


Define move like a real snake. As best as I can tell you have two parts to the snake, the head and the body.

How should it move. If you move the head one square, what should happen to the body? If you don't move it at the same time you will have an empty square between the snake and the body.
 
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ion.

The reason that all the parts are moving as a whole is because you're treating everything as a whole.

The code you wrote is a classic example of mixing business logic and UI code.

You can easily solve your problem by first writing a Snake class that encapsulates the basic properties and behavior of a creature that moves with segmented body parts. Forget about the UI for now. Here is a start:

It's up to you to design Position and Direction. Hint: Direction is an enum.

Try out something and when you get stuck, share your progress.
 
Saloon Keeper
Posts: 26047
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Not directly related to your question but:

1. Class names should start with an upper case character. That is it should be "Snake".

2. Custom painting is done by overriding the paintComponent(...) method, not paint(...) and the first statement should be super.paintComponent(...) to clear the background



... etc.

Swing, like many GUI systems, has two aspects. The aspect that you program is responsible for defining the graphics. However Swing has its own private thread that handles the actual drawing. That's because when you have an overlapping window system where another window might partially cover your application window and then that other window gets moved or removed, then the covered ("damaged") part needs to be drawn in. By defining the graphics as a generic graphic-language "program", Swing can determine what parts need to be re-drawn and what parts are OK. That makes rendering graphics faster, since you don't have to re-draw everything, plus there's less likelihood that the entire window will "flash" as it's redrawn.

Whether it's a good idea to invoke super.paintComponent(), I'm not sure, since usually renderers sort layers (the "Z-axis") and can take care of that themselves.
 
Rob Camick
Rancher
Posts: 3280
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swing, like many GUI systems, has two aspects.....


Have no idea what that blurb is about or why it relates to my general painting suggestion, which is common advice.

Whether it's a good idea to invoke super.paintComponent(), I'm not sure,



Swing components have a contract that states if they are opaque they are responsible for painting the background of the component.

If a component is non-opaque then Swing will find the first parent component that is opaque and invoke repaint(...) on it to make sure the background is cleared.

In this case it may not be necessary because the custom painting code looks like it is painting a grid to cover the entire panel.

However, the code appears to set the preferred size of the panel to 500 x 520, but the painting code appears to only paint a grid of 500 x 500.

There is the potential for painting artifacts if the background of the panel is not fully painted and this potential will increase if the frame is resized.

So, it is always a good idea to invoke super.paintComponent() to avoid this possibility. The default JPanel implementation will just paint the panel its background color. This is a common solution to painting related issues.

Read the section from the Swing tutorial on A Closer Look at the Painting Mechanism for more information.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic