Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.*;
- import javax.imageio.*;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.beans.*;
- import javax.swing.filechooser.*;
- import java.lang.Math;
- public class Main extends JPanel implements ActionListener, PropertyChangeListener {
- static int w;
- static int h;
- static int baseCol = -5708545;
- private JLabel iterLabel, pic;
- private JTextField iterField;
- private JFileChooser fc;
- private JProgressBar progressBar;
- private JButton startButton;
- private Task task;
- class Task extends SwingWorker<Void, Void> {
- /*
- * Main task. Executed in background thread.
- */
- @Override
- public Void doInBackground() {
- setProgress(0);
- BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- bi.setRGB(x, y, (int)(Math.random()*-16777217));
- }
- }
- //for (int n = 0; n < 10; n++) {
- int iterations = Integer.parseInt(iterField.getText());
- for (int i = 0; i < iterations; i++) {
- bi = smooth(bi);
- pic.setIcon(new ImageIcon(bi));
- setProgress((int)(i*100f/iterations+.5f));
- }
- trySave(bi);
- //}
- setProgress(100);
- return null;
- }
- /*
- * Executed in event dispatching thread
- */
- @Override
- public void done() {
- Toolkit.getDefaultToolkit().beep();
- startButton.setEnabled(true);
- setCursor(null); //turn off the wait cursor
- }
- }
- public void actionPerformed(ActionEvent evt) {
- if (evt.getSource() == startButton) {
- startButton.setEnabled(false);
- setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
- //Instances of javax.swing.SwingWorker are not reusuable, so
- //we create new instances as needed.
- task = new Task();
- task.addPropertyChangeListener(this);
- task.execute();
- }
- }
- public void propertyChange(PropertyChangeEvent evt) {
- if ("progress" == evt.getPropertyName()) {
- int progress = (Integer) evt.getNewValue();
- progressBar.setValue(progress);
- }
- }
- public static BufferedImage smooth(BufferedImage bi) {
- BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- img.setRGB(x, y, getAverageColor(x, y, bi));
- }
- }
- return img;
- }
- public static int getAverageRGB(int x, int y, BufferedImage template) {
- int n = 0;
- int d = 0;
- if (x > 0) {
- n+=template.getRGB(x-1, y);
- d++;
- }
- if (y > 0) {
- n+=template.getRGB(x, y-1);
- d++;
- }
- if (x < template.getWidth()-1) {
- n+=template.getRGB(x+1, y);
- d++;
- }
- if (y < template.getHeight()-1) {
- n+=template.getRGB(x, y+1);
- d++;
- }
- if (x > 0 && y > 0) {
- n+=template.getRGB(x-1, y-1);
- d++;
- }
- if (y > 0 && x < template.getWidth()-1) {
- n+=template.getRGB(x+1, y-1);
- d++;
- }
- if (x < template.getWidth()-1 && y < template.getHeight()-1) {
- n+=template.getRGB(x+1, y+1);
- d++;
- }
- if (y < template.getHeight()-1 && x > 0) {
- n+=template.getRGB(x-1, y+1);
- d++;
- }
- return (int)((n*1f/d)+.5f);
- }
- public static int getAverageColor(int x, int y, BufferedImage template) {
- int nRed = 0;
- int nGreen = 0;
- int nBlue = 0;
- int d = 0;
- if (x > 0) {
- Color col = new Color(template.getRGB(x-1, y));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (y > 0) {
- Color col = new Color(template.getRGB(x, y-1));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (x < template.getWidth()-1) {
- Color col = new Color(template.getRGB(x+1, y));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (y < template.getHeight()-1) {
- Color col = new Color(template.getRGB(x, y+1));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (x > 0 && y > 0) {
- Color col = new Color(template.getRGB(x-1, y-1));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (y > 0 && x < template.getWidth()-1) {
- Color col = new Color(template.getRGB(x+1, y-1));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (x < template.getWidth()-1 && y < template.getHeight()-1) {
- Color col = new Color(template.getRGB(x+1, y+1));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- if (y < template.getHeight()-1 && x > 0) {
- Color col = new Color(template.getRGB(x-1, y+1));
- nRed += col.getRed();
- nGreen += col.getGreen();
- nBlue += col.getBlue();
- d++;
- }
- return new Color((int)((nRed*1f/d)+.5f), (int)((nGreen*1f/d)+.5f), (int)((nBlue*1f/d)+.5f)).getRGB();
- }
- public Main() {
- super(new BorderLayout());
- Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
- if (w == 0 && h == 0) {
- w = (int)screen.getWidth();
- h = (int)screen.getHeight();
- }
- System.out.println(w + ":" + h);
- fc = new JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "png", "jpg", "jpeg", "bmp");
- fc.setFileFilter(filter);
- //Create the UI.
- pic = new JLabel();
- startButton = new JButton("Start");
- startButton.setActionCommand("start");
- startButton.addActionListener(this);
- progressBar = new JProgressBar(0, 100);
- progressBar.setValue(0);
- progressBar.setStringPainted(true);
- iterLabel = new JLabel("Iterations:");
- iterField = new JTextField("100");
- iterField.addActionListener(this);
- JPanel panel = new JPanel();
- panel.add(startButton);
- panel.add(progressBar);
- panel.add(iterLabel);
- panel.add(iterField);
- panel.add(pic);
- add(panel, BorderLayout.PAGE_START);
- setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
- }
- private static void createAndShowGUI() {
- //Create and set up the window.
- JFrame frame = new JFrame("Smooth Image");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Create and set up the content pane.
- JComponent newContentPane = new Main();
- newContentPane.setOpaque(true); //content panes must be opaque
- frame.setContentPane(newContentPane);
- //Display the window.
- frame.pack();
- frame.setVisible(true);
- }
- public void trySave(BufferedImage bi) {
- int returnVal = fc.showSaveDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File currentFile = fc.getSelectedFile();
- String filePath = currentFile.getPath();
- if (filePath.lastIndexOf(".") != -1) {
- filePath = currentFile.getPath().substring(0, filePath.lastIndexOf("."));
- }
- filePath = filePath + ".png";
- try {
- ImageIO.write(bi, "png", new File(filePath));
- } catch(IOException e) {
- }
- }
- }
- public static void main(String[] args) {
- //Schedule a job for the event-dispatching thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement