Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.4 kB
1
Indexable
Never
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import java.util.Random;
import javax.swing.JLabel;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MyFrame extends JFrame implements ActionListener {
	
	Integer counter = new Integer(0);
    Integer diamonds = new Integer(0);
    Integer boom = new Integer(0);
	
	MyFrame () {
		System.out.println("Hi");
		
		
		this.setTitle("Bomb czy Diament?! Gra: Sebastian, 20 prób.");
		this.setLayout(new GridLayout(2,2,10,10));
		
		
		JButton button = new JButton("Kliknij tutaj");
		JButton button2 = new JButton("A może tutaj?!");
//		
//		JTextArea textArea = new JTextArea(5, 20);
		JLabel textLabel = new JLabel("Próba: " + counter + " Liczba diamentów: " + diamonds);
		JTextArea textArea2 = new JTextArea(5, 20);
	
		this.add(button);
		this.add(button2);
//		
//		textArea.setLineWrap(true);
		textArea2.setLineWrap(true);
		
		this.add(textLabel);
		this.add(textArea2);
		
		
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				randomNumberProcess(textLabel, textArea2);
			} });
		
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				randomNumberProcess(textLabel, textArea2);
			} });
		
		this.setSize(400,400);
		this.setVisible(true);
	}
	

	public void randomNumberProcess(JLabel textLabel, JTextArea textArea2) {
		counter += 1;
		boolean isWin = getRandomNumberHandler(1);
		
		if(counter == 20) {
			counter = 0;
			diamonds = 0;
			boom = 0;
		}
		
		if(isWin) {
			diamonds += 1;
			textArea2.setText("Szczęście uśmiechnęło się do Ciebie! Wygrałeś diament !!");
		}else {
			boom += 1;
			textArea2.setText("Masz dzisiaj pecha :( Bomba!");
		}
		
		textLabel.setText("Próba: " + counter + " Liczba diamentów: " + diamonds);
	}
	
	public boolean getRandomNumberHandler(int selectedValue) {
		 int randomNumber = getRandomNumber(0, 2);
		 if(randomNumber == 0 && selectedValue == 0) {
			 return true;
		 }
		 if(randomNumber == 1 && selectedValue == 1) {
			 return true;
		 }
		 return false;
 	}
	

 	public void actionPerformed(ActionEvent arg0) {
		System.out.println("Button - actionPerformed");
 	}
 	
 	public int getRandomNumber(int min, int max) {
 	    return (int) ((Math.random() * (max - min)) + min);
 	}
	

;
}