Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.knugi;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JLabel;
- import javax.swing.JSeparator;
- import javax.swing.SwingConstants;
- import javax.swing.JButton;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import javax.swing.JRadioButton;
- import javax.swing.ButtonGroup;
- import javax.swing.JTextArea;
- import javax.swing.JToggleButton;
- public class AppStart{
- private JFrame frame;
- private JPanel panel;
- private JTextArea textArea;
- private final ButtonGroup buttonGroup = new ButtonGroup();
- private boolean isStarted = false;
- private List<Integer> lista;
- private Random random;
- private PanelAction pAction;
- public static JToggleButton[][] table;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- AppStart window = new AppStart();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public AppStart() {
- initialize();
- pAction = new PanelAction(this);
- random = new Random();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 1024, 800);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuBar menuBar = new JMenuBar();
- frame.setJMenuBar(menuBar);
- JMenu mnPlik = new JMenu("Plik");
- menuBar.add(mnPlik);
- JMenuItem mntmNowaGra = new JMenuItem("Nowa gra");
- mnPlik.add(mntmNowaGra);
- JMenuItem mntmWyjscie = new JMenuItem("Wyjscie");
- mnPlik.add(mntmWyjscie);
- JMenu mnPomoc = new JMenu("Pomoc");
- menuBar.add(mnPomoc);
- JMenuItem mntmAutor = new JMenuItem("Autor");
- mnPomoc.add(mntmAutor);
- frame.getContentPane().setLayout(null);
- JSeparator separator = new JSeparator();
- separator.setOrientation(SwingConstants.VERTICAL);
- separator.setBounds(199, 0, 6, 740);
- frame.getContentPane().add(separator);
- panel = new JPanel();
- panel.setBounds(200, 0, 808, 740);
- frame.getContentPane().add(panel);
- panel.setLayout(null);
- JRadioButton rdbtnx_1 = new JRadioButton("4x4");
- rdbtnx_1.setSelected(true);
- rdbtnx_1.setActionCommand(rdbtnx_1.getText());
- buttonGroup.add(rdbtnx_1);
- rdbtnx_1.setBounds(10, 67, 109, 23);
- frame.getContentPane().add(rdbtnx_1);
- JRadioButton rdbtnx_2 = new JRadioButton("6x6");
- rdbtnx_2.setActionCommand(rdbtnx_2.getText());
- buttonGroup.add(rdbtnx_2);
- rdbtnx_2.setBounds(10, 93, 109, 23);
- frame.getContentPane().add(rdbtnx_2);
- JRadioButton rdbtnx_3 = new JRadioButton("8x8");
- rdbtnx_3.setActionCommand(rdbtnx_3.getText());
- buttonGroup.add(rdbtnx_3);
- rdbtnx_3.setBounds(10, 119, 109, 23);
- frame.getContentPane().add(rdbtnx_3);
- JButton btnNowaGra = new JButton("Nowa gra");
- btnNowaGra.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(!isStarted){
- isStarted=true;
- clearPanel();
- int a = Integer.parseInt(buttonGroup.getSelection().getActionCommand().substring(0, 1));
- textArea.setText(a+"");
- table = new JToggleButton[a][a];
- int x = 0;
- int y = 0;
- for(int i = 0; i<a; i++){
- for(int j=0;j<a;j++){
- table[i][j] = (JToggleButton) panel.add(new JToggleButton());
- table[i][j].setActionCommand(i+" "+j);
- table[i][j].setBounds(x, y, 65, 65);
- table[i][j].addActionListener(pAction);
- x+=70;
- }
- x=0;
- y+=70;
- }
- lista = new ArrayList<Integer>();
- int g=1;
- for(int i=0;i<(a*a)/2;i++){
- lista.add(g);
- lista.add(g);
- g++;
- }
- for(int i=0;i<a;i++){
- for(int j=0;j<a;j++){
- table[i][j].setName(getRandomFromList()+"");
- table[i][j].setText(table[i][j].getName());
- }
- }
- panel.validate();
- panel.repaint();
- return;
- }
- }
- });
- btnNowaGra.setBounds(10, 11, 89, 23);
- frame.getContentPane().add(btnNowaGra);
- JSeparator separator_1 = new JSeparator();
- separator_1.setBounds(0, 145, 200, 1);
- frame.getContentPane().add(separator_1);
- textArea = new JTextArea();
- textArea.setEditable(false);
- textArea.setBounds(10, 149, 184, 44);
- frame.getContentPane().add(textArea);
- JLabel label = new JLabel("");
- label.setBounds(0, 0, 1008, 740);
- frame.getContentPane().add(label);
- }
- public void setAreaText(String s){
- this.textArea.setText(s);
- }
- public void clearPanel(){
- this.frame.getContentPane().remove(panel);
- panel = new JPanel();
- panel.setBounds(200, 0, 808, 740);
- frame.getContentPane().add(panel);
- panel.setLayout(null);
- panel.validate();
- panel.repaint();
- }
- private Integer getRandomFromList(){
- int index = random.nextInt(lista.size());
- int ret = lista.get(index);
- lista.remove(index);
- return ret;
- }
- public boolean checkWin(){
- if(isStarted){
- for(int i=0;i<table.length;i++){
- for(int j=0;j<table.length;j++){
- if(table[i][j].isEnabled()){
- return false;
- }
- }
- }
- isStarted=false;
- table=null;
- clearPanel();
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment