diff --git a/MerchantRPGCSE2102/src/model/Character.java b/MerchantRPGCSE2102/src/model/Character.java index f1dcd57..37a63ab 100644 --- a/MerchantRPGCSE2102/src/model/Character.java +++ b/MerchantRPGCSE2102/src/model/Character.java @@ -1,7 +1,7 @@ package model; public class Character { - private int _x, _y; + private int col, row; protected Item[] _inventory; protected String _name; @@ -43,20 +43,20 @@ public String getName() return _name; } - public int getX() { - return _x; + public int getCol() { + return col; } - public void setX(int x) { - _x = x; + public void setCol(int x) { + col = x; } - public int getY() { - return _y; + public int getRow() { + return row; } - public void setY(int y) { - _y = y; + public void setRow(int y) { + row = y; } diff --git a/MerchantRPGCSE2102/src/model/Map.java b/MerchantRPGCSE2102/src/model/Map.java index 78ae804..bff4432 100644 --- a/MerchantRPGCSE2102/src/model/Map.java +++ b/MerchantRPGCSE2102/src/model/Map.java @@ -16,29 +16,29 @@ public Map(int rows, int cols) { } /** - * This method stores the sepcified instance of Player at the specified position on the map + * This method stores the specified instance of Player at the specified position on the map * @param player The player to be initialized - * @param x The x coordinate of the player's location - * @param y The y coordinate of the player's location + * @param row The row of the player's location + * @param column The column of the player's location */ - public void initializePlayer(Player player, int x, int y) { + public void initializePlayer(Player player, int row, int col) { _player = player; - int vertexNum = y*_cols + x; - player.setX(x); - player.setY(y); + int vertexNum = row*_cols + col; + player.setCol(col); + player.setRow(row); _mapGraph.getVertex(vertexNum).setOccupant(player); } /** * This method stores the sepcified instance of Merchant at the specified position on the map * @param player The merchant to be initialized - * @param x The x coordinate of the merchant's location - * @param y The y coordinate of the merchant's location + * @param row The row of the merchant's location + * @param col The column of the merchant's location */ - public void initializeMerchant(Merchant merchant, int x, int y) { - int vertexNum = y*_cols + x; - merchant.setX(x); - merchant.setY(y); + public void initializeMerchant(Merchant merchant, int row, int col) { + int vertexNum = row*_cols + col; + merchant.setCol(col); + merchant.setRow(row); _mapGraph.getVertex(vertexNum).setOccupant(merchant); } @@ -47,49 +47,49 @@ public void initializeMerchant(Merchant merchant, int x, int y) { * @param String The direction to be moved (north, west, east, or south) */ public void movePlayer(String direction) { - int currentX = _player.getX(); - int currentY = _player.getY(); + int currentCol = _player.getCol(); + int currentRow = _player.getRow(); if (direction.equals("north")) { - if (currentY != 0) { - if (!isOccupied(currentX, currentY - 1)) { - _mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null); - _mapGraph.getVertex((currentY - 1)*_cols + currentX).setOccupant(_player); - _player.setY(currentY - 1); + if (currentRow != 0) { + if (!isOccupied(currentCol, currentRow - 1)) { + _mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null); + _mapGraph.getVertex((currentRow - 1)*_cols + currentCol).setOccupant(_player); + _player.setRow(currentRow - 1); } } } if (direction.equals("east")) { - if (currentX != _cols - 1) { - if (!isOccupied(currentX + 1, currentY)) { - _mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null); - _mapGraph.getVertex(currentY*_cols + (currentX + 1)).setOccupant(_player); - _player.setX(currentX + 1); + if (currentCol != _cols - 1) { + if (!isOccupied(currentCol + 1, currentRow)) { + _mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null); + _mapGraph.getVertex(currentRow*_cols + (currentCol + 1)).setOccupant(_player); + _player.setCol(currentCol + 1); } } } if (direction.equals("south")) { - if (currentY != _rows - 1) { - if (!isOccupied(currentX, currentY + 1)) { - _mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null); - _mapGraph.getVertex((currentY + 1)*_cols + currentX).setOccupant(_player); - _player.setY(currentY + 1); + if (currentRow != _rows - 1) { + if (!isOccupied(currentCol, currentRow + 1)) { + _mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null); + _mapGraph.getVertex((currentRow + 1)*_cols + currentCol).setOccupant(_player); + _player.setRow(currentRow + 1); } } } if (direction.equals("west")) { - if (currentX != 0) { - if (!isOccupied(currentX - 1, currentY)) { - _mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null); - _mapGraph.getVertex(currentY*_cols + (currentX - 1)).setOccupant(_player); - _player.setX(currentX - 1); + if (currentCol != 0) { + if (!isOccupied(currentCol - 1, currentRow)) { + _mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null); + _mapGraph.getVertex(currentRow*_cols + (currentCol - 1)).setOccupant(_player); + _player.setCol(currentCol - 1); } } } - System.out.println("Player is at vertex (" + _player.getX() + ", " + _player.getY() + ")"); + } /** @@ -102,12 +102,40 @@ public Graph getGraph() { /** * Checks if a location on the map is occupied by a merchant or player - * @param x x coordinate to check - * @param y y coordinate to check + * @param x column to check + * @param y row to check * @return Boolean value */ - public boolean isOccupied(int x, int y) { - return _mapGraph.getVertex(y*_cols + x).getOccupant() != null; + public boolean isOccupied(int col, int row) { + if (row < 0 || col < 0 || row >= _rows || col >= _cols) + return false; + else + return _mapGraph.getVertex(row*_cols + col).getOccupant() != null; + } + + public boolean collisionTo(String direction) { + + if (direction.equals("north")) + return isOccupied(_player.getCol(), _player.getRow() - 1); + if (direction.equals("south")) + return isOccupied(_player.getCol(), _player.getRow() + 1); + if (direction.equals("east")) + return isOccupied(_player.getCol() + 1, _player.getRow()); + if (direction.equals("west")) + return isOccupied(_player.getCol() - 1, _player.getRow()); + if (direction.equals("northwest")) + return isOccupied(_player.getCol() - 1, _player.getRow() - 1); + if (direction.equals("southwest")) + return isOccupied(_player.getCol() - 1, _player.getRow() + 1); + if (direction.equals("northeast")) + return isOccupied(_player.getCol() + 1, _player.getRow() - 1); + if (direction.equals("southeast")) + return isOccupied(_player.getCol() + 1, _player.getRow() + 1); + return false; + } + + public Player getPlayer() { + return _player; } } diff --git a/MerchantRPGCSE2102/src/sprites/MerchantSprite.java b/MerchantRPGCSE2102/src/sprites/MerchantSprite.java new file mode 100644 index 0000000..ba6414b --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/MerchantSprite.java @@ -0,0 +1,30 @@ +package sprites; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +public class MerchantSprite { + private int x, y; + private static final int WIDTH = 30; + + public MerchantSprite(int row, int col) { + this.x = col*WIDTH; + this.y = row*WIDTH; + } + + /** + * Paints the sprite at its current location + * @param g Graphics2D for painting + */ + public void paint(Graphics2D g) { + g.fillRect(x, y, WIDTH, WIDTH); + } + + /** + * Gets the bounds of the sprite in the form of a Rectangle + * @return the Rectangle formed by the sprite + */ + public Rectangle getBounds() { + return new Rectangle(x, y, WIDTH, WIDTH); + } +} diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 9b1f91f..220e65a 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -2,28 +2,36 @@ import java.awt.Color; import java.awt.Graphics2D; +import java.awt.Rectangle; import java.awt.event.KeyEvent; - import view.MapUI; public class PlayerSprite { - private static final int WIDTH = 30; - MapUI mapui; - public int x, y = 0; - public int row, col; - int dx, dy = 0; - public int changeInX = 0; - public int changeInY = 0; - Color color = new Color(0, 0, 0); - boolean leftBeingPressed = false; - boolean rightBeingPressed = false; - boolean downBeingPressed = false; - boolean upBeingPressed = false; - - public PlayerSprite(MapUI mapui) { + MapUI mapui; // Instance of MapUI which created this PlayerSprite + private static final int WIDTH = 30; // Width of the PlayerSprite + private Color color = new Color(0, 0, 0); // Color of the PlayerSprite (this will no longer be needed once we have a sprite sheet) + + private int x, y; // x and y coordinates (in pixels) + 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; + + // 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; + + public PlayerSprite(MapUI mapui, int row, int col) { + this.x = col*WIDTH; + this.y = row*WIDTH; this.mapui = mapui; } - + + /** + * Changes the sprite's velocity based on which key is being pressed + * @param e The KeyEvent detected by the system + */ public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { dx = -1; @@ -33,6 +41,7 @@ public void keyPressed(KeyEvent e) { dx = 1; rightBeingPressed = true; } + if (e.getKeyCode() == KeyEvent.VK_UP) { dy = -1; upBeingPressed = true; @@ -42,32 +51,35 @@ public void keyPressed(KeyEvent e) { downBeingPressed = true; } } - + + /** + * Changes the sprite's velocity based on which key is released + * @param e The KeyEvent detected by the system + */ public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftBeingPressed = false; - if (rightBeingPressed == false) + if (rightBeingPressed == false) // Sprite will stop only when the RIGHT key is not being pressed dx = 0; } - + if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightBeingPressed = false; - if (leftBeingPressed == false) + if (leftBeingPressed == false) // Sprite will stop only when the LEFT key is not being pressed dx = 0; } - if (e.getKeyCode() == KeyEvent.VK_UP) { upBeingPressed = false; - if (downBeingPressed == false) + if (downBeingPressed == false) // Sprite will stop only when the DOWN key is not being pressed dy = 0; } - + if (e.getKeyCode() == KeyEvent.VK_DOWN) { downBeingPressed = false; - if (upBeingPressed == false) + if (upBeingPressed == false) // Sprite will stop only when the UP key is not being pressed dy = 0; } - + // 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) dx = -1; @@ -77,22 +89,139 @@ public void keyReleased(KeyEvent e) { dy = 1; if(upBeingPressed) dy = -1; - + } + /** + * Moves the sprite using the current velocity and detects for collisions + */ public void move() { - if (x + dx >= 0 && x + dx <= mapui.getWidth() - WIDTH && y + dy >= 0 && y + dy <= mapui.getHeight() - WIDTH) { - x = x + dx; - changeInX = changeInX + dx; - y = y + dy; - changeInY = changeInY + dy; + if (collision()) { + + // If there is a collision, we check from which direction the collision occurs. During each collision the sprite is stopped and their position is reset so they are no longer collided. + if (mapui.getMap().collisionTo("east")) { + dx = 0; + setChangeInX(getChangeInX() - 1); + setX(getX() - 1); + } + if (mapui.getMap().collisionTo("west")) { + dx = 0; + setChangeInX(getChangeInX() + 1); + setX(getX() + 1); + } + if (mapui.getMap().collisionTo("north")) { + dy = 0; + setChangeInY(getChangeInY() + 1); + setY(getY() + 1); + } + if (mapui.getMap().collisionTo("south")) { + dy = 0; + setChangeInY(getChangeInY() - 1); + setY(getY() - 1); + } + if (mapui.getMap().collisionTo("northwest")) { + dy = 0; + dx = 0; + setChangeInY(getChangeInY() + 1); + setChangeInX(getChangeInX() + 1); + setY(getY() + 1); + setX(getX() + 1); + } + if (mapui.getMap().collisionTo("southwest")) { + dy = 0; + dx = 0; + setChangeInY(getChangeInY() - 1); + setChangeInX(getChangeInX() + 1); + setY(getY() - 1); + setX(getX() + 1); + } + if (mapui.getMap().collisionTo("northeast")) { + dy = 0; + dx = 0; + setChangeInY(getChangeInY() + 1); + setChangeInX(getChangeInX() - 1); + setY(getY() + 1); + setX(getX() - 1); + } + if (mapui.getMap().collisionTo("southeast")) { + dy = 0; + dx = 0; + setChangeInY(getChangeInY() - 1); + setChangeInX(getChangeInX() - 1); + setY(getY() - 1); + setX(getX() - 1); + } + } - + + // Checks that the sprite is moving within the bounds of the panel + if (getX() + dx >= 0 && getX() + dx <= mapui.getWidth() - WIDTH && getY() + dy >= 0 && getY() + dy <= mapui.getHeight() - WIDTH) { + setX(getX() + dx); + setChangeInX(getChangeInX() + dx); + setY(getY() + dy); + setChangeInY(getChangeInY() + dy); + } + } - + + /** + * Paints the sprite at its current location + * @param g Graphics2D for painting + */ public void paint(Graphics2D g) { g.setColor(color); - g.fillOval(x, y, WIDTH, WIDTH); + g.fillOval(getX(), getY(), WIDTH, WIDTH); + } + + /** + * Gets the bounds of the sprite in the form of a Rectangle + * @return the Rectangle formed by the sprite + */ + public Rectangle getBounds() { + return new Rectangle(getX(), getY(), WIDTH, WIDTH); + } + + /** + * Detects a collision between the PlayerSprite and any of the Merchantsprites + * @return + */ + public boolean collision() { + for (MerchantSprite merchant : mapui.getMerchants()) + if (merchant.getBounds().intersects(getBounds())) + return true; + return false; + } + + public int getChangeInX() { + return changeInX; + } + + public void setChangeInX(int changeInX) { + this.changeInX = changeInX; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getChangeInY() { + return changeInY; + } + + public void setChangeInY(int changeInY) { + this.changeInY = changeInY; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; } } diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index 68294ac..044fec6 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -7,17 +7,20 @@ import java.awt.RenderingHints; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.util.ArrayList; import javax.swing.JPanel; import model.Map; +import sprites.MerchantSprite; import sprites.PlayerSprite; @SuppressWarnings("serial") public class MapUI extends JPanel { - PlayerSprite player = new PlayerSprite(this); - Map map; + private Map map; + private PlayerSprite player; + private ArrayList merchants = new ArrayList(); public MapUI(Map map) { @@ -48,36 +51,88 @@ public void keyTyped(KeyEvent e) { @Override public void paint(Graphics g) { - super.paint(g); + super.paint(g); // repaints the canvas + Graphics2D g2d = (Graphics2D) g; - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Prevents aliasing + + // For testing purposes. Creates a grid of lines on the canvas + for (int i = 0; i < map.getGraph().getCols(); i++) { + for (int j = 0; j < map.getGraph().getRows(); j++) { + g2d.fillRect(i*30, j*30, 1, 30); + g2d.fillRect(i*30, j*30, 30, 1); + } + } + + // Paints the player onto the canvas player.paint(g2d); + // Paints each merchant onto the canvas + 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 g2d.setColor(Color.GRAY); g2d.setFont(new Font("Verdana", Font.BOLD, 20)); - g2d.drawString("x: " + player.x + " y : " + player.y, 10, 20); - g2d.drawString("delta X: " + player.changeInX +" delta Y: " + player.changeInY, 10, 50); + 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); + } + + /** + * Adds a MerchantSprite to the list of MerchantSprite's to be painted on the canvas + * @param row The row of the merchant + * @param col The column of the merchant + */ + 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 + * @param y The column of the player + */ + public void createPlayerSprite(int row, int col) { + player = new PlayerSprite(this, row, col); } + /** + * Moves the PlayerSprite as well as the Map's instance of Player + */ public void move() { player.move(); - if (player.changeInX == 30) { + // Every 30 pixels, the player will move to a new Vertex + // I used the values 0, 1, 30, 31 to establish a border between crossing Vertices + // For example, if the player reaches a changeInX of 31, this means they are moving to a Vertex in the east direction and their changeInX is reset to 1 + // if they then decide to move west, they will move to the next Vertex when their changeInX reaches 0. + // Thus, the border shared by two vertices is inbetween 0 and 1 as well as 30 and 31. + if (player.getChangeInX() == 31) { map.movePlayer("east"); - player.changeInX = 0; + player.setChangeInX(1); } - if (player.changeInX == -30) { + if (player.getChangeInX() == 0) { map.movePlayer("west"); - player.changeInX = 0; + player.setChangeInX(30); } - if (player.changeInY == -30) { + if (player.getChangeInY() == 0) { map.movePlayer("north"); - player.changeInY = 0; + player.setChangeInY(30); } - if (player.changeInY == 30) { + if (player.getChangeInY() == 31) { map.movePlayer("south"); - player.changeInY = 0; + player.setChangeInY(1); } } + + public Map getMap() { + return map; + } + + public ArrayList getMerchants() { + return merchants; + } } diff --git a/MerchantRPGCSE2102/src/view/StartScreen.java b/MerchantRPGCSE2102/src/view/StartScreen.java index d7e8e4a..4d900dd 100644 --- a/MerchantRPGCSE2102/src/view/StartScreen.java +++ b/MerchantRPGCSE2102/src/view/StartScreen.java @@ -53,8 +53,8 @@ public void actionPerformed(ActionEvent e){ start.setBackground(null); instructions.setBackground(null); this.add(panel); - this.add(instructions); - this.add(start); + this.getContentPane().add(instructions); + this.getContentPane().add(start); } public BufferedImage loadIcon() throws IOException { diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 48181fb..033be48 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -1,19 +1,9 @@ package view; - -import java.awt.EventQueue; -import java.awt.FlowLayout; - -import javax.swing.JComboBox; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JRootPane; -import javax.swing.border.EmptyBorder; -import javax.swing.JButton; -import javax.swing.JLabel; +import javax.swing.*; import java.awt.Font; -import javax.swing.JTextField; +import javax.swing.border.EmptyBorder; import controller.Transaction; import exceptions.MerchantNotEnoughCashException; @@ -27,39 +17,20 @@ @SuppressWarnings("serial") public class TransactionUI extends JFrame { - private static JPanel contentPane; + private static JPanel transactionPanel; private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it - private static boolean _inTransaction; //Prevents the user from making multiple transaction windows - - /** - * Launch the application. - */ - public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - TransactionUI frame = new TransactionUI(MASTER); - frame.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - } - }); - } - + /** * Create the frame. */ public TransactionUI(Transaction master) { MASTER = master; - _inTransaction = false; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); - contentPane = new JPanel(); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - setContentPane(contentPane); - contentPane.setLayout(null); + transactionPanel = new JPanel(); + transactionPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + transactionPanel.setLayout(null); JButton btnBuy = new JButton("BUY"); //creates a "Buy" button btnBuy.addActionListener(new ActionListener() { @@ -68,51 +39,49 @@ public void actionPerformed(ActionEvent arg0) { }); btnBuy.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent arg0) { + public void mouseReleased(MouseEvent arg0) { System.out.println("BUY"); //temporary test code - if(!_inTransaction) //checks if there is another transaction window open if not, then creates a buy transaction window - createBuyWindow(); - } - }); + createBuyWindow(); + } + }); btnBuy.setBounds(58, 155, 169, 105); - contentPane.add(btnBuy); + transactionPanel.add(btnBuy); JButton btnSell = new JButton("SELL"); //creates a "Sell" button btnSell.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent e) { + public void mouseReleased(MouseEvent e) { System.out.println("SELL"); //temporary test code - if(!_inTransaction) //checks if there is another transaction window open, if not creates a sell transaction window - createSellWindow(); + createSellWindow(); } }); btnSell.setBounds(351, 155, 169, 105); - contentPane.add(btnSell); + transactionPanel.add(btnSell); JButton btnCancel = new JButton("End Transaction"); //creates a button to end the transaction btnCancel.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent e) { + public void mouseReleased(MouseEvent e) { System.out.println("Cancel"); //temporary test code - if(!_inTransaction) - { + MASTER.actionCancel(); exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open - } } }); btnCancel.setBounds(210, 310, 160, 50); - contentPane.add(btnCancel); + transactionPanel.add(btnCancel); JLabel lblWouldYouLike = new JLabel("Would you like to:"); lblWouldYouLike.setFont(new Font("Tahoma", Font.PLAIN, 15)); lblWouldYouLike.setBounds(233, 76, 193, 32); - contentPane.add(lblWouldYouLike); + transactionPanel.add(lblWouldYouLike); JLabel lblOr = new JLabel("OR"); lblOr.setFont(new Font("Tahoma", Font.PLAIN, 15)); lblOr.setBounds(277, 189, 35, 32); - contentPane.add(lblOr); + transactionPanel.add(lblOr); + add(transactionPanel); + setLocationRelativeTo(null); } /** @@ -128,19 +97,16 @@ public void exitWindow() * Will create a window for the BUY option * INCOMPLETE METHOD */ - public static void createBuyWindow() + public void createBuyWindow() { - _inTransaction = true; //does not allow new transaction windows to be opened while buy window is - final JFrame frame = new JFrame("Buy"); - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 400, 400); - frame.setUndecorated(true); - frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE); JPanel contentPane = new JPanel(); - contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - frame.setContentPane(contentPane); contentPane.setLayout(null); + getContentPane().removeAll(); + add(contentPane); + setVisible(false); + setVisible(true); + setVisible(true); String[] itemList = new String[MASTER.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list @@ -174,9 +140,11 @@ public static void createBuyWindow() JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent arg0) { - _inTransaction = false; - frame.dispose(); + public void mouseReleased(MouseEvent arg0) { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); } }); btnCancel.setBounds(61, 306, 89, 23); @@ -185,7 +153,7 @@ public void mouseClicked(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 mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + 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); @@ -194,8 +162,10 @@ public void mouseClicked(MouseEvent e) { //information is consumed whe if(MASTER.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item { System.out.println(chosenAmount); - _inTransaction = false; - frame.dispose(); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); } else throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false @@ -208,7 +178,6 @@ public void mouseClicked(MouseEvent e) { //information is consumed whe }); btnAccept.setBounds(247, 306, 89, 23); contentPane.add(btnAccept); - frame.setVisible(true); } /** @@ -216,19 +185,15 @@ public void mouseClicked(MouseEvent e) { //information is consumed whe * INCOMPLETE METHOD: Still needs to integrate with the transaction limit in the daily cycle */ @SuppressWarnings({ "unchecked", "rawtypes" }) - public static void createSellWindow() + public void createSellWindow() { - _inTransaction = true; //does not allow new transaction windows to be opened if sell window is - final JFrame frame = new JFrame("Sell"); - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 400, 400); - frame.setUndecorated(true); - frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE); JPanel contentPane = new JPanel(); - contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - frame.setContentPane(contentPane); contentPane.setLayout(null); + getContentPane().removeAll(); + add(contentPane); + setVisible(false); + setVisible(true); String[] itemList = new String[MASTER.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list @@ -262,9 +227,11 @@ public static void createSellWindow() JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent arg0) { - _inTransaction = false; - frame.dispose(); + public void mouseReleased(MouseEvent arg0) { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); } }); btnCancel.setBounds(61, 306, 89, 23); @@ -273,7 +240,7 @@ public void mouseClicked(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 mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + 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; @@ -286,8 +253,10 @@ public void mouseClicked(MouseEvent e) { //information is consumed w if(isGood) //will attempt to sell the specified amount of the chosen item { System.out.println(chosenAmount); - _inTransaction = false; - frame.dispose(); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); } else throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false @@ -310,32 +279,33 @@ public void mouseClicked(MouseEvent e) { //information is consumed w }); btnAccept.setBounds(247, 306, 89, 23); contentPane.add(btnAccept); - frame.setVisible(true); + // frame.setVisible(true); + // frame.setLocationRelativeTo(null); } public static void createWarning(String message) { final JFrame contentFrame = new JFrame(); + JPanel warningPane = new JPanel(); contentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); contentFrame.setBounds(100, 100, 450, 300); - contentPane = new JPanel(); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - contentFrame.setContentPane(contentPane); - contentPane.setLayout(null); - + warningPane = new JPanel(); + warningPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + contentFrame.setContentPane(warningPane); + warningPane.setLayout(null); + JButton btnOk = new JButton("Ok"); btnOk.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent arg0) { - _inTransaction = false; + public void mouseReleased(MouseEvent arg0) { contentFrame.dispose(); } }); btnOk.setBounds(162, 203, 103, 32); - contentPane.add(btnOk); - + warningPane.add(btnOk); + JLabel lblNewLabel = new JLabel(message); lblNewLabel.setBounds(80, 102, 265, 49); - contentPane.add(lblNewLabel); + warningPane.add(lblNewLabel); contentFrame.setVisible(true); } }