2

I am currently learning java and have chosen a project that is both fun, yet semi challenging. My project is based upon an old 70s - 80s game that I once played as a kid, called DopeWars.

This is my second attempt at building this game, the first attempt was made by putting all my code within one .java file, which was successful to the point of having to stop as a result constantly getting lost within mountains of code.

My second attempt, I tried to break up my code into logical sections, shared between many, but small sized .java files. I thought this method would be alot easier to handle but have found it has created many more problems than anything else.

What should happen: When navigating through my running application, the user is prompted to select his/her player. From the default starting country, the user can then purchase/sell some of the fine products on offer. The user can then press the 'travel' jbutton (xx), choose a destination by pressing lets say the england jbutton(tEng), and then be shipped off to the chosen destination to reap the rewards of his investment. Hopefully the user is lucky enough to find a good deal on a product on offer at his new destination aswell.

My Problem: What ever I try I cannot travel to a new destination. When I click the country I want to go to, the same country im at pops up again. Currently its set to create the new 'country' object when the button is pressed. Also, i'd like it to do the following: When the user leaves England, travels to Germany, and then returns back to England. Englands randomly generated prices refresh from the first time in England. Or vice versa with Germany.

All I can think is that the problem lies where the England Object, once opened, needs to be closed. Allowing for a new England Object to be created when the user next navigates back to England, and thus generating new random prices for the user to either buy or sell at.

My attempts, based on my theory, have been too somehow destroy, dispose or remove the England object on the press of jbutton xx (travels button).

I would ideally like to keep the bulk of my code in place, as any radical changes will probably confuse the hell out of me. I have also removed all the code I have failed with. Any help will be greatly appreciated. Thanks in advance.

PlayerSelect.java:

package ggg;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ggg.GUI;

public class PlayerSelect implements ActionListener{

JFrame fr;
JPanel playerSelect;
JButton playerSelect1, playerSelect2, playerSelect3, playerSelect4, playerSelect5, playerSelect6;
JLabel q, n1, n2, n3, n4, n5, n6;

public PlayerSelect(){

    fr = new JFrame("DopeWars 2014");
    fr.setSize(580, 445);
    fr.setLocation(10, 10);
    fr.setLayout(null);
    fr.setBackground(Color.black);
    fr.setLocationRelativeTo(null);
    fr.setVisible(true);

    playerSelect = new JPanel();
    playerSelect.setSize(650, 500);
    playerSelect.setLocation(0, 0);
    playerSelect.setLayout(null);
    playerSelect.setBackground(Color.black);
    playerSelect.setVisible(true);

    playerSelect1 = new JButton();
    playerSelect1.setSize(177, 135);
    playerSelect1.setLocation(10, 115);
    playerSelect1.setLayout(null);
    playerSelect1.setBackground(Color.pink);
    playerSelect1.addActionListener(this);

    playerSelect2 = new JButton();
    playerSelect2.setSize(177, 135);
    playerSelect2.setLocation(197, 115);
    playerSelect2.setLayout(null);
    playerSelect2.setBackground(Color.green);
    playerSelect2.addActionListener(this);

    playerSelect3 = new JButton();
    playerSelect3.setSize(177, 135);
    playerSelect3.setLocation(384, 115);
    playerSelect3.setLayout(null);
    playerSelect3.setBackground(Color.yellow);
    playerSelect3.addActionListener(this);

    playerSelect4 = new JButton();
    playerSelect4.setSize(177, 135);
    playerSelect4.setLocation(10, 265);
    playerSelect4.setLayout(null);
    playerSelect4.setBackground(Color.magenta);
    playerSelect4.addActionListener(this);

    playerSelect5 = new JButton();
    playerSelect5.setSize(177, 135);
    playerSelect5.setLocation(197, 265);
    playerSelect5.setLayout(null);
    playerSelect5.setBackground(Color.red);
    playerSelect5.addActionListener(this);

    playerSelect6 = new JButton();
    playerSelect6.setSize(177, 135);
    playerSelect6.setLocation(384, 265);
    playerSelect6.setLayout(null);
    playerSelect6.setBackground(Color.blue);
    playerSelect6.addActionListener(this);

    q = new JLabel("Select your player to begin.");
    q.setSize(600, 50);
    q.setLocation(10, 50);
    q.setForeground(Color.white);
    q.setFont(q.getFont().deriveFont(18.0f));

    n1 = new JLabel("Paul");
    n1.setSize(157, 25);
    n1.setLocation(10, 10);
    n1.setBackground(Color.white);

    n2 = new JLabel("Richard");
    n2.setSize(157, 25);
    n2.setLocation(10, 10);
    n2.setBackground(Color.white);

    n3 = new JLabel("Lisa");
    n3.setSize(157, 25);
    n3.setLocation(10, 10);
    n3.setBackground(Color.white);

    n4 = new JLabel("Roger");
    n4.setSize(157, 25);
    n4.setLocation(10, 10);
    n4.setBackground(Color.white);

    n5 = new JLabel("Katherine");
    n5.setSize(157, 25);
    n5.setLocation(10, 10);
    n5.setBackground(Color.white);

    n6 = new JLabel("Harold");
    n6.setSize(157, 25);
    n6.setLocation(10, 10);
    n6.setBackground(Color.white);



    playerSelect1.add(n1);
    playerSelect2.add(n2);
    playerSelect3.add(n3);
    playerSelect4.add(n4);
    playerSelect5.add(n5);
    playerSelect6.add(n6);

    playerSelect.add(q);
    playerSelect.add(playerSelect1);
    playerSelect.add(playerSelect2);
    playerSelect.add(playerSelect3);
    playerSelect.add(playerSelect4);
    playerSelect.add(playerSelect5);
    playerSelect.add(playerSelect6);
    fr.add(playerSelect);

}

@Override
public void actionPerformed(ActionEvent a) {

        GUI b = new GUI();
        England e = new England();

        if (a.getSource() == playerSelect1){
                b.img.setBackground(Color.pink);
                b.img.add(n1);
                b.bg.add(e.england);
                fr.dispose();
        }
        if (a.getSource() == playerSelect2){
                b.img.setBackground(Color.green);
                b.img.add(n2);
                b.bg.add(e.england);
                fr.dispose();
        }
        if (a.getSource() == playerSelect3){
                b.img.setBackground(Color.yellow);
                b.img.add(n3);
                b.bg.add(e.england);
                fr.dispose();
        }
        if (a.getSource() == playerSelect4){
                b.img.setBackground(Color.magenta);
                b.img.add(n4);
                b.bg.add(e.england);
                fr.dispose();
        }
        if (a.getSource() == playerSelect5){
                b.img.setBackground(Color.red);
                b.img.add(n5);
                b.bg.add(e.england);
                fr.dispose();
        }
        if (a.getSource() == playerSelect6){
                b.img.setBackground(Color.blue);
                b.img.add(n6);
                b.bg.add(e.england);
                fr.dispose();
        }
        }
}

GUI.java:

package ggg;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ggg.England;
import ggg.Germany;
import ggg.Engine;

public class GUI extends Engine implements ActionListener {

    JFrame f;
    JPanel bg, s, img, t;
    JLabel £, ca, co;
    static JLabel cav, cov;
    JButton x, xx, y, z, btEng, btGerm;

    public GUI(){
           f = new JFrame("FFF");

           bg = new JPanel();
           bg.setSize(650, 500);
           bg.setBackground(Color.darkGray);
           bg.setLayout(null);
           bg.setLocation(0, 0);

           s = new JPanel();
           s.setSize(180, 445);
           s.setBackground(Color.white);
           s.setLayout(null);
           s.setLocation(10, 10);

           img = new JPanel();
           img.setSize(160, 120);
           img.setBackground(Color.blue);
           img.setLayout(null);
           img.setLocation(10, 10);

           x = new JButton("Travel");
           x.setSize(136, 30);
           x.setLocation(202, 10);
           x.addActionListener(this);

           xx = new JButton("Travels");
           xx.setSize(136, 30);
           xx.setLocation(202, 10);
           xx.addActionListener(this);
           xx.setVisible(false);

           y = new JButton("Finance");
           y.setSize(136, 30);
           y.setLocation(349, 10);
           y.addActionListener(this);

           z = new JButton("Investment");
           z.setSize(136, 30);
           z.setLocation(496, 10);
           z.addActionListener(this);





           // STATS WINDOW
           ca = new JLabel("CASH:");
           ca.setSize(70,25);
           ca.setLocation(10, 145);

           £ = new JLabel("£");
           £.setSize(10, 25);
           £.setLocation(80, 145);

           cav = new JLabel();
           cav.setSize(80, 25);
           cav.setLocation(90, 145);
           int aaa = Engine.playerCash;
           String bbb = String.valueOf(aaa);
           cav.setText(bbb);

           co = new JLabel("COCAINE:");
           co.setSize(70, 25);
           co.setLocation(10, 190);

           cov = new JLabel();
           cov.setSize(70, 25);
           cov.setLocation(80, 190);
           int ccc = Engine.playerCoca;
           String ddd = String.valueOf(ccc);
           cov.setText(ddd);

           //Travel Window
           t = new JPanel();
           t.setSize(430, 403);
           t.setBackground(Color.white);
           t.setLayout(null);
           t.setLocation(202, 52);
           t.setVisible(false);

           btEng = new JButton("ENGLAND");
           btEng.setSize(120, 25);
           btEng.setLocation(10, 100);
           btEng.addActionListener(this);

           btGerm = new JButton("GERMANY");
           btGerm.setSize(120, 25);
           btGerm.setLocation(140, 100);
           btGerm.addActionListener(this);

           f.setSize(650, 500);
           f.setLayout(null);
           f.setLocationRelativeTo(null);
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           f.setVisible(true);




           // ADD COMPONENT
           s.add(img);
           s.add(ca);
           s.add(£);
           s.add(cav);
           s.add(co);
           s.add(cov);

           t.add(btEng);
           t.add(btGerm);          

           bg.add(s);
           bg.add(x);
           bg.add(xx);
           bg.add(y);
           bg.add(z);
           bg.add(t);      

           f.add(bg);
    }





    @Override
    public void actionPerformed(ActionEvent a) {
           England e = new England();
           Germany g = new Germany();

           if (a.getSource() == x){
               t.setVisible(true);
           }
           if (a.getSource() == y){

           }
           if (a.getSource() == z){

           }
           if (a.getSource() == xx){
               t.setVisible(true);
               x.setVisible(true);
               xx.setVisible(false);
           }
           if (a.getSource() == btEng){
               bg.add(e.england);
               bg.remove(g.germany);///////////
               xx.setVisible(true);
               x.setVisible(false);
               t.setVisible(false);
           }
           if (a.getSource() == btGerm){
               bg.add(g.germany);
               bg.remove(e.england);
               xx.setVisible(true);
               x.setVisible(false);
               t.setVisible(false);
           }
    }
}

Engine.java:

package ggg;

import javax.swing.JLabel;
import ggg.England;
import ggg.GUI;

public class Engine {

    public static int playerCash = 120;
    public static int playerCoca = 0;

    public int cocaPrice;
    public int cocaQuantity;
    JLabel cp, cq;
    public int cpp, cqq;
    public String cqqq;
    public int total;
    public int deduction;

    public void buyEng(){   

            England.error.setText(null);

            // CocaPrice
            cp = new JLabel();
            cp.setText(England.pr.getText());
            cpp = Integer.parseInt(cp.getText());
            cocaPrice = cpp;

            // CocaQuantity
            cq = new JLabel();
            cq.setText(England.tf.getText());
            cqqq = cq.getText();
            cqq = Integer.parseInt(cqqq);
            cocaQuantity = cqq;     


            // CALCULATIONS
            total = cocaPrice * cocaQuantity;    

            if (playerCash >= total){

                playerCoca = playerCoca + cocaQuantity;
                String q = String.valueOf(playerCoca);

                playerCash = playerCash - total;
                String t = String.valueOf(playerCash);

                GUI.cav.setText(t);
                GUI.cov.setText(q);
                England.tf.setText(null);
                return;
            }
            else {
                England.tf.setText(null);
                England.error.setText("You don't have enough money!");
            }
            return;
    }



    public void sellEng(){

            if (playerCoca > 0){

                England.error.setText(null);

                // CocaPrice
                cp = new JLabel();
                cp.setText(England.pr.getText());
                cpp = Integer.parseInt(cp.getText());
                cocaPrice = cpp;

                // CocaQuantity
                cq = new JLabel();
                cq.setText(England.tf.getText());
                cqqq = cq.getText();
                cqq = Integer.parseInt(cqqq);
                cocaQuantity = cqq;     


                // CALCULATIONS
                total = cocaPrice * cocaQuantity;  

                playerCoca = playerCoca - cocaQuantity;
                String q = String.valueOf(playerCoca);

                playerCash = playerCash + total;
                String t = String.valueOf(playerCash);

                GUI.cav.setText(t);
                GUI.cov.setText(q);
                England.tf.setText(null);
                return;
            }            
            else if (playerCoca <= 0){

                England.tf.setText(null);
                England.error.setText("You don't have anything to sell!");
            }
            return;
    }



    public void buyGerm(){  

        Germany.error.setText(null);

        // CocaPrice
        cp = new JLabel();
        cp.setText(Germany.pr.getText());
        cpp = Integer.parseInt(cp.getText());
        cocaPrice = cpp;

        // CocaQuantity
        cq = new JLabel();
        cq.setText(Germany.tf.getText());
        cqqq = cq.getText();
        cqq = Integer.parseInt(cqqq);
        cocaQuantity = cqq;     


        // CALCULATIONS
        total = cocaPrice * cocaQuantity;    

        if (playerCash >= total){

            playerCoca = playerCoca + cocaQuantity;
            String q = String.valueOf(playerCoca);

            playerCash = playerCash - total;
            String t = String.valueOf(playerCash);

            GUI.cav.setText(t);
            GUI.cov.setText(q);
            Germany.tf.setText(null);
            return;
        }
        else {
            Germany.tf.setText(null);
            Germany.error.setText("You don't have enough money!");
        }
        return;
    }



    public void sellGerm(){

        if (playerCoca > 0){

            Germany.error.setText(null);

            // CocaPrice
            cp = new JLabel();
            cp.setText(Germany.pr.getText());
            cpp = Integer.parseInt(cp.getText());
            cocaPrice = cpp;

            // CocaQuantity
            cq = new JLabel();
            cq.setText(Germany.tf.getText());
            cqqq = cq.getText();
            cqq = Integer.parseInt(cqqq);
            cocaQuantity = cqq;     


            // CALCULATIONS
            total = cocaPrice * cocaQuantity;  

            playerCoca = playerCoca - cocaQuantity;
            String q = String.valueOf(playerCoca);

            playerCash = playerCash + total;
            String t = String.valueOf(playerCash);

            GUI.cav.setText(t);
            GUI.cov.setText(q);
            Germany.tf.setText(null);
            return;
        }            
        else if (playerCoca <= 0){

            Germany.tf.setText(null);
            Germany.error.setText("You don't have anything to sell!");
        }
        return;
    }





    public static void main(String[] args) {
        new PlayerSelect();
    }
}

England.java:

package ggg;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import ggg.Engine;

public class England extends Engine implements ActionListener {

JPanel england, imgEng;

JLabel co2, £co2;
static JLabel pr;
static JLabel error;
static JTextField tf;
JButton b1, b2;


public England(){

    england = new JPanel();
    england.setSize(430, 403);
    england.setBackground(Color.red);
    england.setLayout(null);
    england.setLocation(202, 52);
    england.setVisible(true);

    imgEng = new JPanel();
    imgEng.setSize(410, 80);
    imgEng.setBackground(Color.blue);
    imgEng.setLayout(null);
    imgEng.setLocation(10, 10);

    co2 = new JLabel("COCAINE");
    co2.setSize(70, 25);
    co2.setLocation(10, 100);

    £co2 = new JLabel("£");
    £co2.setSize(10, 25);
    £co2.setLocation(80, 100);

    pr = new JLabel();
    pr.setSize(60, 25);
    pr.setLocation(90, 100);
    Random random1 = new Random();
    int x = random1.nextInt(80) + 1;
    String c = String.valueOf(x);
    pr.setText(c);

    tf = new JTextField();
    tf.setSize(70, 25);
    tf.setLocation(160, 100);   

    b1 = new JButton("BUY");
    b1.setSize(70, 25);
    b1.setLocation(270, 100);
    b1.addActionListener(this);

    b2 = new JButton("SELL");
    b2.setSize(70, 25);
    b2.setLocation(350, 100);
    b2.addActionListener(this);



    // ERROR MESSAGE
    error = new JLabel();
    error.setSize(200, 25);
    error.setLocation(10, 40);



    // ADD COMPONENTS
    england.add(imgEng);
    imgEng.add(error);

    england.add(co2);
    england.add(£co2);
    england.add(pr);
    england.add(tf);
    england.add(b1);
    england.add(b2);    
}



@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == b1){
        Engine a = new Engine();
        a.buyEng();
    }
    if (e.getSource() == b2){
        Engine a = new Engine();
        a.sellEng();
    }   
}
}

Germany.java:

package ggg;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import ggg.Engine;

public class Germany extends Engine implements ActionListener {

JPanel germany, imgGerm;

JLabel co2, £co2;
static JLabel pr;
static JLabel error;
static JTextField tf;
JButton b1, b2;


public Germany(){

    germany = new JPanel();
    germany.setSize(430, 403);
    germany.setBackground(Color.blue);
    germany.setLayout(null);
    germany.setLocation(202, 52);
    germany.setVisible(true);

    imgGerm = new JPanel();
    imgGerm.setSize(410, 80);
    imgGerm.setBackground(Color.red);
    imgGerm.setLayout(null);
    imgGerm.setLocation(10, 10);

    co2 = new JLabel("COCAINE");
    co2.setSize(70, 25);
    co2.setLocation(10, 100);

    £co2 = new JLabel("£");
    £co2.setSize(10, 25);
    £co2.setLocation(80, 100);

    pr = new JLabel();
    pr.setSize(60, 25);
    pr.setLocation(90, 100);
    Random random1 = new Random();
    int x = random1.nextInt(80) + 1;
    String c = String.valueOf(x);
    pr.setText(c);

    tf = new JTextField();
    tf.setSize(70, 25);
    tf.setLocation(160, 100);   

    b1 = new JButton("BUY");
    b1.setSize(70, 25);
    b1.setLocation(270, 100);
    b1.addActionListener(this);

    b2 = new JButton("SELL");
    b2.setSize(70, 25);
    b2.setLocation(350, 100);
    b2.addActionListener(this);



    // ERROR MESSAGE
    error = new JLabel();
    error.setSize(200, 25);
    error.setLocation(10, 40);



    // ADD COMPONENTS
    germany.add(imgGerm);
    imgGerm.add(error);

    germany.add(co2);
    germany.add(£co2);
    germany.add(pr);
    germany.add(tf);
    germany.add(b1);
    germany.add(b2);    
}



@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == b1){
        Engine a = new Engine();
        a.buyGerm();
    }
    if (e.getSource() == b2){
        Engine a = new Engine();
        a.sellGerm();
    }   
}
}
dazbrad
  • 182
  • 1
  • 12
  • 3
    That is _a lot_ of stuff we are supposed to sort through. Look into creating an [MCVE](http://stackoverflow.com/help/mcve). – takendarkk Jan 31 '14 at 20:53
  • I did actually break my code down to a simplified version because my first attempt had way to much body. As I dont know where my problem is, I dont really know what to include and what to exclude. Either way, point taken. I will look into mvce which im assuming is model-view-controller, dont really know what the e stands for but il look into that too. Thanks for the tip. – dazbrad Feb 01 '14 at 11:14
  • 1
    [MCVE](http://stackoverflow.com/help/mcve) <------ That is a link, click it and take a look. – takendarkk Feb 01 '14 at 18:23

1 Answers1

2

"My Problem: What ever I try I cannot travel to a new destination. When I click the country I want to go to, the same country im at pops up again."

I think what's going on is that you're missing the whole idea of what extending a class does. It in no way makes them reference any of the same inherited objects. In order for that to happen, a reference would need to be pass of the same object.

What I see you doing is create a new Engine() every time the button is pressed. That makes perfect sense as to why you're getting the same country popping up.

What I would recommend id maybe using a CardLayout. Since your England and Germany objects seem mainly consist of a JPanel with components and some logic, You can make those classes extends JPanel. Add those panels to a main panel with a CardLayout. When you want to go to different countries, just change the panel of the CardLayout. See this example foe how to use a CardLayout also you can go through the How to Use CardLayout tutorial

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720