Ellie29

MenuFrame

Mar 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. package lab_03;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.nio.file.DirectoryIteratorException;
  10. import java.nio.file.DirectoryStream;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.nio.file.Paths;
  14. import java.util.Vector;
  15.  
  16. import javax.swing.DefaultListModel;
  17. import javax.swing.JButton;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JFormattedTextField;
  20. import javax.swing.JFrame;
  21. import javax.swing.JLabel;
  22. import javax.swing.JList;
  23. import javax.swing.JPanel;
  24. import javax.swing.ListSelectionModel;
  25. import javax.swing.event.ListSelectionEvent;
  26. import javax.swing.event.ListSelectionListener;
  27.  
  28.  
  29. public class MenuFrame extends JFrame implements ActionListener,ListSelectionListener{
  30.  
  31.     Board board;
  32.     int sizeOfBoard = 5;
  33.     int winner = 5;
  34.     BoardFrame boardGUI;
  35.     String classesPathString;
  36.     String pathName;
  37.     Paths classesPath;
  38.    
  39.     private static final long serialVersionUID = 1L;
  40.  
  41.     JFormattedTextField sizeArea;
  42.     File file;
  43.     Path dir;
  44.    
  45.     JPanel panel = new JPanel();
  46.     JButton exitButton = new JButton("Exit");
  47.     JButton playButton = new JButton("Play");
  48.     JButton sizeButton = new JButton("Set Size");
  49.     JButton catalogButton = new JButton("Choose Catalog");
  50.     JButton strategyButton = new JButton("Load Strategies");
  51.     JButton levelButton = new JButton("Set Level");
  52.     JButton sampleButton = new JButton("X");
  53.    
  54.     JLabel sizeLabel = new JLabel("Size of Board");
  55.     JLabel strategyLabel = new JLabel("Strategy:");
  56.     JLabel levelLabel = new JLabel("Dificculty Level:");
  57.     JFileChooser fc = new JFileChooser();
  58.    
  59.     JList<String> loadedStratedyList = new JList<String>();
  60.     DefaultListModel<String> listStrategyModel = new DefaultListModel<>();
  61.    
  62.     JList<String> loadedLevelList = new JList<String>();
  63.     DefaultListModel<String> listLevelModel = new DefaultListModel<>();
  64.    
  65.     Vector<Loader> vectorClass = new Vector<Loader>();
  66.     Vector<String> levelVector = new Vector<String>();
  67.     Vector<String> strategyVector = new Vector<String>();
  68.    
  69.     public MenuFrame() {
  70.        
  71.          fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  72.         sizeArea = new JFormattedTextField(sizeOfBoard);
  73.        
  74.         sizeArea.setEditable(true);
  75.         sizeArea.setSize(new Dimension(2,4));
  76.         //JScrollPane sizeScrollPane = new JScrollPane(sizeArea);
  77.        
  78.          setContentPane(panel);
  79.        
  80.        
  81.        
  82.         loadedStratedyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  83.         loadedStratedyList.addListSelectionListener(this);
  84.         loadedStratedyList.setDragEnabled(true);
  85.         loadedStratedyList = new JList<>(listStrategyModel);
  86.        
  87.         exitButton.addActionListener(this);
  88.         playButton.addActionListener(this);
  89.         catalogButton.addActionListener(this);
  90.         sizeButton.addActionListener(this);
  91.         strategyButton.addActionListener(this);
  92.         levelButton.addActionListener(this);
  93.        
  94.         panel.add(sizeLabel);
  95.         panel.add(sizeArea);
  96.         //panel.add(sizeScrollPane);
  97.         panel.add(sizeButton);
  98.         panel.add(catalogButton);
  99.         panel.add(strategyLabel);
  100.         panel.add(loadedStratedyList);
  101.         panel.add(strategyButton);
  102.         panel.add(levelLabel);
  103.         panel.add(levelButton);
  104.         panel.add(playButton);
  105.         panel.add(exitButton);
  106.        
  107.        
  108.         setLayout(new FlowLayout(FlowLayout.CENTER));
  109.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  110.         setSize(600,650);
  111.         this.setVisible(true);
  112.        
  113.     }
  114.  
  115.      public static void main(String[] args) {
  116.            
  117.                 new MenuFrame();
  118.            
  119.         }
  120.    
  121.    
  122.     @Override
  123.     public void actionPerformed(ActionEvent e) {
  124.         Object source = e.getSource();
  125.         if(source == sizeButton) {
  126.             sizeOfBoard = (int) sizeArea.getValue();
  127.         }
  128.         if(source == catalogButton) {
  129.            
  130.             int returnVal = fc.showOpenDialog(MenuFrame.this);
  131.            
  132.             if (returnVal == JFileChooser.APPROVE_OPTION) {
  133.                 file = fc.getSelectedFile();
  134.          
  135.                 classesPathString = file.getAbsolutePath();
  136.                 pathName = file.getPath();
  137.                 dir = file.toPath();
  138.                
  139.                 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
  140.                     //Wybór wszystkich podkatalogów danego katalogu
  141.                     for (Path filePath: stream) {
  142.                         listStrategyModel.addElement(filePath.getFileName().toString());
  143.                         strategyVector.addElement(filePath.getFileName().toString());
  144.                     }
  145.                    
  146.                    
  147.                 } catch (IOException | DirectoryIteratorException x) {
  148.                    
  149.                 }
  150.              
  151.             } else {
  152.             }
  153.         }
  154.         if(source == strategyButton){
  155.             for(int i = 0; i < strategyVector.size(); i++) {
  156.                 System.out.println(strategyVector.get(i));
  157.             }
  158.         //System.out.println("Rozmiar" + sizeOfBoard);
  159.         }
  160.         if(source == levelButton) {
  161.             System.out.println(pathName);
  162.             System.out.println(classesPathString);
  163.         }
  164.         if(source == playButton) {
  165.             board = new Board(sizeOfBoard);
  166.             boardGUI = new BoardFrame(sizeOfBoard,strategyVector,levelVector);
  167.         }
  168.        
  169.         if(source == exitButton) {
  170.             System.exit(0);
  171.         }
  172.        
  173.        
  174.     }
  175.  
  176.     @Override
  177.     public void valueChanged(ListSelectionEvent e) {
  178.         // TODO Auto-generated method stub
  179.        
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment