Advertisement
CaptainSpaceCat

FluffyClouds

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