Advertisement
CaptainSpaceCat

Maze Progress Bar - Main

Aug 9th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.18 KB | None | 0 0
  1. import java.awt.image.*;
  2. import javax.imageio.*;
  3. import java.io.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. import java.beans.*;
  8. import javax.swing.filechooser.*;
  9. import java.lang.Math;
  10.  
  11. public class Main extends JPanel implements ActionListener, PropertyChangeListener {
  12.  
  13.   static int w = 500;
  14.   static int h = 500;
  15.   static int baseCol = -5708545;
  16.  
  17.   private JLabel iterLabel, pic;
  18.   private JTextField iterField;
  19.   private JFileChooser fc;
  20.   private JProgressBar progressBar;
  21.   private JButton startButton;
  22.   private Task task;
  23.  
  24.   class Task extends SwingWorker<Void, Void> {
  25.     /*
  26.      * Main task. Executed in background thread.
  27.      */
  28.     @Override
  29.     public Void doInBackground() {
  30.       RecursiveGen rg = new RecursiveGen(w/2, h/2);
  31.       setProgress(0);
  32.       BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  33.      
  34.       /*for (int y = 0; y < h; y++) {
  35.         for (int x = 0; x < w; x++) {
  36.           bi.setRGB(x, y, (int)(Math.random()*-16777217));
  37.         }
  38.       }*/
  39.      
  40.       //for (int n = 0; n < 10; n++) {
  41.       //int iterations = Integer.parseInt(iterField.getText());
  42.       /*for (int i = 0; i < iterations; i++) {
  43.        bi = smooth(bi);
  44.        pic.setIcon(new ImageIcon(bi));
  45.        setProgress((int)(i*100f/iterations+.5f));
  46.        }*/
  47.      
  48.       while (!rg.checkCompleted()) {
  49.         try {
  50.           rg.step();
  51.           /*bi = formatImg(rg.step());
  52.           pic.setIcon(new ImageIcon(bi));*/
  53.           setProgress(rg.getProgress());
  54.           try {
  55.             Thread.sleep(1);
  56.           } catch (InterruptedException ignore) {
  57.           }
  58.         } catch (Exception ex) {
  59.           //System.err.println(ex);
  60.         }
  61.       }
  62.       trySave(formatImg(rg.getMaze()));
  63.       //}
  64.       setProgress(100);
  65.       return null;
  66.     }
  67.    
  68.     /*
  69.      * Executed in event dispatching thread
  70.      */
  71.     @Override
  72.     public void done() {
  73.       Toolkit.getDefaultToolkit().beep();
  74.       startButton.setEnabled(true);
  75.       setCursor(null); //turn off the wait cursor
  76.     }
  77.   }
  78.  
  79.   public BufferedImage formatImg(boolean[][][] m) {
  80.     BufferedImage b = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  81.     for (int y = 0; y < m[0].length; y++) {
  82.       for (int x = 0; x < m.length; x++) {
  83.         b.setRGB(x*2+1, y*2+1, Color.red.getRGB());
  84.         if (!m[x][y][1]) {
  85.           b.setRGB(x*2+2, y*2+1, Color.red.getRGB());
  86.         }
  87.         if (!m[x][y][2]) {
  88.           b.setRGB(x*2+1, y*2+2, Color.red.getRGB());
  89.         }
  90.       }
  91.     }
  92.     return b;
  93.   }
  94.  
  95.   public void actionPerformed(ActionEvent evt) {
  96.     if (evt.getSource() == startButton) {
  97.       startButton.setEnabled(false);
  98.       setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  99.       //Instances of javax.swing.SwingWorker are not reusuable, so
  100.       //we create new instances as needed.
  101.       task = new Task();
  102.       task.addPropertyChangeListener(this);
  103.       task.execute();
  104.       //debug();
  105.     }
  106.   }
  107.  
  108.   public void propertyChange(PropertyChangeEvent evt) {
  109.     if ("progress" == evt.getPropertyName()) {
  110.       int progress = (Integer) evt.getNewValue();
  111.       progressBar.setValue(progress);
  112.     }
  113.   }
  114.  
  115.   public static BufferedImage smooth(BufferedImage bi) {
  116.     BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
  117.     for (int y = 0; y < h; y++) {
  118.       for (int x = 0; x < w; x++) {
  119.         img.setRGB(x, y, getAverageColor(x, y, bi));
  120.       }
  121.     }
  122.     return img;
  123.   }
  124.  
  125.   public static int getAverageRGB(int x, int y, BufferedImage template) {
  126.     int n = 0;
  127.     int d = 0;
  128.     if (x > 0) {
  129.       n+=template.getRGB(x-1, y);
  130.       d++;
  131.     }
  132.     if (y > 0) {
  133.       n+=template.getRGB(x, y-1);
  134.       d++;
  135.     }
  136.     if (x < template.getWidth()-1) {
  137.       n+=template.getRGB(x+1, y);
  138.       d++;
  139.     }
  140.     if (y < template.getHeight()-1) {
  141.       n+=template.getRGB(x, y+1);
  142.       d++;
  143.     }
  144.     if (x > 0 && y > 0) {
  145.       n+=template.getRGB(x-1, y-1);
  146.       d++;
  147.     }
  148.     if (y > 0 && x < template.getWidth()-1) {
  149.       n+=template.getRGB(x+1, y-1);
  150.       d++;
  151.     }
  152.     if (x < template.getWidth()-1 && y < template.getHeight()-1) {
  153.       n+=template.getRGB(x+1, y+1);
  154.       d++;
  155.     }
  156.     if (y < template.getHeight()-1 && x > 0) {
  157.       n+=template.getRGB(x-1, y+1);
  158.       d++;
  159.     }
  160.     return (int)((n*1f/d)+.5f);
  161.   }
  162.  
  163.   public static int getAverageColor(int x, int y, BufferedImage template) {
  164.     int nRed = 0;
  165.     int nGreen = 0;
  166.     int nBlue = 0;
  167.     int d = 0;
  168.     if (x > 0) {
  169.       Color col = new Color(template.getRGB(x-1, y));
  170.       nRed += col.getRed();
  171.       nGreen += col.getGreen();
  172.       nBlue += col.getBlue();
  173.       d++;
  174.     }
  175.     if (y > 0) {
  176.       Color col = new Color(template.getRGB(x, y-1));
  177.       nRed += col.getRed();
  178.       nGreen += col.getGreen();
  179.       nBlue += col.getBlue();
  180.       d++;
  181.     }
  182.     if (x < template.getWidth()-1) {
  183.       Color col = new Color(template.getRGB(x+1, y));
  184.       nRed += col.getRed();
  185.       nGreen += col.getGreen();
  186.       nBlue += col.getBlue();
  187.       d++;
  188.     }
  189.     if (y < template.getHeight()-1) {
  190.       Color col = new Color(template.getRGB(x, y+1));
  191.       nRed += col.getRed();
  192.       nGreen += col.getGreen();
  193.       nBlue += col.getBlue();
  194.       d++;
  195.     }
  196.     if (x > 0 && y > 0) {
  197.       Color col = new Color(template.getRGB(x-1, y-1));
  198.       nRed += col.getRed();
  199.       nGreen += col.getGreen();
  200.       nBlue += col.getBlue();
  201.       d++;
  202.     }
  203.     if (y > 0 && x < template.getWidth()-1) {
  204.       Color col = new Color(template.getRGB(x+1, y-1));
  205.       nRed += col.getRed();
  206.       nGreen += col.getGreen();
  207.       nBlue += col.getBlue();
  208.       d++;
  209.     }
  210.     if (x < template.getWidth()-1 && y < template.getHeight()-1) {
  211.       Color col = new Color(template.getRGB(x+1, y+1));
  212.       nRed += col.getRed();
  213.       nGreen += col.getGreen();
  214.       nBlue += col.getBlue();
  215.       d++;
  216.     }
  217.     if (y < template.getHeight()-1 && x > 0) {
  218.       Color col = new Color(template.getRGB(x-1, y+1));
  219.       nRed += col.getRed();
  220.       nGreen += col.getGreen();
  221.       nBlue += col.getBlue();
  222.       d++;
  223.     }
  224.     return new Color((int)((nRed*1f/d)+.5f), (int)((nGreen*1f/d)+.5f), (int)((nBlue*1f/d)+.5f)).getRGB();
  225.   }
  226.  
  227.   public Main() {
  228.     super(new BorderLayout());
  229.    
  230.     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  231.     if (w == 0 && h == 0) {
  232.       w = (int)screen.getWidth();
  233.       h = (int)screen.getHeight();
  234.     }
  235.     System.out.println(w + ":" + h);
  236.    
  237.     fc = new JFileChooser();
  238.     FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "png", "jpg", "jpeg", "bmp");
  239.     fc.setFileFilter(filter);
  240.     //Create the UI.
  241.    
  242.     pic = new JLabel();
  243.    
  244.     startButton = new JButton("Start");
  245.     startButton.setActionCommand("start");
  246.     startButton.addActionListener(this);
  247.    
  248.     progressBar = new JProgressBar(0, 100);
  249.     progressBar.setValue(0);
  250.     progressBar.setStringPainted(true);
  251.    
  252.     iterLabel = new JLabel("Iterations:");
  253.     iterField = new JTextField("100");
  254.     iterField.addActionListener(this);
  255.    
  256.     JPanel panel = new JPanel();
  257.     panel.add(startButton);
  258.     panel.add(progressBar);
  259.     panel.add(iterLabel);
  260.     panel.add(iterField);
  261.    
  262.     panel.add(pic);
  263.    
  264.     add(panel, BorderLayout.PAGE_START);
  265.     setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  266.    
  267.   }
  268.  
  269.   private static void createAndShowGUI() {
  270.     //Create and set up the window.
  271.     JFrame frame = new JFrame("Smooth Image");
  272.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  273.    
  274.     //Create and set up the content pane.
  275.     JComponent newContentPane = new Main();
  276.     newContentPane.setOpaque(true); //content panes must be opaque
  277.     frame.setContentPane(newContentPane);
  278.    
  279.     //Display the window.
  280.     frame.pack();
  281.     frame.setVisible(true);
  282.   }
  283.  
  284.   public void trySave(BufferedImage bi) {
  285.     int returnVal = fc.showSaveDialog(this);
  286.     if (returnVal == JFileChooser.APPROVE_OPTION) {
  287.       File currentFile = fc.getSelectedFile();
  288.       String filePath = currentFile.getPath();
  289.       if (filePath.lastIndexOf(".") != -1) {
  290.         filePath = currentFile.getPath().substring(0, filePath.lastIndexOf("."));
  291.       }
  292.       filePath = filePath + ".png";
  293.      
  294.       try {
  295.         ImageIO.write(bi, "png", new File(filePath));
  296.       } catch(IOException e) {
  297.       }
  298.     }
  299.   }
  300.  
  301.  
  302.   public void debug() {
  303.     RecursiveGen rg = new RecursiveGen(w/2, h/2);
  304.       BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  305.      
  306.       for (int y = 0; y < h; y++) {
  307.         for (int x = 0; x < w; x++) {
  308.           bi.setRGB(x, y, (int)(Math.random()*-16777217));
  309.         }
  310.       }
  311.      
  312.       while (!rg.checkCompleted()) {
  313.         bi = formatImg(rg.step());
  314.         pic.setIcon(new ImageIcon(bi));
  315.       }
  316.   }
  317.  
  318.  
  319.   public static void main(String[] args) {
  320.     //Schedule a job for the event-dispatching thread:
  321.     //creating and showing this application's GUI.
  322.    
  323.     javax.swing.SwingUtilities.invokeLater(new Runnable() {
  324.       public void run() {
  325.         createAndShowGUI();
  326.       }
  327.     });
  328.   }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement