Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.44 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package maze;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics2D;
  10. import java.awt.Image;
  11. import java.awt.image.BufferedImage;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileReader;
  16. import java.io.IOException;
  17. import java.util.StringTokenizer;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JFileChooser;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24.  
  25. /**
  26.  *
  27.  * @author Krzysztof
  28.  */
  29. public class JFrame extends javax.swing.JFrame {
  30.  
  31.     /**
  32.      * Creates new form JFrame
  33.      */
  34.     public JFrame() {
  35.         initComponents();
  36.     }
  37.  
  38.     /**
  39.      * This method is called from within the constructor to initialize the form.
  40.      * WARNING: Do NOT modify this code. The content of this method is always
  41.      * regenerated by the Form Editor.
  42.      */
  43.     @SuppressWarnings("unchecked")
  44.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  45.     private void initComponents() {
  46.  
  47.         imageLabel = new javax.swing.JLabel();
  48.         insertButton = new javax.swing.JButton();
  49.         solveButton = new javax.swing.JButton();
  50.  
  51.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  52.         setTitle("Maze solver");
  53.         setBackground(new java.awt.Color(153, 153, 153));
  54.         setResizable(false);
  55.  
  56.         imageLabel.setBackground(new java.awt.Color(102, 102, 102));
  57.  
  58.         insertButton.setText("Wczytaj");
  59.         insertButton.addActionListener(new java.awt.event.ActionListener() {
  60.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  61.                 insertButtonActionPerformed(evt);
  62.             }
  63.         });
  64.  
  65.         solveButton.setText("Rozwiąż");
  66.         solveButton.setEnabled(file != null);
  67.         solveButton.addComponentListener(new java.awt.event.ComponentAdapter() {
  68.             public void componentHidden(java.awt.event.ComponentEvent evt) {
  69.                 solveButtonComponentHidden(evt);
  70.             }
  71.         });
  72.  
  73.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  74.         getContentPane().setLayout(layout);
  75.         layout.setHorizontalGroup(
  76.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  77.             .addGroup(layout.createSequentialGroup()
  78.                 .addContainerGap(45, Short.MAX_VALUE)
  79.                 .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
  80.                 .addGap(32, 32, 32)
  81.                 .addComponent(solveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
  82.                 .addGap(48, 48, 48))
  83.             .addGroup(layout.createSequentialGroup()
  84.                 .addContainerGap()
  85.                 .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  86.                 .addContainerGap())
  87.         );
  88.         layout.setVerticalGroup(
  89.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  90.             .addGroup(layout.createSequentialGroup()
  91.                 .addContainerGap()
  92.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  93.                     .addComponent(insertButton)
  94.                     .addComponent(solveButton))
  95.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  96.                 .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
  97.                 .addContainerGap())
  98.         );
  99.  
  100.         pack();
  101.         setLocationRelativeTo(null);
  102.     }// </editor-fold>                        
  103. File file;
  104.     private void solveButtonComponentHidden(java.awt.event.ComponentEvent evt) {                                            
  105.      
  106.     }                                          
  107.  
  108.     private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  109.        
  110.         javax.swing.UIManager.put( "FileChooser.CancelButtonText", "Anuluj" );
  111.         javax.swing.UIManager.put( "OpenButtonText", "Otwórz" );
  112.        
  113.         javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
  114.         javax.swing.filechooser.FileNameExtensionFilter filter = new javax.swing.filechooser.FileNameExtensionFilter("Plik tekstowy", "txt");
  115.         fileChooser.setFileFilter(filter);
  116.        
  117.         int retValue = fileChooser.showOpenDialog(this);
  118.        
  119.         if ( retValue == JFileChooser.APPROVE_OPTION ){
  120.            
  121.             file = fileChooser.getSelectedFile();          
  122.  
  123.         }
  124.        
  125.         try {
  126.             drawMaze();
  127.         } catch (IOException ex) {
  128.            
  129.         }
  130.     }                                            
  131.  
  132.     /**
  133.      * @param args the command line arguments
  134.      */
  135.     public static void main(String args[]) {
  136.         /* Set the Nimbus look and feel */
  137.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  138.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  139.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  140.          */
  141.         try {
  142.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  143.                 if ("Nimbus".equals(info.getName())) {
  144.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  145.                     break;
  146.                 }
  147.             }
  148.         } catch (ClassNotFoundException ex) {
  149.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  150.         } catch (InstantiationException ex) {
  151.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  152.         } catch (IllegalAccessException ex) {
  153.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  154.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  155.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  156.         }
  157.         //</editor-fold>
  158.  
  159.         /* Create and display the form */
  160.         java.awt.EventQueue.invokeLater(new Runnable() {
  161.             public void run() {
  162.                 new JFrame().setVisible(true);
  163.             }
  164.         });
  165.     }
  166.  
  167.     // Variables declaration - do not modify                    
  168.     private javax.swing.JLabel imageLabel;
  169.     private javax.swing.JButton insertButton;
  170.     private javax.swing.JButton solveButton;
  171.     // End of variables declaration                  
  172.  
  173.     private int getMazeSize( File file ) throws FileNotFoundException, IOException {
  174.      
  175.            
  176.         BufferedReader txt = new BufferedReader( new FileReader( file ) );                      
  177.         String line;
  178.         String size = "";
  179.  
  180.         while( ( line = txt.readLine() ) != null ){
  181.            
  182.             StringTokenizer t = new StringTokenizer(line, " ");
  183.             size  = t.nextToken();
  184.                        
  185.         }
  186.        
  187.         return Integer.parseInt(size);
  188.     }
  189.  
  190.     private void drawMaze() throws IOException {
  191.        BufferedReader txt = new BufferedReader( new FileReader( file ) );  
  192.        String line;
  193.        int size = getMazeSize( file ) + 1;
  194.        BufferedImage img = new BufferedImage( size, size , BufferedImage.TYPE_INT_RGB);
  195.        Graphics2D graphics2d = (Graphics2D) img.getGraphics();
  196.        
  197.        
  198.         while( ( line = txt.readLine() ) != null ){
  199.            
  200.             StringTokenizer t = new StringTokenizer(line, " ");
  201.            
  202.             int x = Integer.parseInt(t.nextToken());
  203.             int y = Integer.parseInt(t.nextToken());
  204.            
  205.            if ( t.hasMoreTokens()){
  206.             String operation = t.nextToken();
  207.            
  208.            switch (operation) {
  209.                case "PD":
  210.                    graphics2d.drawLine(x, y, x + 1, y);
  211.                    graphics2d.drawLine(x, y, x, y + 1);
  212.                    break;
  213.                case "P":
  214.                    graphics2d.drawLine(x, y, x + 1, y);
  215.                    break;
  216.                case "D":
  217.                    graphics2d.drawLine(x, y, x, y + 1);
  218.                    break;
  219.                default:
  220.                    JFrame error = new JFrame();
  221.                    JOptionPane.showMessageDialog( error , "Incorrect input file" );                                                                    
  222.                    break;
  223.            }
  224.           }
  225.         }
  226.         showImageInJLabel( img, imageLabel);
  227.     }
  228.    
  229.     private void showImageInJLabel(BufferedImage bufferedImage, JLabel imageLabel) {
  230.            
  231.             int labelWidth = imageLabel.getWidth();
  232.             int labelHeight = imageLabel.getHeight();
  233.            
  234.             Image imageTemp = bufferedImage.getScaledInstance(labelWidth, labelHeight, java.awt.Image.SCALE_SMOOTH);
  235.             ImageIcon imageIcon = new ImageIcon(imageTemp);
  236.             imageLabel.setIcon(imageIcon);
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement