NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Opp project:

/*
* 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
*/

/**
*
* @author S C
*/
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Game {

JFrame window;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel, playerPanel;
JLabel titleNameLabel, hpLabel, hpLabelNumber, weaponLabel, weaponLabelName;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 28);
JButton startButton, choice1, choice2, choice3, choice4;
JTextArea mainTextArea;
int playerHP, monsterHP, silverRing;
String weapon, position;

TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler choiceHandler = new ChoiceHandler();

ImageIcon logo = new ImageIcon(".//res//jackfrost.jpg");

public static void main(String[] args) {

new Game();
}

public Game() {

window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setIconImage(logo.getImage());
con = window.getContentPane();

titleNamePanel = new JPanel();
titleNamePanel.setBounds(100, 100, 600, 150);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("ADVENTURE");
titleNameLabel.setForeground(Color.white);
titleNameLabel.setFont(titleFont);

startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);

startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);

titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);

con.add(titleNamePanel);
con.add(startButtonPanel);

window.setVisible(true);
}

public void createGameScreen() {
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);

mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
mainTextArea = new JTextArea("This is the main text are. This game is going to be great. I'm sure of it!!!!!!!");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextArea.setWrapStyleWord(true);
mainTextArea.setEditable(false);

mainTextPanel.add(mainTextArea);

choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(250, 350, 300, 150);
choiceButtonPanel.setBackground(Color.black);
choiceButtonPanel.setLayout(new GridLayout(4, 1));
con.add(choiceButtonPanel);
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.black);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.setFocusPainted(false);
choice1.addActionListener(choiceHandler);
choice1.setActionCommand("c1");
choiceButtonPanel.add(choice1);
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.black);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.setFocusPainted(false);
choice2.addActionListener(choiceHandler);
choice2.setActionCommand("c2");
choiceButtonPanel.add(choice2);
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.black);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.setFocusPainted(false);
choice3.addActionListener(choiceHandler);
choice3.setActionCommand("c3");
choiceButtonPanel.add(choice3);
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.black);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.setFocusPainted(false);
choice4.addActionListener(choiceHandler);
choice4.setActionCommand("c4");
choiceButtonPanel.add(choice4);

// choice4.setContentAreaFilled(false); // Disable highlighting on press!!!
playerPanel = new JPanel();
playerPanel.setBounds(100, 15, 600, 50);
playerPanel.setBackground(Color.black);
playerPanel.setLayout(new GridLayout(1, 4));
con.add(playerPanel);
hpLabel = new JLabel("HP:");
hpLabel.setFont(normalFont);
hpLabel.setForeground(Color.white);
playerPanel.add(hpLabel);
hpLabelNumber = new JLabel();
hpLabelNumber.setFont(normalFont);
hpLabelNumber.setForeground(Color.white);
playerPanel.add(hpLabelNumber);
weaponLabel = new JLabel("Weapon:");
weaponLabel.setFont(normalFont);
weaponLabel.setForeground(Color.white);
weaponLabel.setBackground(Color.red);
playerPanel.add(weaponLabel);
weaponLabelName = new JLabel();
weaponLabelName.setFont(normalFont);
weaponLabelName.setForeground(Color.white);
playerPanel.add(weaponLabelName);

playerSetup();

}

public void playerSetup() {

playerHP = 15;
monsterHP = 20;
weapon = "Knife";
weaponLabelName.setText(weapon);
hpLabelNumber.setText("" + playerHP);

townGate();
}

public void townGate() {
position = "townGate";
mainTextArea.setText("You are at the gate of the town. nA guard is standing in front of you. nnWhat do you do?");
choice1.setText("Talk to the guard");
choice2.setText("Attack the guard");
choice3.setText("Leave");
choice4.setText("");
}

public void talkGuard() {
position = "talkGuard";
mainTextArea.setText("Guard: Hello stranger. I have never seen your face. nI'm sorry but we cannot let a stranger enter our town.");
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}

public void attackGuard() {
position = "attackGuard";
mainTextArea.setText("Guard: Hey don't be stupid!nnThe guard fought back and hit you hard.n(You receive 3 damage)");
//playerHP = playerHP -3;
playerHP -= 3;
hpLabelNumber.setText("" + playerHP);
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}

public void crossRoad() {
position = "crossRoad";
mainTextArea.setText("You are at a crossroad.nIf you go south, you will go back to the town.");
choice1.setText("Go north");
choice2.setText("Go east");
choice3.setText("Go south");
choice4.setText("Go west");
}

public void north() {
position = "north";
mainTextArea.setText("There is a river. nYou drink the water and rest at the riverside. nn(Your HP is recovered by 2)");
playerHP = playerHP + 2;
hpLabelNumber.setText("" + playerHP);
choice1.setText("Go south");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}

public void east() {
position = "east";
mainTextArea.setText("You walked into a forest and found a Long Sword!nn(You obtained a Long Sword)");
weapon = "Long Sword";
weaponLabelName.setText(weapon);
choice1.setText("Go west");
choice2.setText("");
choice3.setText("");
choice4.setText("");

}

public void west() {
position = "west";
mainTextArea.setText("You encounter a goblin!");
choice1.setText("Fight");
choice2.setText("Run");
choice3.setText("");
choice4.setText("");
}

public void fight() {
position = "fight";
mainTextArea.setText("Monter HP: " + monsterHP + "nnWhat do you do?");
choice1.setText("Attack");
choice2.setText("Run");
choice3.setText("");
choice4.setText("");
}

public void playerAttack() {
position = "playerAttack";

int playerDamage = 0;

if (weapon.equals("Knife")) {
playerDamage = new java.util.Random().nextInt(3);
} else if (weapon.equals("Long Sword")) {
playerDamage = new java.util.Random().nextInt(12);
}

mainTextArea.setText("You attacked the monster and gave " + playerDamage + " damage!");

monsterHP = monsterHP - playerDamage;

choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}

public void monsterAttack() {
position = "monsterAttack";

int monsterDamage = 0;

monsterDamage = new java.util.Random().nextInt(6);

mainTextArea.setText("The monster attacked you and gave " + monsterDamage + " damage!");

playerHP = playerHP - monsterDamage;
hpLabelNumber.setText("" + playerHP);

choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}

public void win() {
position = "win";

mainTextArea.setText("You defeated the monster!nThe monster dropped a ring!nn(You obtained a Silver Ring)");

silverRing = 1;

choice1.setText("Go east");
choice2.setText("");
choice3.setText("");
choice4.setText("");

}

public void lose() {
position = "lose";

mainTextArea.setText("You are dead!nnGAME OVER");

choice1.setText("");
choice2.setText("");
choice3.setText("");
choice4.setText("");
choice1.setVisible(false);
choice2.setVisible(false);
choice3.setVisible(false);
choice4.setVisible(false);
}

public void ending() {
position = "ending";

mainTextArea.setText("Guard: Oh you killed that goblin!?nThank you so much. You are true hero!nWelcome to our town!nnTHE END");

choice1.setText("");
choice2.setText("");
choice3.setText("");
choice4.setText("");
choice1.setVisible(false);
choice2.setVisible(false);
choice3.setVisible(false);
choice4.setVisible(false);
}

public class TitleScreenHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {

createGameScreen();
}
}

public class ChoiceHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {

String yourChoice = event.getActionCommand();

switch (position) {
case "townGate":
switch (yourChoice) {
case "c1":
if (silverRing == 1) {
ending();
} else {
talkGuard();
}
break;
case "c2":
attackGuard();
break;
case "c3":
crossRoad();
break;
}
break;
case "talkGuard":
switch (yourChoice) {
case "c1":
townGate();
break;
}
break;
case "attackGuard":
switch (yourChoice) {
case "c1":
townGate();
break;
}
break;
case "crossRoad":
switch (yourChoice) {
case "c1":
north();
break;
case "c2":
east();
break;
case "c3":
townGate();
break;
case "c4":
west();
break;
}
break;
case "north":
switch (yourChoice) {
case "c1":
crossRoad();
break;
}
break;
case "east":
switch (yourChoice) {
case "c1":
crossRoad();
break;
}
break;
case "west":
switch (yourChoice) {
case "c1":
fight();
break;
case "c2":
crossRoad();
break;
}
break;
case "fight":
switch (yourChoice) {
case "c1":
playerAttack();
break;
case "c2":
crossRoad();
break;
}
break;
case "playerAttack":
switch (yourChoice) {
case "c1":
if (monsterHP < 1) {
win();
} else {
monsterAttack();
}
break;
}
break;
case "monsterAttack":
switch (yourChoice) {
case "c1":
if (playerHP < 1) {
lose();
} else {
fight();
}
break;
}
break;
case "win":
switch (yourChoice) {
case "c1":
crossRoad();
}
break;

}

}
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.