Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:04:42+00:00 2026-06-05T08:04:42+00:00

I decided to re-create Snake using Java, but I’m sort of stuck. At the

  • 0

I decided to re-create Snake using Java, but I’m sort of stuck. At the moment, I have a square that the user can move around the screen using the arrow keys. When you press LEFT once, the square begins to move left using a timer.. You don’t need to hold down the key or keep pressing it; it changes direction when you press any of the other keys that are set(Right, Up, Down). My goal is to use an ArrayList to hold the squares that make up the snake. At the moment, I’ve created an ArrayList with just one Snake object inside, if I add a second Snake object to the list and add it to the frame(with the first), only one Snake object is visible and the keys to move it don’t function. I’m looking for some ideas as to how I can progress with this project – please don’t give me the full answer, because I’d like to figure out most of it on my own; I just need some direction. Thanks in advance – code is below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Snake extends JPanel implements KeyListener, ActionListener
{
int x = 0, y = 0, velx = 0, vely = 0;
Timer t = new Timer(250, this);


public Snake(int num1, int num2)
{
    t.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(true);
    x = num1;
    y = num2;
}
public void paintComponent(Graphics g)
{   
    super.paintComponent(g);

    g.setColor(Color.blue);
    g.fillRect(x, y, 20, 20);
}
public void actionPerformed(ActionEvent e)
{
    repaint();
    x += velx;
    y += vely;
}
public void up()
{
    vely = -20;
    velx = 0;
}
public void down()
{
    vely = 20;
    velx = 0;
}
public void left()
{
    vely = 0;
    velx = -20;
}
public void right()
{
    vely = 0;
    velx = 20;
}
public void keyPressed(KeyEvent e)
{
    int code = e.getKeyCode();

    if(code == KeyEvent.VK_UP)
        up();
    else if(code == KeyEvent.VK_DOWN)
        down();
    else if(code == KeyEvent.VK_RIGHT)
        right();
    else if(code == KeyEvent.VK_LEFT)
        left();

}
public void keyReleased(KeyEvent e)
{

}
public void keyTyped(KeyEvent e)
{

}
}
//Driver Class
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class UserClass
{
private static JFrame frame = new JFrame("Snake");
private static ArrayList<Snake> mySnake = new ArrayList<Snake>();

public static void main(String[] args)
{
    frame.setSize(500,500);
    frame.setBackground(Color.black);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mySnake.add(new Snake(20,20));
    frame.add(mySnake.get(0));
}
}

P.S When I put this same exact code in Eclipse on my Mac, the background of my frame is black, but on Windows it’s light gray… Anyone know why? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:04:43+00:00Added an answer on June 5, 2026 at 8:04 am

    To answer your postscript, set a background color or make a JPanel behind everything else with a painted color.

    To progress with the project, consider making it an exercise in the MVC framework. What is happening now is that your Model and View are linked in the same class – this makes it hard to follow the logic behind everything.

    What you can do is first separate your Snake from anything to do with the view – make a snake with an ArrayList<Integer[]> segmentLocations or something, to represent the (x,y) of each of the segment locations on a board that you define – you can use absolute coordinates or you can make an arbitrary grid and change to absolute coordinates in your View (this typifies the MVC relationship better). Snake should also have a field for the direction the snake is facing – I would use an enum Direction {N, S, E, W}, but you have options on that, as you could also have an integer representing direction, or a variety of other ways.

    Your Snake would then also have ways to update itself – move(), shifting the location of all of the segments based on the current direction for the initial segment and causing all of the other segments to follow the movement of the one before it (this is pretty easy if you consider it for a couple of minutes).

    Your view could be a JFrame with a GridLayout consisting of JPanels which poll your Snake instance and see if there is a segment in the location and if so, draw it, or a multitude of other options.

    Your controller would be the KeyAdapter that sends the updates in direction to your Snake when the arrow keys are pressed.

    Small hint, to make your life easier: when you add a new segment, just have it at the location of the last segment of the Snake. The next time it moves, the new segment will be in the same location, and the rest of the Snake should have move accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I decided to create a currency converter in Java, and have it so that
For a project I have to create a music visualizer in java (I've decided
I have decided to create a new table in my database that will be
I am new to JSF 2.0 and Primefaces but have decided to create my
I have to develop a customized tab control and decided to create it with
I decided to create custom wizard page in my inno-setup-based installer. But i do
First some background: The company I work for have decided to create an iPhone
I am working on understanding collision detection and have decided to create a program
I am using wordpress to create blog similar to stackoverflow.com where users can ask
I've decided to create a web based chat system for the experience. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.