The code is an implementation of a simple Java Swing application called "PetQuestX." The application appears to be a game where a player (represented by an icon) can move around using arrow keys and interact with a pet character. Here's a brief overview of the project structure:
PetQuestX.java:
This class is the main class of the application.
It sets up the main JFrame and uses CardLayout to manage different panels.
It has methods to create various panels like titlePanel, namePanel, resultPanel, and gamePanel.
The gamePanel is responsible for handling player movement using KeyListeners and interacting with the pet character.
The application has a timer to track the elapsed time.
Player.java:
This class represents the player in the game.
It has attributes for the player's icon, x and y coordinates.
Provides methods to get and set the x and y coordinates of the player.
The draw method is responsible for rendering the player's icon on the screen.
The game uses two images - "mario.png" for the player and "kong.png" for the pet character.
The user interacts with the application by clicking buttons and entering their name.
After entering the name, a result panel is displayed, showing a greeting message and a gamer tag generated based on the rearrangement of the name.
The result panel also contains buttons for additional information (How to Play, Controls, Character Ability, Start).
The game has a timer that updates the elapsed time.
Player movement is handled by arrow keys.
There's collision detection between the player and pet character, with the pet's position updating when the player intersects.
It's important to note that the code assumes the existence of specific image files ("mario.png" and "kong.png") at specific paths, and these images are used for representing the player and the pet in the game. The paths to these images may need to be adjusted based on the actual project structure.
PetQuestX.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package petquestx;
/**
*
* @author sagar_xbitlabs
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class PetQuestX {
private static JFrame frame;
private static JPanel mainPanel;
private static JPanel titlePanel;
private static JPanel namePanel;
private static JPanel resultPanel;
private static JTextField nameField;
private static JLabel playerLabel;
private static JLabel petLabel;
private static JLabel timerLabel;
private static Timer gameTimer;
private static int playerX = 50;
private static int playerY = 50;
private static int petX = 200;
private static int petY = 50;
private static int elapsedTime = 0;
private static Player player;
public static void main(String[] args) {
frame = new JFrame("PetQuestX");
frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel(new CardLayout());
frame.add(mainPanel);
createTitlePanel();
createNamePanel();
createResultPanel();
createGamePanel();
// Make the frame visible
frame.setVisible(true);
// start the game timer
startGameTimer();
}
private static void startGameTimer(){
int delay = 100; // milliseconds
ActionListener taskPerformer = new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
updateTimer();
}
};
gameTimer = new Timer(delay, taskPerformer);
gameTimer.start();
}
private static void updateTimer(){
elapsedTime++;
timerLabel.setText("Time: " + elapsedTime);
}
private static void handlePlayerMovement(KeyEvent e){
int step = 10;
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
player.setY(Math.max(0, player.getY() - step));
System.out.println("UP key pressed");
break;
case KeyEvent.VK_DOWN:
player.setY(Math.min(500 - 32, player.getY() + step)); // Adjust the maximum limit
System.out.println("DOWN key pressed");
break;
case KeyEvent.VK_LEFT:
player.setX(Math.max(0, player.getX() - step));
System.out.println("LEFT key pressed");
break;
case KeyEvent.VK_RIGHT:
player.setX(Math.min(700 - 32, player.getX() + step)); // Adjust the maximum limit
System.out.println("RIGHT key pressed");
break;
}
// Check for intersection with pet and update pet position
if (player.getX() + 32 >= petLabel.getX() && player.getX() <= petLabel.getX() + 32
&& player.getY() + 32 >= petLabel.getY() && player.getY() <= petLabel.getY() + 32) {
petLabel.setLocation(player.getX() + 50, player.getY());
}
// Update the game panel
repaintGamePanel();
}
private static void repaintGamePanel() {
gamePanel.repaint();
}
private static JPanel gamePanel;
private static void createGamePanel() {
gamePanel = new JPanel(null);
ImageIcon playerIcon = new ImageIcon("/home/sagar_xbitlabs/NetBeansProjects/java_projects/images/mario.png");
ImageIcon petIcon = new ImageIcon("/home/sagar_xbitlabs/NetBeansProjects/java_projects/images/kong.png");
// creating a player instance
player = new Player(playerIcon, 50, 50);
// creating a player label with image/ icon
playerLabel = new JLabel(playerIcon);
playerLabel.setBounds(player.getX(), player.getY(), 50, 50); // Adjust the position and size as needed
gamePanel.add(playerLabel);
// Create a pet label with an image/icon
petLabel = new JLabel(petIcon);
petLabel.setBounds(300, 50, 50, 50); // Adjust the position and size as needed
gamePanel.add(petLabel);
// set up key listener
gamePanel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// Unused method, but needs to be implemented due to the KeyListener interface
}
@Override
public void keyPressed(KeyEvent e) {
handlePlayerMovement(e);
}
@Override
public void keyReleased(KeyEvent e) {
// Unused method, but needs to be implemented due to the KeyListener interface
}
});
gamePanel.setFocusable(true);
// other components
mainPanel.add(gamePanel, "gamePanel");
gamePanel.requestFocusInWindow();
}
private static void createTitlePanel() {
titlePanel = new JPanel(null);
titlePanel.setBackground(Color.YELLOW);
JLabel titleLabel = new JLabel("PetQuestX");
titleLabel.setBounds(150, 30, 100, 25);
titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
titleLabel.setForeground(Color.RED);
titlePanel.add(titleLabel);
JButton continueButton = new JButton("Click to continue");
continueButton.setBounds(120, 80, 160, 25);
titlePanel.add(continueButton);
continueButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "namePanel");
}
});
mainPanel.add(titlePanel, "titlePanel");
}
private static void createNamePanel() {
namePanel = new JPanel(null);
namePanel.setBackground(Color.YELLOW);
JLabel enterNameLabel = new JLabel("Enter the name:");
enterNameLabel.setBounds(150, 30, 200, 25);
namePanel.add(enterNameLabel);
nameField = new JTextField();
nameField.setBounds(120, 80, 160, 25);
namePanel.add(nameField);
JButton submitButton = new JButton("Submit");
submitButton.setBounds(120, 120, 160, 25);
namePanel.add(submitButton);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "resultPanel");
displayResultPanel();
}
});
mainPanel.add(namePanel, "namePanel");
}
private static void createResultPanel() {
resultPanel = new JPanel(null);
resultPanel.setBackground(Color.GREEN);
JLabel resultLabel = new JLabel("Greetings!");
resultLabel.setBounds(150, 30, 500, 25);
resultPanel.add(resultLabel);
JLabel playerNameLabel = new JLabel();
playerNameLabel.setBounds(120, 80, 500, 25);
resultPanel.add(playerNameLabel);
JLabel gtTagLabel = new JLabel();
gtTagLabel.setBounds(120, 120, 500, 25);
resultPanel.add(gtTagLabel);
mainPanel.add(resultPanel, "resultPanel");
}
private static void displayResultPanel() {
String playerName = nameField.getText();
JLabel playerNameLabel = (JLabel) resultPanel.getComponent(1);
String welcomeText = "Greetings, " + playerName;
playerNameLabel.setText(welcomeText);
String gtTag = rearrangeString(playerName);
JLabel gtTagLabel = (JLabel) resultPanel.getComponent(2);
String gtTagText = "Your Gamer Tag is: " + gtTag;
gtTagLabel.setText(gtTagText);
// Buttons for additional information
JButton howToPlayButton = new JButton("How to Play");
howToPlayButton.setBounds(50, 180, 120, 25);
resultPanel.add(howToPlayButton);
JButton controlsButton = new JButton("Controls");
controlsButton.setBounds(50, 220, 120, 25);
resultPanel.add(controlsButton);
JButton characterAbilityButton = new JButton("Character Ability");
characterAbilityButton.setBounds(50, 260, 140, 25);
resultPanel.add(characterAbilityButton);
JButton startButton = new JButton("Start");
startButton.setBounds(50, 300, 120, 25);
resultPanel.add(startButton);
// Action listeners for the additional buttons
howToPlayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Add your logic for 'How to Play' here
System.out.println("How to Play button clicked");
}
});
controlsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Add your logic for 'Controls' here
System.out.println("Controls button clicked");
}
});
characterAbilityButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Add your logic for 'Character Ability' here
System.out.println("Character Ability button clicked");
}
});
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Switch to the 'Game Screen'
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "gamePanel");
}
});
}
public static String rearrangeString(String input) {
// Check if the input string has at least 4 characters
if (input.length() < 4) {
System.out.println("Input string should have at least 4 characters");
return input;
}
// Extract first two characters from the end
String firstTwoFromEnd = input.substring(input.length() - 2);
// Extract last two characters from the start
String lastTwoFromStart = input.substring(0, 2);
// Concatenate the rearranged string
String rearrangedString = firstTwoFromEnd + input.substring(2, input.length() - 2) + lastTwoFromStart;
return rearrangedString;
}
}
Player.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package petquestx;
import java.awt.Graphics;
import javax.swing.ImageIcon;
/**
*
* @author sagar_xbitlabs
*/
public class Player {
private ImageIcon icon;
private int x;
private int y;
public Player(ImageIcon icon, int x, int y){
this.icon = icon;
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public void setX(int x){
this.x = x;
}
public int getY(){
return y;
}
public void draw(Graphics g){
icon.paintIcon(null, g, x, y);
}
void setY(int y) {
this.y = y;
}
}