Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Brief demo
/**
* Created by christopher on 08/07/14.
*/
//Necessary imports
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TrialClass extends JPanel {
private static final long serialVersionUID = 1L;
//Knappvariabler
private static String buttonString = "Slumpa Kort";
private JButton btnRefresh;
/*
* SlumpaKort-konstruktor
*/
public TrialClass() {
//Skapa en JPanel
super(new BorderLayout());
//Skapa knappen
btnRefresh = new JButton(buttonString);
btnRefresh.setActionCommand(buttonString);
btnRefresh.addActionListener(new Clicked());
//Skapa en panel som knappen kan ligga på
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(btnRefresh);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//Lägg till allt ovan till fönstret
add(buttonPane, BorderLayout.PAGE_END);
}
/*
* Klick-Event för knappen
*/
class Clicked implements ActionListener {
class Kort {
private String[] farg = {"Hjärter", "Spader", "Ruter", "Klöver"};
private String[] valor = {"Ess", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Knäckt", "Dam", "Kung"};
//* Visar vilka färger samt valörer som finns representerade i kortleken *//
public int f, v;
public Kort(int farg, int valor) {
if (farg < 1 || farg > 4 ||
//* Fyra fäger *//
valor < 1 || valor > 13) {
//* 13 valörer *//
}
f = farg;
v = valor;
}
public int geFarg() {
return f;
}
public int geValor() {
return v;
}
public String toString() {
return farg[f - 1] + " " + valor[v - 1];
}
}
public class Kortlek {
public Kort[] bunt = new Kort[52];
public int antal = 0;
public int geAntal() {
return antal;
}
public void laggOverst(Kort k) {
bunt[antal] = k;
antal++;
}
public Kort geOversta() {
antal--;
return bunt[antal];
}
public void görKortlek() {
for (int f = 1; f <= 4; f++) {
for (int v = 1; v <= 13; v++) {
laggOverst(new Kort(f, v));
}
}
}
public void blanda() {
for (int i = 0; i < antal; i++) {
int n = i + (int) (Math.random() * (antal - i));
Kort temp = bunt[n];
bunt[n] = bunt[i];
bunt[i] = temp;
}
}
public void main(String args[]) {
//* Blanda kortlek *//
Kortlek kortlek = new Kortlek();
kortlek.görKortlek();
kortlek.blanda();
//*Loopar kortlek.geOversta fem gånger för att sedan dela ut fem slumpade kort*//
for (int i = 0; i < 5; i++) {
Kort k = kortlek.geOversta();
System.out.println(k.toString());
}
}
}
public void actionPerformed(ActionEvent e) {
//Vad som ska hända när man klickar på knappen
JOptionPane.showMessageDialog(null, "Detta fungerar!");
}
}
/*
* Startpunkten för programmet.
* Här skapas fönstret.
*/
public static void main(String[] args) {
//Skapa formen
JFrame frame = new JFrame("Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
//Skapa TestButton
JComponent contentPane = new TrialClass();
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
//Visa fönstret
frame.pack();
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.