Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import javax.swing.filechooser.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.io.*;
- import javax.imageio.*;
- import java.awt.image.*;
- import java.awt.Graphics;
- import java.awt.Dimension;
- /*
- * This class handles the UI and allows
- * the user to input an image and control
- * the rest of the algorithm.
- * */
- public class RunUI extends JFrame implements ActionListener, ChangeListener {
- public String stl;
- JButton startButton, saveButton;
- JTextField percentField;
- JFileChooser fc;
- JFrame f;
- JPanel panel;
- JLabel pic, widthLabel, heightLabel, logLabel;
- JSlider widthSlider, heightSlider;
- File currentFile;
- FrameImage mazePic;
- boolean fileOpen = false;
- public RunUI() {
- fc = new JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter(".stl (STL Files)", "stl");
- fc.setFileFilter(filter);
- initializeGUI();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- repaint();
- }
- private void initializeGUI() {
- panel = new JPanel() {
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- }
- };
- this.setSize(new Dimension(700, 540));
- panel.setLayout(null);
- panel.setPreferredSize(new Dimension(700, 540));
- mazePic = new FrameImage(500, 500);
- pic = new JLabel(new ImageIcon(mazePic));
- panel.add(pic);
- pic.setBounds(20, 20, 500, 500);
- widthSlider = new JSlider(JSlider.HORIZONTAL, 5, 8, 8);
- widthSlider.addChangeListener(this);
- panel.add(widthSlider);
- widthSlider.setBounds(540, 40, 120, 20);
- widthLabel = new JLabel("Width: 8", JLabel.CENTER);
- panel.add(widthLabel);
- widthLabel.setBounds(540, 20, 120, 20);
- heightSlider = new JSlider(JSlider.HORIZONTAL, 5, 8, 8);
- heightSlider.addChangeListener(this);
- panel.add(heightSlider);
- heightSlider.setBounds(540, 80, 120, 20);
- heightLabel = new JLabel("Height: 8", JLabel.CENTER);
- panel.add(heightLabel);
- heightLabel.setBounds(540, 60, 120, 20);
- startButton = new JButton("Generate");
- startButton.addActionListener(this);
- panel.add(startButton);
- startButton.setBounds(540, 100, 120, 30);
- saveButton = new JButton("Save");
- saveButton.addActionListener(this);
- panel.add(saveButton);
- saveButton.setBounds(540, 140, 120, 30);
- saveButton.setEnabled(false);
- pack();
- getContentPane().add(panel);
- getRootPane().setDefaultButton(startButton);
- panel.setVisible(true);
- }
- public void stateChanged(ChangeEvent e) {
- if (e.getSource() == widthSlider) {
- widthLabel.setText("Width: " + widthSlider.getValue());
- repaint();
- } else if (e.getSource() == heightSlider) {
- heightLabel.setText("Height: " + heightSlider.getValue());
- repaint();
- }
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == startButton) {
- RecursiveGen r = new RecursiveGen();
- boolean[][][] maze = r.generate(widthSlider.getValue(), heightSlider.getValue());
- while (!r.validateMaze(maze)) {
- maze = r.generate(widthSlider.getValue(), heightSlider.getValue());
- }
- float edgeSize = .1f;
- float cellSizeX = (8f-edgeSize)/widthSlider.getValue() - edgeSize;
- float cellSizeY = (8f-edgeSize)/heightSlider.getValue() - edgeSize;
- MazeMesh mazemesh = new MazeMesh(maze, cellSizeX, cellSizeY, edgeSize);
- int s = 500;
- int t = 2;
- int wd = maze.length;
- int ht = maze[0].length;
- 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);
- try {
- stl = mazemesh.getSTL();
- } catch (Exception ex) {
- } finally {
- saveButton.setEnabled(true);
- repaint();
- }
- } else if (e.getSource() == saveButton) {
- int returnVal = fc.showSaveDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- currentFile = fc.getSelectedFile();
- //String filePath = currentFile.getPath().substring(0, currentFile.getPath().lastIndexOf("."));
- //String fileType = currentFile.getPath().substring(currentFile.getPath().lastIndexOf("."));
- String filePath = currentFile.getPath();
- if (filePath.lastIndexOf(".") != -1) {
- filePath = currentFile.getPath().substring(0, filePath.lastIndexOf("."));
- }
- filePath = filePath + ".stl";
- String fileName = currentFile.getName();
- BufferedWriter output = null;
- try {
- File f = new File(filePath);
- output = new BufferedWriter(new FileWriter(f));
- output.write(stl);
- } catch (IOException i) {
- } finally {
- if (output != null) {
- try {
- //repaint("STL file saved as " + fileName + " to " + filePath.substring(0, filePath.lastIndexOf("\\")));
- output.close();
- } catch(IOException i) {
- }
- }
- }
- }
- }
- }
- public void repaint() {
- if (pic != null) {
- panel.remove(pic);
- pic = new JLabel(new ImageIcon(mazePic));
- panel.add(pic);
- pic.setBounds(20, 20, 500, 500);
- }
- pack();
- panel.repaint();
- }
- public void repaint(String str) {
- log(str);
- pack();
- panel.repaint();
- }
- void log(String str) {
- logLabel.setText(str);
- }
- public void init() {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- setVisible(true);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement