Permalink
Browse files

Merge branch 'master' of github.com:AlexSzlavik/LostCities

  • Loading branch information...
2 parents b253194 + e1168f9 commit e04eb10a8f519190f4d5a3fed9490498d7f9c47a Alex Szlavik committed Mar 14, 2012
Showing with 224 additions and 1 deletion.
  1. +166 −0 LostCities/Card.java
  2. +44 −0 LostCities/Deck.java
  3. +14 −1 LostCities/Makefile
View
@@ -0,0 +1,166 @@
+import java.awt.Color;
+import java.util.Observable;
+import java.util.Observer;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.event.ComponentListener;
+import java.awt.event.FocusListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.util.Comparator;
+
+import javax.imageio.ImageIO;
+import javax.swing.BorderFactory;
+import javax.swing.ImageIcon;
+import javax.swing.JComponent;
+import javax.swing.JLabel;
+import javax.swing.border.BevelBorder;
+import javax.swing.border.EmptyBorder;
+
+/*
+ * Card Abstract Class
+ * Used to Derive specific cards
+ * All cards are components, as they may be displayed
+ * at one point or another
+ */
+public abstract class Card extends JLabel implements Comparable<Card>, Observer {
+ //Type definitions
+ private String cName;
+ public enum Type {Troop,Tactic};
+
+ protected Type type;
+ private boolean selected;
+
+ //Images
+ private static BufferedImage [] back_image = null;
+ private Image front_image;
+
+ //Constructor
+ public Card(Type myType) {
+ //System.out.println("Created Card");
+ this.type = myType;
+ this.cName = (myType == Type.Tactic)?"Tactic Card":"Troop Card";
+ this.selected = false;
+
+ //Load the back image pictures once if they haven't been allocated yet
+ if (back_image == null) {
+ back_image = new BufferedImage[2]; //Two types of back images
+ try {
+ back_image[0] = ImageIO.read(getClass().getResource("Images/troop_back.jpg"));
+ back_image[1] = ImageIO.read(getClass().getResource("Images/troop_back.jpg"));
+ }catch (IOException e) {
+ System.err.println("Couldn't load back images");
+ System.err.println(e);
+ System.exit(ERROR);
+ }
+ }
+
+ double ratio = (double)back_image[0].getWidth() / (double)back_image[0].getHeight();
+ int newHeight = 150;
+ int newWidth = (int)(newHeight * ratio);
+
+ this.addMouseListener(new CardActions(this));
+ this.setIcon(new ImageIcon(back_image[0].getScaledInstance(newWidth, newHeight,BufferedImage.SCALE_FAST)));
+ this.setBorder(BorderFactory.createLineBorder(this.getBackground(), 4));
+ }//End Constructor
+
+ protected abstract void getCardImage(String filename);
+
+ public String getName() {
+ return this.cName;
+ }
+
+ public boolean isSelected() {
+ return selected;
+ }
+
+ public void setName(String name){
+ this.cName = name;
+ }
+
+ public String toString() {
+ return this.cName;
+ }
+
+ /*
+ * General Card comperison.
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compareTo(Card b) {
+ //This should compare this card to card b
+ return 0;
+ }//compareTo
+
+
+ /* (non-Javadoc)
+ * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
+ */
+ @Override
+ public void update(Observable o, Object arg) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void toogleHightlight() {
+ selected = selected ^ true;
+ if (selected) {
+ this.setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));
+ }else{
+ this.setBorder(BorderFactory.createLineBorder(getBackground(), 4));
+ }
+ }
+
+ ///////////////
+ //Interactivity
+ ///////////////
+ class CardActions implements MouseListener {
+
+ private Card data;
+
+ public CardActions(Card data){
+ this.data = data;
+ }
+
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ System.out.println("This is Card: " + data);
+ data.getParent().dispatchEvent(e);
+ }
+
+ @Override
+ public void mouseEntered(MouseEvent e) {
+ // TODO Auto-generated method stub
+ if(data.isSelected())
+ this.data.setBorder(BorderFactory.createLineBorder(Color.RED, 4));
+ else
+ this.data.setBorder(BorderFactory.createLineBorder(Color.black, 4));
+ }
+
+ @Override
+ public void mouseExited(MouseEvent e) {
+ // TODO Auto-generated method stub
+ if(!data.isSelected())
+ this.data.setBorder(BorderFactory.createLineBorder(data.getBackground(), 4));
+ else
+ this.data.setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));
+ }
+
+ @Override
+ public void mousePressed(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mouseReleased(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ }
+}
View
@@ -0,0 +1,44 @@
+import java.util.LinkedList;
+import java.util.Random;
+
+/*
+ * Define a Deck, which manages the cards
+ * within a deck.
+ */
+public class Deck {
+ protected LinkedList<Card> cards; //Array of cards
+ private Random rnd = new Random();
+
+ public Deck() {
+ cards = new LinkedList<Card>();
+ }//Deck
+
+ /*
+ * Remove the top of the list and return it
+ */
+ public Card drawCard() {
+ return cards.pop();
+ }
+
+ public String toString() {
+ return "Deck Size: " + cards.size();
+ }
+
+ /*
+ * Shuffle function, randomize card deck
+ */
+ public void shuffle() {
+ int index = 0;
+ if (cards.size() < 2)
+ return;
+
+ int new_index = rnd.nextInt(cards.size()-1);
+ for(int it=cards.size() - 1; it > 0; it--) {
+ index = rnd.nextInt(it);
+ cards.add(index, cards.get(it)); //Insert duplicate
+ Card a = cards.remove(index+1); //Cards got shifted, remove the original index location
+ cards.add(it, a);
+ cards.remove(it+1); //Remove the duplicate
+ }
+ }
+}
View
@@ -1 +1,14 @@
-#Makefile for non-Eclipse based compilation
+#Makefile for non-Eclipse based compilation
+
+FILES=$(wildcard *.java)
+
+all: $(patsubst %.java,%.class, $(FILES))
+
+.PHONY:
+ echo $(FILES)
+
+%.class: %.java
+ javac $<
+
+clean:
+ -rm *.class

0 comments on commit e04eb10

Please sign in to comment.