Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.border.Border;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Random;
- public class test extends JFrame implements ActionListener {
- JTextField text = new JTextField("Unten");
- LotteryButton[] fields = new LotteryButton[49];
- int counter = 0;
- int [] auswahl = new int [6];
- JButton bplay = new JButton("Play");
- public test() {
- super("Titel");
- this.setSize(400, 400);
- this.setLocation(100, 100);
- this.setLayout(new BorderLayout());
- JPanel center = new JPanel();
- center.setLayout(new GridLayout(7, 7, 10, 10));
- this.add(center, BorderLayout.CENTER);
- for (int i = 0; i < 49; i++) {
- LotteryButton lb = new LotteryButton(i + 1);
- fields[i] = lb;
- center.add(lb);
- lb.addActionListener(this);
- }
- this.add(bplay, BorderLayout.NORTH);
- bplay.addActionListener(this);
- text.setEditable(false);
- this.add(text, BorderLayout.SOUTH);
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- private int playLottery(int [] check) {
- int [] ziehung = new int[6];
- int gleiche = 0;
- for(int i = 0; i < 6; i++) {
- Random rand = new Random();
- int zahl = rand.nextInt(49) + 1;
- for(int k = 0; k <= i; k++) {
- if(ziehung[k] == zahl) {
- zahl = rand.nextInt(49) + 1;
- k = -1;
- }
- }
- ziehung[i] = zahl;
- System.out.print(ziehung[i] + " ");
- }
- System.out.println();
- for(int j = 0; j < 6; j++) {
- for(int k = 0; k < 6; k++) {
- if(check[j] == ziehung[k]) {
- gleiche++;
- }
- }
- }
- text.setText("You have " + gleiche + " matches with the 6 winning numbers!");
- return gleiche;
- }
- private class LotteryButton extends JButton {
- private int number;
- private LotteryButton(int number) {
- super("" + number);
- this.number = number;
- }
- public int getNumber() {
- return this.number;
- }
- }
- public void actionPerformed(ActionEvent e) {
- for(int i = 0; i < 49; i++) {
- if(e.getSource().equals(fields[i])) {
- if(fields[i].getText() == "X") {
- fields[i].setText("" + fields[i].getNumber());
- fields[i].setForeground(Color.BLACK);
- counter--;
- text.setText(fields[i].getNumber() + " unselected.");
- for(int j = 0; j < 6; j++) {
- if(auswahl[j] == fields[i].getNumber()) {
- auswahl[j] = 0;
- }
- }
- } else {
- if(counter == 6) {
- text.setText("You have already selected 6 fields!");
- } else {
- text.setText(fields[i].getNumber() + " selected.");
- int k = 0;
- while(auswahl[k] != 0) {
- k++;
- }
- auswahl[k] = fields[i].getNumber();
- fields[i].setText("X");
- fields[i].setForeground(Color.BLUE);
- counter++;
- }
- }
- }
- }
- if(e.getSource().equals(bplay)) {
- if(counter != 6) {
- text.setText("Please select 6 fields!");
- } else {
- playLottery(auswahl);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement