Advertisement
CaptainSpaceCat

3DPMaze - RunUI

Jul 18th, 2016
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.event.*;
  2. import javax.swing.filechooser.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.io.*;
  6. import javax.imageio.*;
  7. import java.awt.image.*;
  8. import java.awt.Graphics;
  9. import java.awt.Dimension;
  10.  
  11. /*
  12.  * This class handles the UI and allows
  13.  * the user to input an image and control
  14.  * the rest of the algorithm.
  15.  * */
  16.  
  17. public class RunUI extends JFrame implements ActionListener, ChangeListener {
  18.  
  19.   public String stl;
  20.  
  21.   JButton startButton, saveButton;
  22.   JTextField percentField;
  23.   JFileChooser fc;
  24.   JFrame f;
  25.   JPanel panel;
  26.   JLabel pic, widthLabel, heightLabel, logLabel;
  27.   JSlider widthSlider, heightSlider;
  28.   File currentFile;
  29.   FrameImage mazePic;
  30.   boolean fileOpen = false;
  31.  
  32.   public RunUI() {
  33.     fc = new JFileChooser();
  34.     FileNameExtensionFilter filter = new FileNameExtensionFilter(".stl (STL Files)", "stl");
  35.     fc.setFileFilter(filter);
  36.     initializeGUI();
  37.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.     repaint();
  39.   }
  40.  
  41.   private void initializeGUI() {
  42.     panel = new JPanel() {
  43.       public void paintComponent(Graphics g) {
  44.         super.paintComponent(g);
  45.       }
  46.     };
  47.    
  48.     this.setSize(new Dimension(700, 540));
  49.     panel.setLayout(null);
  50.     panel.setPreferredSize(new Dimension(700, 540));
  51.    
  52.     mazePic = new FrameImage(500, 500);
  53.     pic = new JLabel(new ImageIcon(mazePic));
  54.     panel.add(pic);
  55.     pic.setBounds(20, 20, 500, 500);
  56.    
  57.     widthSlider = new JSlider(JSlider.HORIZONTAL, 5, 8, 8);
  58.     widthSlider.addChangeListener(this);
  59.     panel.add(widthSlider);
  60.     widthSlider.setBounds(540, 40, 120, 20);
  61.    
  62.     widthLabel = new JLabel("Width: 8", JLabel.CENTER);
  63.     panel.add(widthLabel);
  64.     widthLabel.setBounds(540, 20, 120, 20);
  65.    
  66.     heightSlider = new JSlider(JSlider.HORIZONTAL, 5, 8, 8);
  67.     heightSlider.addChangeListener(this);
  68.     panel.add(heightSlider);
  69.     heightSlider.setBounds(540, 80, 120, 20);
  70.    
  71.     heightLabel = new JLabel("Height: 8", JLabel.CENTER);
  72.     panel.add(heightLabel);
  73.     heightLabel.setBounds(540, 60, 120, 20);
  74.    
  75.     startButton = new JButton("Generate");
  76.     startButton.addActionListener(this);
  77.     panel.add(startButton);
  78.     startButton.setBounds(540, 100, 120, 30);
  79.    
  80.     saveButton = new JButton("Save");
  81.     saveButton.addActionListener(this);
  82.     panel.add(saveButton);
  83.     saveButton.setBounds(540, 140, 120, 30);
  84.     saveButton.setEnabled(false);
  85.    
  86.     pack();
  87.     getContentPane().add(panel);
  88.     getRootPane().setDefaultButton(startButton);
  89.     panel.setVisible(true);
  90.   }
  91.  
  92.   public void stateChanged(ChangeEvent e) {
  93.     if (e.getSource() == widthSlider) {
  94.       widthLabel.setText("Width: " + widthSlider.getValue());
  95.       repaint();
  96.     } else if (e.getSource() == heightSlider) {
  97.       heightLabel.setText("Height: " + heightSlider.getValue());
  98.       repaint();
  99.     }
  100.   }
  101.  
  102.   public void actionPerformed(ActionEvent e) {
  103.     if (e.getSource() == startButton) {
  104.       RecursiveGen r = new RecursiveGen();
  105.       boolean[][][] maze = r.generate(widthSlider.getValue(), heightSlider.getValue());
  106.       while (!r.validateMaze(maze)) {
  107.         maze = r.generate(widthSlider.getValue(), heightSlider.getValue());
  108.       }
  109.       float edgeSize = .1f;
  110.       float cellSizeX = (8f-edgeSize)/widthSlider.getValue() - edgeSize;
  111.       float cellSizeY = (8f-edgeSize)/heightSlider.getValue() - edgeSize;
  112.       MazeMesh mazemesh = new MazeMesh(maze, cellSizeX, cellSizeY, edgeSize);
  113.       int s = 500;
  114.       int t = 2;
  115.       int wd = maze.length;
  116.       int ht = maze[0].length;
  117.       mazePic = new FrameImage((int)((s-(wd+1)*t)/wd)*wd+(wd+1)*t, (int)((s-(ht+1)*t)/ht)*ht+(ht+1)*t, s, t, maze);
  118.       try {
  119.         stl = mazemesh.getSTL();
  120.       } catch (Exception ex) {
  121.       } finally {
  122.         saveButton.setEnabled(true);
  123.         repaint();
  124.       }
  125.     } else if (e.getSource() == saveButton) {
  126.       int returnVal = fc.showSaveDialog(this);
  127.       if (returnVal == JFileChooser.APPROVE_OPTION) {
  128.         currentFile = fc.getSelectedFile();
  129.        
  130.         //String filePath = currentFile.getPath().substring(0, currentFile.getPath().lastIndexOf("."));
  131.         //String fileType = currentFile.getPath().substring(currentFile.getPath().lastIndexOf("."));
  132.         String filePath = currentFile.getPath();
  133.         if (filePath.lastIndexOf(".") != -1) {
  134.           filePath = currentFile.getPath().substring(0, filePath.lastIndexOf("."));
  135.         }
  136.         filePath = filePath + ".stl";
  137.         String fileName = currentFile.getName();
  138.        
  139.         BufferedWriter output = null;
  140.         try {
  141.           File f = new File(filePath);
  142.           output = new BufferedWriter(new FileWriter(f));
  143.           output.write(stl);
  144.         } catch (IOException i) {
  145.         } finally {
  146.           if (output != null) {
  147.             try {
  148.               //repaint("STL file saved as " + fileName + " to " + filePath.substring(0, filePath.lastIndexOf("\\")));
  149.               output.close();
  150.             } catch(IOException i) {
  151.             }
  152.           }
  153.         }
  154.       }
  155.     }
  156.   }
  157.  
  158.   public void repaint() {
  159.     if (pic != null) {
  160.       panel.remove(pic);
  161.       pic = new JLabel(new ImageIcon(mazePic));
  162.       panel.add(pic);
  163.       pic.setBounds(20, 20, 500, 500);
  164.     }
  165.     pack();
  166.     panel.repaint();
  167.   }
  168.  
  169.   public void repaint(String str) {
  170.     log(str);
  171.     pack();
  172.     panel.repaint();
  173.   }
  174.  
  175.   void log(String str) {
  176.     logLabel.setText(str);
  177.   }
  178.  
  179.   public void init() {
  180.    
  181.     SwingUtilities.invokeLater(new Runnable() {
  182.       public void run() {
  183.         setVisible(true);
  184.       }
  185.     });
  186.   }
  187.  
  188. }
Advertisement
RAW Paste Data Copied
Advertisement