Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package maze;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.StringTokenizer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.ImageIcon;
- import javax.swing.JFileChooser;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- /**
- *
- * @author Krzysztof
- */
- public class JFrame extends javax.swing.JFrame {
- /**
- * Creates new form JFrame
- */
- public JFrame() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- imageLabel = new javax.swing.JLabel();
- insertButton = new javax.swing.JButton();
- solveButton = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Maze solver");
- setBackground(new java.awt.Color(153, 153, 153));
- setResizable(false);
- imageLabel.setBackground(new java.awt.Color(102, 102, 102));
- insertButton.setText("Wczytaj");
- insertButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- insertButtonActionPerformed(evt);
- }
- });
- solveButton.setText("Rozwiąż");
- solveButton.setEnabled(file != null);
- solveButton.addComponentListener(new java.awt.event.ComponentAdapter() {
- public void componentHidden(java.awt.event.ComponentEvent evt) {
- solveButtonComponentHidden(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap(45, Short.MAX_VALUE)
- .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(32, 32, 32)
- .addComponent(solveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(48, 48, 48))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(insertButton)
- .addComponent(solveButton))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>
- File file;
- private void solveButtonComponentHidden(java.awt.event.ComponentEvent evt) {
- }
- private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {
- javax.swing.UIManager.put( "FileChooser.CancelButtonText", "Anuluj" );
- javax.swing.UIManager.put( "OpenButtonText", "Otwórz" );
- javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
- javax.swing.filechooser.FileNameExtensionFilter filter = new javax.swing.filechooser.FileNameExtensionFilter("Plik tekstowy", "txt");
- fileChooser.setFileFilter(filter);
- int retValue = fileChooser.showOpenDialog(this);
- if ( retValue == JFileChooser.APPROVE_OPTION ){
- file = fileChooser.getSelectedFile();
- }
- try {
- drawMaze();
- } catch (IOException ex) {
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new JFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel imageLabel;
- private javax.swing.JButton insertButton;
- private javax.swing.JButton solveButton;
- // End of variables declaration
- private int getMazeSize( File file ) throws FileNotFoundException, IOException {
- BufferedReader txt = new BufferedReader( new FileReader( file ) );
- String line;
- String size = "";
- while( ( line = txt.readLine() ) != null ){
- StringTokenizer t = new StringTokenizer(line, " ");
- size = t.nextToken();
- }
- return Integer.parseInt(size);
- }
- private void drawMaze() throws IOException {
- BufferedReader txt = new BufferedReader( new FileReader( file ) );
- String line;
- int size = getMazeSize( file ) + 1;
- BufferedImage img = new BufferedImage( size, size , BufferedImage.TYPE_INT_RGB);
- Graphics2D graphics2d = (Graphics2D) img.getGraphics();
- while( ( line = txt.readLine() ) != null ){
- StringTokenizer t = new StringTokenizer(line, " ");
- int x = Integer.parseInt(t.nextToken());
- int y = Integer.parseInt(t.nextToken());
- if ( t.hasMoreTokens()){
- String operation = t.nextToken();
- switch (operation) {
- case "PD":
- graphics2d.drawLine(x, y, x + 1, y);
- graphics2d.drawLine(x, y, x, y + 1);
- break;
- case "P":
- graphics2d.drawLine(x, y, x + 1, y);
- break;
- case "D":
- graphics2d.drawLine(x, y, x, y + 1);
- break;
- default:
- JFrame error = new JFrame();
- JOptionPane.showMessageDialog( error , "Incorrect input file" );
- break;
- }
- }
- }
- showImageInJLabel( img, imageLabel);
- }
- private void showImageInJLabel(BufferedImage bufferedImage, JLabel imageLabel) {
- int labelWidth = imageLabel.getWidth();
- int labelHeight = imageLabel.getHeight();
- Image imageTemp = bufferedImage.getScaledInstance(labelWidth, labelHeight, java.awt.Image.SCALE_SMOOTH);
- ImageIcon imageIcon = new ImageIcon(imageTemp);
- imageLabel.setIcon(imageIcon);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement