//Author: ^-^ Veerle ^-^ //Extends the painting/layout class, this is used for interacting with the user's mouse clicks import java.awt.*; import java.awt.event.*; public class spiderSolitareWindow extends spiderSolitarePaintAndLayout implements MouseListener, MouseMotionListener { private final int refreshRate = 5; private int refreshCounter = 0; private boolean updateDrag = false; public spiderSolitareWindow () { this.addMouseListener(this); this.addMouseMotionListener(this); } public void mouseClicked (MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && !hasWon) { int x = e.getX(); int y = e.getY(); if (menuSystem.checkMenuClicked(x,y)) //Check if the menu or menu items were clicked { performMenuAction(); //if so, perform the action of the menu item clicked } else if (cardDeck.clickedDeck(x,y)) //Check if the deck area was clicked { if (cardDeck.successfullDeckDeal(cardColumn)) //Tests if a deck can be dealed (columns must not be empty, last cards must be face up) { testColumnDeck(); //Tests if a full deck has been made (e.g. if an ace was dealed onto a column already with King to two) Very rare but could happen repaint(); //Repaints the whole screen, a lot easier then working out the heights for each column } } else //Check each column to see if a face down card can be flipped { int column = columnMouseIsOver(x,y); //Gets the column the mouse is over (-1 if not over a column) if (column != -1) { if (cardColumn[column].canFlipCard(cardColumn[column].cardMouseIsOver(y))) //Tests if the card that was clicked can be flipped { currentInstruction.setInstruction(cardColumn[column].getPaintInstruction()); //Repaint the flipped card only repaint(); } } } } } private int columnMouseIsOver (int x, int y) { //If the mouse is anywhere between the first card column and the last if (x >= gapColumn && x <= ((71 + gapColumn) * 10) && y >= 50) { //Cycle through each column, seeing if it was the one the press occured in for (int column = 0; column < 10; column++) { if (cardColumn[column].isMouseOverColumn(x,y)) //Returns true if the mouse is over any card in the column { return column; } } } return -1; } private void testWin () //Tests if all the columns and deck are empty { boolean wonGame = true; for (int column = 0; column < 10; column++) { if (cardColumn[column].size() != 0) { wonGame = false; break; } } if (cardDeck.size() != 0) { wonGame = false; } if (wonGame) { hasWon = true; } } private void testColumnDeck () //Tests if a full deck (same colour with rank king - ace) has been found, and if so will remove that stack { boolean shouldTestWin = false; //If at least one deck was found, test if all decks have been found for (int column = 0; column < 10; column++) { if (cardColumn[column].testFullDeck()) { shouldTestWin = true; currentInstruction.reset(); } } if (shouldTestWin) { testWin(); } } public void mouseEntered (MouseEvent e) { } public void mouseExited (MouseEvent e) //If dragging and leave the window { mouseReleased(e); } public void mousePressed (MouseEvent e) { //Must be left mouse button, and the game must not have already been won if (e.getButton() == MouseEvent.BUTTON1 && !hasWon) { //Gets the current x and y coordinates of the mouse cursor int x = e.getX(); int y = e.getY(); int column = columnMouseIsOver(x,y); //Checks which column the mouse is over (returns -1 if none) if (column != -1) { int dragCard = cardColumn[column].cardMouseIsOver(y); //Gets the card index that you are dragging if (cardColumn[column].canBeDragged(dragCard)) //Tests the card can be dragged { cardDrag.setOrgColumn(column); //Records the original column and drag card so it can be restored cardDrag.setDragStartCard(dragCard); updateDrag = true; //Buffer, if you start dragging then it will remove the stack } } } } public void mouseReleased (MouseEvent e) { isDragging = false; updateDrag = false; if (cardDrag.size() != 0) //If dragging something { int x = e.getX(); int y = e.getY(); boolean badMove = true; int column = columnMouseIsOver(x,y); if (column != -1) { if (cardColumn[column].canBeDropped(cardDrag.getCard(0))) //Tests if dropping the stack would be legal { int previousStackSize = cardColumn[column].size(); cardColumn[column].addStack(cardDrag.removeStack(0)); //If it is legal, drop the stack badMove = false; testColumnDeck(); //Check if a deck can be removed } } if (badMove) //If drop is ilegal { cardColumn[cardDrag.getOrgColumn()].addStack(cardDrag.removeStack(0)); //Restores the original column's stack } repaint(); //Repaints the changes } } public void mouseDragged (MouseEvent e) { int x = e.getX(); int y = e.getY(); if (updateDrag) { int column = cardDrag.getOrgColumn(); int dragCard = cardDrag.getDragStartCard(); isDragging = true; updateDrag = false; refreshCounter = 0; cardDrag.setStartInstruction(cardColumn[column].getDragStartInstruction(dragCard)); cardDrag.addStack(cardColumn[column].removeStack(dragCard)); cardDrag.setMouseCurrentPosition(x,y); repaint(); } if (isDragging) { if (refreshCounter > refreshRate) { refreshCounter = 0; cardDrag.setMouseCurrentPosition(x,y); repaint(); } else { refreshCounter++; } } } public void mouseMoved (MouseEvent e) { if (menuSystem.checkMouseMove(e.getX(),e.getY())) { repaint(); } } }