ADVANCED JAVA FRONT PG-merged
ADVANCED JAVA FRONT PG-merged
BANGALORE-560068
SUBMITTED BY:
BASAVARAJU L: 1MJ22IS017
DHIRAJ ACHARYA: 1MJ22IS025
MOHAMED MUNTHASIR ABAAN: 1MJ22IS060
MOHAMMED MUNAZIR: 1MJ22IS061
SUBMITTED TO:
VENKATESH G
INTRODUCTION
The Snake game is a classic arcade game that has been enjoyed by players for decades. It
involves controlling a snake that moves around the screen, eating food to grow longer, and
avoiding collisions with the walls or itself. Implementing the Snake game in Java can be a
great way to practice object-oriented programming, handling user input, and managing game
state.
1. Game Board: The game is played on a grid. The board can be represented as a 2D array
where each cell can be empty, contain part of the snake, or contain food.
2. Snake: The snake is made up of a series of segments, usually implemented as a list of
coordinates. It moves in a given direction, and each segment follows the one before it.
3. Food: Food appears randomly on the board. When the snake's head moves over the food,
the snake grows, and new food is placed on the board.
4. Collision Detection: The game must detect when the snake hits the wall or itself to end
the game.
5. User Input: The direction of the snake is controlled by user input, typically through
keyboard arrow keys.
This introduction covers the basic structure of a Snake game in Java. You can enhance it by
adding features such as levels, different types of food, and improved graphics.
IDENTITY
Advanced Java refers to the aspects of Java programming beyond the basic language
features, often involving enterprise-level applications, graphical user interfaces (GUIs), and
sophisticated data management. It includes various technologies and frameworks that enable
developers to build robust, scalable, and feature-rich applications.
Swing is one of the key libraries for building graphical user interfaces (GUIs) in Java. It is
part of the Java Foundation Classes (JFC) and provides a rich set of GUI components and
tools to create desktop applications.
Swing Overview
Swing is a GUI toolkit that provides a set of lightweight components for building user
interfaces. It is built on top of the Abstract Window Toolkit (AWT) and offers a more
sophisticated set of components and functionalities.
1. ActionListener:
ActionListener is an interface in java that defines a single method,
actionPerformed(ActionEvent e), which is called when an action occurs.
Method(s):
actionPerformed(ActionEvent e)
2. KeyListener:
Purpose: This interface is used to receive keyboard events.
Method(s):
keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
Classes:
1. java.awt.Dimension:
Purpose: Represents a width and height dimension.
2. java.awt.Graphics:
Purpose: Provides methods for drawing shapes, text, and images.
3. java.awt.Color:
Purpose: Defines colors used for drawing and rendering.
4. java.awt.Font:
Purpose: Represents font information for text rendering.
5. javax.swing.JPanel:
Purpose: A container that can be used to group and manage other Swing
components.
Method(s):
setPreferredSize(Dimension d)
setBackground(Color c)
addKeyListener(KeyListener l)
6. javax.swing.Timer:
Purpose: Fires action events at specified intervals.
Method(s):
start()
stop()
7. java.util.Random:
Purpose: Generates pseudo-random numbers.
Method(s):
nextInt(int bound)
8. java.util.ArrayList:
Purpose: Implements a resizable array.
Method(s):
add(E e)
get(int index)
size()
9. java.awt.event.ActionEvent:
Purpose: Represents an action event.
10. java.awt.event.KeyEvent:
Purpose: Represents a key event.
Summary
Interfaces: ActionListener and KeyListener.
Classes: Dimension, Graphics, Color, Font, JPanel, Timer, Random, ArrayList,
ActionEvent, KeyEvent, and the custom SnakeGame and Tile classes.
Methods:
In SnakeGame: Methods for handling the game logic, rendering, and user input.
In Tile: Constructor to initialize tile positions.
PROGRAM 2
Classes:
1) JFrame JFrame is a class in the Java Swing library used for creating graphical user
interfaces . It provides a window with standard decorations such as a title, border, and
buttons for closing, minimizing, and maximizing the window.
2) JPanel JPanel is a Java Swing component that provides a container for other
components.
3) JTextField JTextField is a Java Swing component that allows users to enter and edit text.
It's a single-line text field that can be used to input and display text, such as names,
password, etc.
4) JButton JButton is a Java Swing component that creates a button with a label or icon
that can be clicked by the user to perform an action.
5) JLabel JLabel is a Java Swing component that displays text, images, or both. It's a non-
editable label that can be used to display information, instructions, or decorative text in a
graphical user interface
METHODS:
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
int x;
int y;
Tile(int x, int y) {
this.x = x;
this.y = y;
int boardWidth;
int boardHeight;
Tile snakeHead;
ArrayList<Tile> snakeBody;
Tile food;
Random random;
int velocityX;
int velocityY;
Timer gameLoop;
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
setBackground(Color.black);
addKeyListener(this);
setFocusable(true);
placeFood();
velocityX = 1;
velocityY = 0;
gameLoop = new Timer(100, this);
gameLoop.start();
super.paintComponent(g);
draw(g);
g.setColor(Color.red);
g.setColor(Color.green);
if (gameOver) {
g.setColor(Color.red);
else {
food.x = random.nextInt(boardWidth/tileSize);
food.y = random.nextInt(boardHeight/tileSize);
placeFood();
if (i == 0) {
snakePart.x = snakeHead.x;
snakePart.y = snakeHead.y;
else {
snakePart.x = prevSnakePart.x;
snakePart.y = prevSnakePart.y;
snakeHead.x += velocityX;
snakeHead.y += velocityY;
gameOver = true;
gameOver = true;
@Override
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}
@Override
velocityX = 0;
velocityY = -1;
velocityX = 0;
velocityY = 1;
velocityX = -1;
velocityY = 0;
velocityX = 1;
velocityY = 0;
PROGRAM 2
import javax.swing.*;
public class App {
frame.setVisible(true);
frame.setSize(boardWidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(snakeGame);
frame.pack();
snakeGame.requestFocus();
}
OUTPUT