Directory

← Back

PlayingCards

All you need for creating a card game with Vaadin

Author

Rating

Popularity

<100

PlayingCards contain Card, Deck, CardStack and CardPile widgets for creating card games using Vaadin:

  • Card represents a single playing card that can be front or back side up. To avoid cheating the card information is not sent to the browser if the back side is up.

  • Deck contains a collection of cards and the top card can be drawn.

  • CardStack contains 0..N cards that are ordered below each other so the value of all cards (that are not backside up) can be seen.

  • A CardPile represents a pile of cards on top of each other. Can also be empty. Nothing but the top card can every be shown.

Typically you want to place the cards inside an AbsoluteLayout so they can overlap each other. See the code fragments below for examples.

Includes one size cards (150x104). The cards are from the SVG-cards project by David Bellot.

Sample code

package org.vaadin.artur.playingcards.app;

import org.vaadin.artur.playingcards.Card;
import org.vaadin.artur.playingcards.CardPile;
import org.vaadin.artur.playingcards.CardStack;
import org.vaadin.artur.playingcards.Deck;
import org.vaadin.artur.playingcards.client.ui.Suite;

import com.vaadin.Application;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.event.MouseEvents.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window;

public class TestCards extends Application {

    private Deck deck;
    private CardStack stack;

    @Override
    public void init() {
        setMainWindow(new Window("Cards test", new HorizontalLayout()));

        deck = new Deck();
        deck.addListener(new ClickListener() {

            @Override
            public void click(ClickEvent event) {
                if (!deck.isEmpty())
                    stack.addCard(deck.removeTopCard());

            }
        });
        getMainWindow().addComponent(deck);

        Card c1 = new Card(Suite.SPADES, 12);
        getMainWindow().addComponent(c1);

        stack = new CardStack();
        stack.addListener(new LayoutClickListener() {

            @Override
            public void layoutClick(LayoutClickEvent event) {
                Card c = (Card) event.getChildComponent();
                if (c != null) {
                    c.setSelected(!c.isSelected());
                }

            }
        });
        stack.addCard(deck.removeTopCard());
        getMainWindow().addComponent(stack);

        CardPile pile = new CardPile();
        pile.addCard(deck.removeTopCard());

        getMainWindow().addComponent(pile);
    }
}
        AbsoluteLayout layout = new AbsoluteLayout();
        layout.setWidth("500px");
        layout.setHeight("500px");
        Card card = new Card(Suite.DIAMONDS, 12);
        layout.addComponent(card,"top: 20px; left: 30px");
        
        getMainWindow().addComponent(layout);

package org.vaadin.artur.playingcards.app;

import org.vaadin.artur.playingcards.Card;
import org.vaadin.artur.playingcards.CardPile;
import org.vaadin.artur.playingcards.CardStack;
import org.vaadin.artur.playingcards.Deck;
import org.vaadin.artur.playingcards.client.ui.Suite;

import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.event.MouseEvents.ClickListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.UI;

public class TestCards extends UI {

	private Deck deck;
	private CardStack stack;

	@Override
	protected void init(VaadinRequest request) {
		HorizontalLayout layout = new HorizontalLayout();
		setContent(layout);

		deck = new Deck();
		deck.addListener(new ClickListener() {

			@Override
			public void click(ClickEvent event) {
				if (!deck.isEmpty())
					stack.addCard(deck.removeTopCard());

			}
		});
		layout.addComponent(deck);

		Card c1 = new Card(Suite.SPADES, 12);
		layout.addComponent(c1);

		stack = new CardStack();
		stack.addListener(new LayoutClickListener() {

			@Override
			public void layoutClick(LayoutClickEvent event) {
				Card c = (Card) event.getChildComponent();
				if (c != null) {
					c.setSelected(!c.isSelected());
				}

			}
		});
		stack.addCard(deck.removeTopCard());
		layout.addComponent(stack);

		CardPile pile = new CardPile();
		pile.addCard(deck.removeTopCard());

		layout.addComponent(pile);
	}
}
		AbsoluteLayout layout = new AbsoluteLayout();
		layout.setWidth("500px");
		layout.setHeight("500px");
		Card card = new Card(Suite.DIAMONDS, 12);
		layout.addComponent(card, "top: 20px; left: 30px");

		setContent(layout);

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Compatible with Vaadin 8

Released
2017-04-02
Maturity
BETA
License
GNU General Public License v3.0 only

Compatibility

Framework
Vaadin 8.0+
Vaadin 7.0+ in 0.8.1
Vaadin 6.6+ in 0.4.0
Vaadin 6.3+ in 0.3.2
Browser
Firefox
Opera
Safari
Google Chrome
Internet Explorer
Microsoft Edge

PlayingCards - Vaadin Add-on Directory

All you need for creating a card game with Vaadin PlayingCards - Vaadin Add-on Directory
PlayingCards contain Card, Deck, CardStack and CardPile widgets for creating card games using Vaadin: * Card represents a single playing card that can be front or back side up. To avoid cheating the card information is not sent to the browser if the back side is up. * Deck contains a collection of cards and the top card can be drawn. * CardStack contains 0..N cards that are ordered below each other so the value of all cards (that are not backside up) can be seen. * A CardPile represents a pile of cards on top of each other. Can also be empty. Nothing but the top card can every be shown. Typically you want to place the cards inside an AbsoluteLayout so they can overlap each other. See the code fragments below for examples. Includes one size cards (150x104). The cards are from the SVG-cards project by David Bellot.
Online