Advertisement
CaptainSpaceCat

Smooth Progress Bar

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