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 ElectronCutout extends JFrame implements ActionListener, ChangeListener {
- public String stl;
- private boolean DEBUG = false;
- ImageEditor imgEdit;
- STLConverter stlConv;
- JButton openButton, saveButton, thresholdButton, rodButton, chunkButton, thickenButton, stlButton;
- JTextField percentField;
- JFileChooser fc;
- JFrame f;
- JPanel panel;
- JLabel pic, sliderLabel, logLabel;
- JSlider scaleSlider;
- File currentFile;
- boolean fileOpen = false;
- BufferedImage img;
- float pxcm;
- FileNameExtensionFilter iFilter = new FileNameExtensionFilter("Image files", "png", "jpg", "jpeg", "bmp");
- FileNameExtensionFilter oFilter = new FileNameExtensionFilter(".stl (STL Files)", "stl");
- public ElectronCutout() {
- fc = new JFileChooser();
- initializeGUI();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- repaint();
- }
- public BufferedImage loadImage(File f) {
- BufferedImage i = null;
- try {
- i = ImageIO.read(f);
- } catch (IOException e) {
- }
- if (imgEdit.calibrateImg(i)) {
- float[] info = imgEdit.getImgInfo();
- center = new int[] {(int)info[0], (int)info[1]};
- pxcm = info[2];
- }
- return i;
- }
- public void initializeGUI() {
- panel = new JPanel() {
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- }
- };
- this.setSize(new Dimension(900, 700));
- panel.setLayout(null);
- panel.setPreferredSize(new Dimension(900, 700));
- openButton = new JButton("Open");
- openButton.addActionListener(this);
- panel.add(openButton);
- openButton.setBounds(20, 10, 100, 30);
- saveButton = new JButton("Save");
- saveButton.addActionListener(this);
- panel.add(saveButton);
- saveButton.setBounds(120, 10, 100, 30);
- saveButton.setEnabled(false);
- if (DEBUG) {
- rodButton = new JButton("Clear Crosshairs");
- rodButton.addActionListener(this);
- panel.add(rodButton);
- rodButton.setBounds(740, 50, 130, 30);
- rodButton.setEnabled(false);
- chunkButton = new JButton("Clear Chunks");
- chunkButton.addActionListener(this);
- panel.add(chunkButton);
- chunkButton.setBounds(740, 90, 130, 30);
- chunkButton.setEnabled(false);
- thresholdButton = new JButton("Set Threshold");
- thresholdButton.addActionListener(this);
- panel.add(thresholdButton);
- thresholdButton.setBounds(740, 130, 130, 30);
- thresholdButton.setEnabled(false);
- thickenButton = new JButton("Thicken Margin");
- thickenButton.addActionListener(this);
- panel.add(thickenButton);
- thickenButton.setBounds(740, 170, 130, 30);
- thickenButton.setEnabled(false);
- }
- stlButton = new JButton("Convert to STL");
- stlButton.addActionListener(this);
- panel.add(stlButton);
- if (DEBUG) {
- stlButton.setBounds(740, 210, 130, 30);
- } else {
- stlButton.setBounds(740, 50, 130, 30);
- }
- stlButton.setEnabled(false);
- scaleSlider = new JSlider(JSlider.HORIZONTAL, 75, 100, 95);
- scaleSlider.addChangeListener(this);
- panel.add(scaleSlider);
- if (DEBUG) {
- scaleSlider.setBounds(740, 270, 130, 30);
- } else {
- scaleSlider.setBounds(740, 110, 130, 30);
- }
- scaleSlider.setVisible(false);
- sliderLabel = new JLabel("Scale down to: %", JLabel.CENTER);
- panel.add(sliderLabel);
- if (DEBUG) {
- sliderLabel.setBounds(740, 250, 130, 20);
- } else {
- sliderLabel.setBounds(740, 90, 130, 20);
- }
- sliderLabel.setVisible(false);
- percentField = new JTextField("95");
- percentField.addActionListener(this);
- panel.add(percentField);
- if (DEBUG) {
- percentField.setBounds(830, 250, 25, 20);
- } else {
- percentField.setBounds(830, 90, 25, 20);
- }
- percentField.setVisible(false);
- logLabel = new JLabel("");
- panel.add(logLabel);
- logLabel.setBounds(20, 670, 800, 20);
- BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_BYTE_BINARY);
- pic = new JLabel(new ImageIcon(bi));
- panel.add(pic);
- pic.setBounds(20, 50, 500, 500);
- /*pic = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imgBasic.png"))));
- panel.add(pic);
- pic.setBounds(20, 50, w, h);*/
- formatPanel(500, 500);
- pack();
- getContentPane().add(panel);
- getRootPane().setDefaultButton(openButton);
- panel.setVisible(true);
- }
- public void stateChanged(ChangeEvent e) {
- if (e.getSource() == scaleSlider) {
- percentField.setText("" + scaleSlider.getValue());
- repaint();
- }
- }
- void log(String str) {
- logLabel.setText(str);
- }
- private int[] center;
- private int[][] crossPoints;
- public String fileType;
- public String filePath;
- public String fileName;
- public String filePathConst;
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == openButton) {
- fc.setFileFilter(iFilter);
- int returnVal = fc.showOpenDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- currentFile = fc.getSelectedFile();
- filePath = currentFile.getPath().substring(0, currentFile.getPath().lastIndexOf("."));
- filePathConst = currentFile.getPath().substring(0, currentFile.getPath().lastIndexOf("."));
- fileType = currentFile.getPath().substring(currentFile.getPath().lastIndexOf("."));
- fileName = currentFile.getName();
- System.out.println(filePath + " " + fileType);
- img = loadImage(currentFile);
- if (img != null) {
- if (pic != null) {
- panel.remove(pic);
- }
- pic = new JLabel(new ImageIcon(img));
- panel.add(pic);
- pic.setBounds(20, 50, imgEdit.w, imgEdit.h);
- formatPanel(imgEdit.w, imgEdit.h);
- fileOpen = true;
- saveButton.setEnabled(false);
- if (DEBUG) {
- chunkButton.setEnabled(false);
- thresholdButton.setEnabled(false);
- thickenButton.setEnabled(false);
- stlButton.setEnabled(false);
- scaleSlider.setVisible(false);
- sliderLabel.setVisible(false);
- percentField.setVisible(false);
- rodButton.setEnabled(true);
- } else {
- stlButton.setEnabled(true);
- scaleSlider.setVisible(true);
- sliderLabel.setVisible(true);
- percentField.setVisible(true);
- }
- repaint(fileName + " loaded.");
- } else {
- repaint("Failed to load image file.");
- }
- } else {
- }
- } else if (e.getSource() == saveButton) {
- fc.setFileFilter(oFilter);
- fc.setSelectedFile(new File(filePathConst + "_model.stl"));
- int returnVal = fc.showSaveDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- currentFile = fc.getSelectedFile();
- filePath = currentFile.getPath();
- if (filePath.lastIndexOf(".") != -1) {
- filePath = currentFile.getPath().substring(0, filePath.lastIndexOf("."));
- }
- filePath = filePath + ".stl";
- BufferedWriter output = null;
- try {
- File f = new File(filePath);
- fileName = f.getName();
- output = new BufferedWriter(new FileWriter(f));
- output.write(stl);
- } catch (IOException i) {
- i.printStackTrace();
- } finally {
- if (output != null) {
- try {
- repaint("STL file saved as " + fileName + " to " + filePath.substring(0, filePath.lastIndexOf("\\")));
- output.close();
- } catch(IOException i) {
- }
- }
- }
- }
- } else if (e.getSource() == thresholdButton) {
- if (fileOpen) {
- try {
- imgEdit.threshold();
- thickenButton.setEnabled(true);
- repaint("Threshold set.");
- } catch(Exception ex) {
- repaint("Failed.");
- }
- }
- } else if (e.getSource() == rodButton) {
- if (fileOpen) {
- try {
- imgEdit.removeRods();
- chunkButton.setEnabled(true);
- repaint("Crosshairs cleared.");
- } catch(Exception ex) {
- repaint("Failed.");
- }
- }
- } else if (e.getSource() == chunkButton) {
- if (fileOpen) {
- try {
- imgEdit.clearChunks();
- thresholdButton.setEnabled(true);
- repaint("Excess pixels removed.");
- } catch(Exception ex) {
- repaint("Failed.");
- }
- }
- } else if (e.getSource() == thickenButton) {
- if (fileOpen) {
- try {
- imgEdit.thickenMargin(10);
- imgEdit.thickenMargin(10);
- crossPoints = imgEdit.getCrossPoints();
- //imgEdit.thickenCrosshairs();
- stlButton.setEnabled(true);
- scaleSlider.setVisible(true);
- sliderLabel.setVisible(true);
- percentField.setVisible(true);
- repaint("Margins thickened.");
- } catch(Exception ex) {
- repaint("Failed.");
- }
- }
- } else if (e.getSource() == stlButton) {
- if (fileOpen) {
- if (!DEBUG) {
- try {
- imgEdit.removeRods();
- chunkButton.setEnabled(true);
- repaint("Crosshairs cleared.");
- } catch(Exception ex) {
- repaint("Failed to clear crosshairs.");
- }
- try {
- imgEdit.clearChunks();
- thresholdButton.setEnabled(true);
- repaint("Excess pixels removed.");
- } catch(Exception ex) {
- repaint("Failed to remove excess pixels.");
- }
- try {
- imgEdit.threshold();
- thickenButton.setEnabled(true);
- repaint("Threshold set.");
- } catch(Exception ex) {
- repaint("Failed to set threshold.");
- }
- try {
- imgEdit.thickenMargin(10);
- imgEdit.thickenMargin(10);
- crossPoints = imgEdit.getCrossPoints();
- //imgEdit.thickenCrosshairs();
- stlButton.setEnabled(true);
- scaleSlider.setVisible(true);
- sliderLabel.setVisible(true);
- percentField.setVisible(true);
- repaint("Margins thickened.");
- } catch(Exception ex) {
- repaint("Failed to thicken margins.");
- }
- }
- try {
- stlConv.setImageTemplate(copyImage(img));
- stlConv.setCenterInfo(center, crossPoints);
- stl = stlConv.convertImage(img, (scaleSlider.getValue()/100f) * (.94f/.95f), pxcm);
- saveButton.setEnabled(true);
- stlButton.setEnabled(false);
- scaleSlider.setVisible(false);
- sliderLabel.setVisible(false);
- percentField.setVisible(false);
- repaint("STL file generated.");
- } catch(Exception ex) {
- repaint("STL file conversion failed.");
- }
- }
- } else if (e.getSource() == percentField) {
- scaleSlider.setValue(Integer.parseInt(percentField.getText()));
- } else {
- }
- }
- public BufferedImage copyImage(BufferedImage i) {
- BufferedImage o = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
- for (int y = 0; y < i.getHeight(); y++) {
- for (int x = 0; x < i.getWidth(); x++) {
- o.setRGB(x, y, i.getRGB(x, y));
- }
- }
- return o;
- }
- public void formatPanel(int w, int h) {
- int minX = 200; //290
- int minY = 230; //320
- int dimX = w < minX ? minX : w;
- int dimY = h < minY ? minY : h;
- this.setSize(new Dimension(dimX + 190, dimY + 80));
- panel.setPreferredSize(new Dimension(dimX + 190, dimY + 80));
- if (DEBUG) {
- rodButton.setBounds(dimX + 40, 50, 130, 30);
- chunkButton.setBounds(dimX + 40, 90, 130, 30);
- thresholdButton.setBounds(dimX + 40, 130, 130, 30);
- thickenButton.setBounds(dimX + 40, 170, 130, 30);
- stlButton.setBounds(dimX + 40, 210, 130, 30);
- scaleSlider.setBounds(dimX + 40, 270, 130, 30);
- sliderLabel.setBounds(dimX + 40, 250, 130, 20);
- percentField.setBounds(dimX + 130, 250, 25, 20);
- } else {
- stlButton.setBounds(dimX + 40, 50, 130, 30);
- scaleSlider.setBounds(dimX + 40, 110, 130, 30);
- sliderLabel.setBounds(dimX + 40, 90, 130, 20);
- percentField.setBounds(dimX + 130, 90, 25, 20);
- }
- logLabel.setBounds(20, dimY + 50, 800, 20);
- }
- public void repaint() {
- pack();
- panel.repaint();
- }
- public void repaint(String str) {
- log(str);
- pack();
- panel.repaint();
- }
- public void init() {
- imgEdit = new ImageEditor();
- stlConv = new STLConverter();
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- setVisible(true);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment