diff --git a/MerchantRPGCSE2102/.classpath b/MerchantRPGCSE2102/.classpath index e1badea..964c4ef 100644 --- a/MerchantRPGCSE2102/.classpath +++ b/MerchantRPGCSE2102/.classpath @@ -1,8 +1,9 @@ - + + diff --git a/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs b/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs index 838bd9d..54e493c 100644 --- a/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs +++ b/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs @@ -1,11 +1,11 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/MerchantRPGCSE2102/src/config/inventory.txt b/MerchantRPGCSE2102/src/config/inventory.txt index fcf6cb0..55691b5 100644 --- a/MerchantRPGCSE2102/src/config/inventory.txt +++ b/MerchantRPGCSE2102/src/config/inventory.txt @@ -1,11 +1,30 @@ merchant 1 -water 3 -armor 5 -food 10 +Water 2 +Candy 5 +Apple 7 +Bread 3 +Cake 14 +Jerky 9 merchant 2 -wood 3 -tarp 6 +PigIron 19 +HighQualityIron 25 +Steel 29 +WoodPlanks 10 +HighQualityWood 18 +FairyTreeWood 40 +WornSword 23 +IronSword 29 +SteelSword 38 +FairySteelSword 95 +BlessedIronDaggerofPig 50000 merchant 3 -glass 3 -tape 13 -rope 5 \ No newline at end of file +LooseGems 60 +FineCutDiamond 70 +SilverBracelets 85 +SilverNecklace 93 +SilverEmeraldRing 100 +GoldBracelets 84 +GoldNecklace 103 +GoldSapphireRing 133 +GoldDiamondCrown 210 +ExpensiveJewlery? 500 \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 9711281..9d18455 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -7,6 +7,7 @@ import javax.swing.JFrame; +import model.Map; import model.Merchant; import model.Player; import view.MapUI; @@ -20,7 +21,7 @@ public class RPGame { private ArrayList playerInventoryList = new ArrayList(); // the player's inventory list private Player _player; private Merchant _merchant1, _merchant2, _merchant3; - private boolean _movement; + public boolean _movement; private int _currentDay; private int _transactionLimit; @@ -74,9 +75,9 @@ else if (currentMerchant == 3) */ public void buildMerchants() { - _merchant1 = new Merchant("Merchant 1", 1000, merchantInventoryList1); - _merchant2 = new Merchant("Merchant 2", 1000, merchantInventoryList2); - _merchant3 = new Merchant("Merchant 3", 1000, merchantInventoryList3); + _merchant1 = new Merchant("Cheap Merchant", 200, merchantInventoryList1); + _merchant2 = new Merchant("Medium Merchant", 600, merchantInventoryList2); + _merchant3 = new Merchant("Expensive Merchant", 1000, merchantInventoryList3); } /** @@ -130,8 +131,7 @@ public void createTransaction(Player player, Merchant targetMerchant) if(_transactionLimit > 0) { toggleMovement("OFF"); - Transaction newTransaction = new Transaction(player, targetMerchant); - toggleMovement("ON"); + Transaction newTransaction = new Transaction(player, targetMerchant, this); _transactionLimit -= 1; } else @@ -140,6 +140,7 @@ public void createTransaction(Player player, Merchant targetMerchant) } } + /** * Will refresh number of transactions the Player has available * @@ -163,11 +164,13 @@ public void advanceDailyCycle(){ scaleAllMerchantPrices(allMerchants); //will scale all the prices of the items that the merchants hold based on their dailyRandomPercent - if( (_currentDay % 3) == 0) //will increase the merchant's base cash every 3 days + if( (_currentDay % 6) == 0) //will increase the merchant's base cash every 6 days { - addAndRefreshCash(_merchant1, 100); - addAndRefreshCash(_merchant2, 100); - addAndRefreshCash(_merchant3, 100); + int multiplier = (int) Math.floor(_currentDay / 10) + 1; + + addAndRefreshCash(_merchant1, multiplier * 100); + addAndRefreshCash(_merchant2, multiplier * 100); + addAndRefreshCash(_merchant3, multiplier * 100); } else //if not on a third day, will just refresh the merchant's cash amount { @@ -251,6 +254,15 @@ public int getDay() { return _currentDay; } + + /** + * + * @return Movement variable + */ + public boolean getMovement() + { + return _movement; + } /** * Main method used to test the GUI components since test classes do not maintain the GUI @@ -265,7 +277,45 @@ public static void main(String[] args) throws InterruptedException playerInventory.addAll(_rpg.getMerchantInventoryList(2)); playerInventory.addAll(_rpg.getMerchantInventoryList(3)); _rpg.buildPlayer("test", 500, playerInventory); - _rpg.getPlayer().getItem("armor").increaseQuantity(15); - _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + + // SETTING UP PLAYER AND MERCHANT + Player player = new Player("Player", 0, null); + Merchant merch1 = new Merchant("Bill", 0, null); + Merchant merch2 = new Merchant("Joe", 0, null); + Merchant merch3 = new Merchant("Sam", 0, null); + + // CREATING MAP AND INITIALIZE PLAYER AND MERCHANTS + Map map = new Map(30, 40); + map.initializePlayer(player, 15, 20); + map.initializeMerchant(merch1, 15, 0); + map.initializeMerchant(merch2, 29, 20); + map.initializeMerchant(merch3, 15, 39); + + // CREATING JFRAME WINDOW + JFrame frame = new JFrame("Merchant RPG"); + + // CREATING MAPUI AND SPRITES + MapUI mapui = new MapUI(map,_rpg); + mapui.createPlayerSprite(15, 20); + mapui.addMerchantSprite(15, 0); + mapui.addMerchantSprite(29, 20); + mapui.addMerchantSprite(15, 39); + + // ADDING MAPUI TO JFRAME + frame.add(mapui); + + // SETTING PROPERTIES OF JFRAME + frame.setSize(RPGame.WIDTH, RPGame.HEIGHT); + frame.setResizable(false); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocationRelativeTo(null); + + // MAIN GAME LOOP + while (true) { + mapui.move(); + mapui.repaint(); + Thread.sleep(100/12); // Controls the speed of the game (currently 120 frames/second) + } } } diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 4e1d13f..e7b4fb9 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -12,11 +12,13 @@ public class Transaction private Player _player; private Merchant _targetMerchant; private TransactionUI _window; + private RPGame _game; - public Transaction(Player player, Merchant targetMerchant) + public Transaction(Player player, Merchant targetMerchant, RPGame game) { _player = player; _targetMerchant = targetMerchant; + _game = game; _window = new TransactionUI(this); _window.setVisible(true); } @@ -61,16 +63,6 @@ public boolean actionSell(String itemName, int amount) throws NotInInventoryExce return false; } - /** - * This method will push a true up to the class calling it - * incomplete method - * @return returns true - */ - public boolean actionCancel() - { - return true; - } - /** * Searches the player inventory for the item matching the item name * @param itemName name of the item @@ -91,6 +83,14 @@ public Item searchMerchantInventory(String itemName) return _targetMerchant.getItem(itemName); } + /** + * Toggles the RPGame movement back on + */ + public void endTransaction() + { + _game.toggleMovement("ON"); + } + /** * Returns the player in transaction * @return returns the player diff --git a/MerchantRPGCSE2102/src/images/testsprite.png b/MerchantRPGCSE2102/src/images/testsprite.png new file mode 100644 index 0000000..99d0222 Binary files /dev/null and b/MerchantRPGCSE2102/src/images/testsprite.png differ diff --git a/MerchantRPGCSE2102/src/model/Map.java b/MerchantRPGCSE2102/src/model/Map.java index bff4432..f410d11 100644 --- a/MerchantRPGCSE2102/src/model/Map.java +++ b/MerchantRPGCSE2102/src/model/Map.java @@ -6,6 +6,7 @@ public class Map { private Graph _mapGraph; // Graph representation of the map private int _rows, _cols; // The n*n dimension of the map + private int[] initialPlayerLocation = new int[2]; private Player _player; public Map(int rows, int cols) { @@ -22,7 +23,9 @@ public Map(int rows, int cols) { * @param column The column of the player's location */ public void initializePlayer(Player player, int row, int col) { - _player = player; + _player = player; + initialPlayerLocation[0] = row; + initialPlayerLocation[1] = col; int vertexNum = row*_cols + col; player.setCol(col); player.setRow(row); @@ -134,6 +137,11 @@ public boolean collisionTo(String direction) { return false; } + public void resetPlayerLocation() { + _player.setCol(initialPlayerLocation[1]); + _player.setRow(initialPlayerLocation[0]); + } + public Player getPlayer() { return _player; } diff --git a/MerchantRPGCSE2102/src/sprites/Animation.java b/MerchantRPGCSE2102/src/sprites/Animation.java new file mode 100644 index 0000000..bc64c9d --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Animation.java @@ -0,0 +1,104 @@ +package sprites; + +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; + + +public class Animation { + + private int frameCount; // Counts ticks for change + private int frameDelay; // frame delay 1-12 (You will have to play around with this) + private int currentFrame; // animations current frame + private int animationDirection; // animation direction (i.e counting forward or backward) + private int totalFrames; // total amount of frames for your animation + + private boolean stopped; // has animations stopped + + private List frames = new ArrayList(); // Arraylist of frames + + public Animation(BufferedImage[] frames, int frameDelay) { + this.frameDelay = frameDelay; + this.stopped = true; + + for (int i = 0; i < frames.length; i++) { + addFrame(frames[i], frameDelay); + } + + this.frameCount = 0; + this.frameDelay = frameDelay; + this.currentFrame = 0; + this.animationDirection = 1; + this.totalFrames = this.frames.size(); + + } + + public void start() { + if (!stopped) { + return; + } + + if (frames.size() == 0) { + return; + } + + stopped = false; + } + + public void stop() { + if (frames.size() == 0) { + return; + } + + stopped = true; + } + + public void restart() { + if (frames.size() == 0) { + return; + } + + stopped = false; + currentFrame = 0; + } + + public void reset() { + this.stopped = true; + this.frameCount = 0; + this.currentFrame = 0; + } + + private void addFrame(BufferedImage frame, int duration) { + if (duration <= 0) { + System.err.println("Invalid duration: " + duration); + throw new RuntimeException("Invalid duration: " + duration); + } + + frames.add(new Frame(frame, duration)); + currentFrame = 0; + } + + public BufferedImage getSprite() { + return frames.get(currentFrame).getFrame(); + } + + public void update() { + if (!stopped) { + frameCount++; + + if (frameCount > frameDelay) { + frameCount = 0; + currentFrame += animationDirection; + + if (currentFrame > totalFrames - 1) { + currentFrame = 0; + } + else if (currentFrame < 0) { + currentFrame = totalFrames - 1; + } + } + } + + } + +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/sprites/Frame.java b/MerchantRPGCSE2102/src/sprites/Frame.java new file mode 100644 index 0000000..f74ac07 --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Frame.java @@ -0,0 +1,31 @@ +package sprites; + +import java.awt.image.BufferedImage; + +public class Frame { + + private BufferedImage frame; + private int duration; + + public Frame(BufferedImage frame, int duration) { + this.frame = frame; + this.duration = duration; + } + + public BufferedImage getFrame() { + return frame; + } + + public void setFrame(BufferedImage frame) { + this.frame = frame; + } + + public int getDuration() { + return duration; + } + + public void setDuration(int duration) { + this.duration = duration; + } + +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 220e65a..081da07 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -4,6 +4,8 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; + import view.MapUI; public class PlayerSprite { @@ -15,16 +17,37 @@ public class PlayerSprite { private int dx, dy = 0; // Velocity of the sprite private int changeInX = 15; // Counts the number of pixels moved between vertices (in reference to the center of the sprite) private int changeInY = 15; + private int initialX; + private int initialY; // Keep track of which keys are currently being pressed private boolean leftBeingPressed = false; private boolean rightBeingPressed = false; private boolean downBeingPressed = false; private boolean upBeingPressed = false; + + // Images for each animation + private BufferedImage[] walkingLeft = {Sprite.getSprite(0, 1), Sprite.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet + private BufferedImage[] walkingRight = {Sprite.getSprite(0, 2), Sprite.getSprite(2, 1)}; + private BufferedImage[] walkingUp = {Sprite.getSprite(0, 1), Sprite.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet + private BufferedImage[] walkingDown = {Sprite.getSprite(0, 2), Sprite.getSprite(2, 1)}; + private BufferedImage[] standing = {Sprite.getSprite(1, 0)}; + + // Animation states + private Animation walkLeft = new Animation(walkingLeft, 10); + private Animation walkRight = new Animation(walkingRight, 10); + private Animation walkUp = new Animation(walkingUp, 10); + private Animation walkDown = new Animation(walkingDown, 10); + private Animation standing1 = new Animation(standing, 10); + + // Actual animation + private Animation animation = standing1; public PlayerSprite(MapUI mapui, int row, int col) { this.x = col*WIDTH; + initialX = x; this.y = row*WIDTH; + initialY = y; this.mapui = mapui; } @@ -35,19 +58,27 @@ public PlayerSprite(MapUI mapui, int row, int col) { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { dx = -1; + animation = walkLeft; + animation.start(); leftBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { dx = 1; + animation = walkRight; + animation.start(); rightBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_UP) { dy = -1; + animation = walkUp; + animation.start(); upBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { dy = 1; + animation = walkDown; + animation.start(); downBeingPressed = true; } } @@ -57,6 +88,12 @@ public void keyPressed(KeyEvent e) { * @param e The KeyEvent detected by the system */ public void keyReleased(KeyEvent e) { + + if(e.getKeyChar()=='n') { + mapui.nextDay(); + resetLocation(); + } + if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftBeingPressed = false; if (rightBeingPressed == false) // Sprite will stop only when the RIGHT key is not being pressed @@ -79,6 +116,14 @@ public void keyReleased(KeyEvent e) { if (upBeingPressed == false) // Sprite will stop only when the UP key is not being pressed dy = 0; } + if (e.getKeyCode() == KeyEvent.VK_F){ + if(collision()){ + mapui.game.createTransaction(null, null);//RPGame initialize Trade + } + else{ + return; + } + } // This fixes some bugs with holding three keys down at once and letting go of two (Basically ensures that dx and dy match which keys are currently being held) if(leftBeingPressed) @@ -163,14 +208,22 @@ public void move() { } } + + public void resetLocation() { + x = initialX; + y = initialY; + changeInX = 15; + changeInY = 15; + + } /** * Paints the sprite at its current location * @param g Graphics2D for painting */ public void paint(Graphics2D g) { - g.setColor(color); - g.fillOval(getX(), getY(), WIDTH, WIDTH); + + g.drawImage(animation.getSprite(), x, y, null); } /** diff --git a/MerchantRPGCSE2102/src/sprites/Sprite.java b/MerchantRPGCSE2102/src/sprites/Sprite.java new file mode 100644 index 0000000..81711fa --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Sprite.java @@ -0,0 +1,36 @@ +package sprites; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class Sprite { + + private static BufferedImage spriteSheet; + private static final int TILE_SIZE = 32; + + public static BufferedImage loadSprite(String file) { + + BufferedImage sprite = null; + + try { + sprite = ImageIO.read(new File("src/images/testsprite.png")); + } catch (IOException e) { + e.printStackTrace(); + } + + return sprite; + } + + public static BufferedImage getSprite(int xGrid, int yGrid) { + + if (spriteSheet == null) { + spriteSheet = loadSprite("AnimationSpriteSheet"); + } + + return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE); + } + +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/tests/MockGame.java b/MerchantRPGCSE2102/src/tests/MockGame.java index 1856265..f7962d8 100644 --- a/MerchantRPGCSE2102/src/tests/MockGame.java +++ b/MerchantRPGCSE2102/src/tests/MockGame.java @@ -6,27 +6,51 @@ import model.Player; import view.MapUI; import controller.RPGame; +import model.Merchant; +import model.Player; public class MockGame { - public static void main(String[] args) throws InterruptedException { - // This sets up the window for the game which is a 300 by 300 pixels JFrame - Player player = new Player("TestPlayer", 0, null); + public static void main(String[] args) throws InterruptedException { + RPGame rpg = new RPGame(); + // SETTING UP PLAYER AND MERCHANT + Player player = new Player("Player", 0, null); + Merchant merch1 = new Merchant("Bill", 0, null); + Merchant merch2 = new Merchant("Joe", 0, null); + Merchant merch3 = new Merchant("Sam", 0, null); + + // CREATING MAP AND INITIALIZE PLAYER AND MERCHANTS Map map = new Map(30, 40); - map.initializePlayer(player, 0, 0); + map.initializePlayer(player, 15, 20); + map.initializeMerchant(merch1, 15, 0); + map.initializeMerchant(merch2, 29, 20); + map.initializeMerchant(merch3, 15, 39); + + // CREATING JFRAME WINDOW JFrame frame = new JFrame("Merchant RPG"); - MapUI mapui = new MapUI(map); + + // CREATING MAPUI AND SPRITES + MapUI mapui = new MapUI(map, rpg); + mapui.createPlayerSprite(15, 20); + mapui.addMerchantSprite(15, 0); + mapui.addMerchantSprite(29, 20); + mapui.addMerchantSprite(15, 39); + + // ADDING MAPUI TO JFRAME frame.add(mapui); + + // SETTING PROPERTIES OF JFRAME frame.setSize(RPGame.WIDTH, RPGame.HEIGHT); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); + // MAIN GAME LOOP while (true) { mapui.move(); mapui.repaint(); - Thread.sleep(10); + Thread.sleep(100/12); // Controls the speed of the game (currently 120 frames/second) } } diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index 044fec6..eae961f 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -11,20 +11,27 @@ import javax.swing.JPanel; +import controller.RPGame; + import model.Map; import sprites.MerchantSprite; import sprites.PlayerSprite; +import controller.RPGame; @SuppressWarnings("serial") public class MapUI extends JPanel { - private Map map; + public RPGame game; + public Map map; private PlayerSprite player; - private ArrayList merchants = new ArrayList(); + private ArrayList merchants = new ArrayList(); + private boolean _tranState=game._movement; - public MapUI(Map map) { + public MapUI(Map map,RPGame Game) { this.map = map; + this.game= Game; + addKeyListener(new KeyListener() { @Override @@ -70,12 +77,13 @@ public void paint(Graphics g) { for (MerchantSprite merchant : merchants) merchant.paint(g2d); - // For testing purposes. Showes the Player's current x and y position as well as the current Vertex + // For testing purposes. Shows the Player's current x and y position as well as the current Vertex g2d.setColor(Color.GRAY); g2d.setFont(new Font("Verdana", Font.BOLD, 20)); g2d.drawString("x: " + player.getX() + " y : " + player.getY(), 10, 20); g2d.drawString("delta X: " + player.getChangeInX() +" delta Y: " + player.getChangeInY(), 10, 50); g2d.drawString("Vertex: (" + map.getPlayer().getRow() + ", " + map.getPlayer().getCol() + ")", 1000, 20); + } /** @@ -87,7 +95,7 @@ public void addMerchantSprite(int row, int col) { MerchantSprite merchant = new MerchantSprite(row, col); merchants.add(merchant); } - + /** * Creates a player sprite to be painted on the canvas * @param row The row of the player @@ -96,11 +104,22 @@ public void addMerchantSprite(int row, int col) { public void createPlayerSprite(int row, int col) { player = new PlayerSprite(this, row, col); } + + public void changeState(){ + + } /** * Moves the PlayerSprite as well as the Map's instance of Player */ public void move() { + //check transaction + if (_tranState=false){ + + } + else{ + + player.move(); // Every 30 pixels, the player will move to a new Vertex @@ -124,6 +143,16 @@ public void move() { map.movePlayer("south"); player.setChangeInY(1); } + } + } + + /** + * Calls the method for advancing the day in the instance of RPGame + * @return + */ + public void nextDay() { + game.advanceDailyCycle(); + map.resetPlayerLocation(); } public Map getMap() { diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 033be48..bfc722c 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -19,12 +19,14 @@ public class TransactionUI extends JFrame { private static JPanel transactionPanel; private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it - + private static boolean _isWarning; + /** * Create the frame. */ public TransactionUI(Transaction master) { MASTER = master; + _isWarning = false; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); @@ -42,8 +44,8 @@ public void actionPerformed(ActionEvent arg0) { public void mouseReleased(MouseEvent arg0) { System.out.println("BUY"); //temporary test code createBuyWindow(); - } - }); + } + }); btnBuy.setBounds(58, 155, 169, 105); transactionPanel.add(btnBuy); @@ -64,8 +66,8 @@ public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) { System.out.println("Cancel"); //temporary test code - MASTER.actionCancel(); - exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open + MASTER.endTransaction(); + exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open } }); btnCancel.setBounds(210, 310, 160, 50); @@ -121,7 +123,7 @@ public void createBuyWindow() @SuppressWarnings({ "rawtypes", "unchecked" }) final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from - comboBox.setBounds(61, 141, 131, 20); + comboBox.setBounds(61, 141, 250, 20); contentPane.add(comboBox); final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into @@ -141,10 +143,13 @@ public void createBuyWindow() btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + if(!_isWarning) + { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } } }); btnCancel.setBounds(61, 306, 89, 23); @@ -153,26 +158,29 @@ public void mouseReleased(MouseEvent arg0) { JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field btnAccept.addMouseListener(new MouseAdapter() { @Override - public void mouseReleased(MouseEvent e) { //information is consumed when player clicks the accept button - String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu - System.out.println(chosenItem); - - try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot - int chosenAmount = Integer.parseInt(textField.getText()); - if(MASTER.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item - { - System.out.println(chosenAmount); - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + public void mouseReleased(MouseEvent e) { + if(!_isWarning) + { + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot + int chosenAmount = Integer.parseInt(textField.getText()); + if(MASTER.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item + { + System.out.println(chosenAmount); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false + } + catch(NumberFormatException exception){ + System.out.println("Must input an amount that you can afford"); + createWarning("Must input an amount that you can afford"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false - } - catch(NumberFormatException exception){ - System.out.println("Must input an amount that you can afford"); - createWarning("Must input an amount that you can afford"); } } }); @@ -208,7 +216,7 @@ public void createSellWindow() contentPane.add(lblYouHave); final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from - comboBox.setBounds(61, 141, 131, 20); + comboBox.setBounds(61, 141, 250, 20); contentPane.add(comboBox); final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into @@ -228,10 +236,13 @@ public void createSellWindow() btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + if(!_isWarning) + { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } } }); btnCancel.setBounds(61, 306, 89, 23); @@ -241,40 +252,43 @@ public void mouseReleased(MouseEvent arg0) { btnAccept.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { //information is consumed when player clicks the accept button - String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu - System.out.println(chosenItem); - boolean isGood = false; + if(!_isWarning) + { + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + boolean isGood = false; - try{ try{ - try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot - int chosenAmount = Integer.parseInt(textField.getText()); - isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item - if(isGood) //will attempt to sell the specified amount of the chosen item - { - System.out.println(chosenAmount); - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + try{ + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot + int chosenAmount = Integer.parseInt(textField.getText()); + isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item + if(isGood) //will attempt to sell the specified amount of the chosen item + { + System.out.println(chosenAmount); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false + } + catch(MerchantNotEnoughCashException mnecexception){ + System.out.println("Merchant does not have enough cash"); + createWarning("Merchant does not have enough cash"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false } - catch(MerchantNotEnoughCashException mnecexception){ - System.out.println("Merchant does not have enough cash"); - createWarning("Merchant does not have enough cash"); + catch(NumberFormatException exception){ + System.out.println("You do not have enough of that item"); + createWarning("You do not have enough of that item"); } } - catch(NumberFormatException exception){ - System.out.println("You do not have enough of that item"); - createWarning("You do not have enough of that item"); + catch(NotInInventoryException niiexception){ + System.out.println("Merchant does not accept that item"); + createWarning("Merchant does not accept that item"); } } - catch(NotInInventoryException niiexception){ - System.out.println("Merchant does not accept that item"); - createWarning("Merchant does not accept that item"); - } } }); btnAccept.setBounds(247, 306, 89, 23); @@ -282,8 +296,9 @@ public void mouseReleased(MouseEvent e) { //information is consumed // frame.setVisible(true); // frame.setLocationRelativeTo(null); } - + public static void createWarning(String message) { + _isWarning = true; final JFrame contentFrame = new JFrame(); JPanel warningPane = new JPanel(); contentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -297,6 +312,7 @@ public static void createWarning(String message) { btnOk.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { + _isWarning = false; contentFrame.dispose(); } });