package info.seravee.ui;

import info.seravee.data.ScalingAlgorithm;
import info.seravee.utils.ImageScalerUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by ysaak on 27/01/15.
 */
class ImageDisplayer extends JComponent {

    private ScalingAlgorithm scalingAlgorithm = ScalingAlgorithm.CENTER;
    private BufferedImage image = null;

    public ImageDisplayer() {
        //
    }

    public void setImage(File imageFile) {
        try {
            image = ImageIO.read(imageFile);
        }
        catch (IOException e) {
            e.printStackTrace();
            image = null;
        }
        repaintLater();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 =(Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.clearRect(0, 0, getWidth(), getHeight());

        g2.setColor(Color.CYAN);
        g2.fillRect(0, 0, getWidth(), getHeight());

        if (image != null) {

            Dimension scaledDimension = ImageScalerUtils.getScaledImage(scalingAlgorithm, new Dimension(image.getWidth(), image.getHeight()), getSize());
            Image scaled = image.getScaledInstance(scaledDimension.width, scaledDimension.height, Image.SCALE_FAST);

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            int x = (width - scaled.getWidth(this)) / 2;
            int y = (height - scaled.getHeight(this)) / 2;

            g2.drawImage(scaled, x, y, this);
        }

        super.paintComponent(g);
    }

    public void setScalingAlgorithm(ScalingAlgorithm image1Size) {
        this.scalingAlgorithm = image1Size;
        repaintLater();
    }

    private void repaintLater() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImageDisplayer.this.repaint();
            }
        });
    }
}

@@ -461,16 +461,54 @@
  = null;
+%0A    private Image scaledImage = null;
 %0A%0A    pu
@@ -650,16 +650,45 @@
 geFile);
+%0A%0A            rebuildImage();
 %0A       
@@ -793,33 +793,41 @@
     
-%7D%0A        repaintLater();
+    scaledImage = null;%0A        %7D
 %0A   
@@ -1185,297 +1185,30 @@
 if (
-image != null) %7B%0A%0A            Dimension scaledDimension = ImageScalerUtils.getScaledImage(scalingAlgorithm, new Dimension(image.getWidth(), image.getHeight()), getSize());%0A            Image scaled = image.getScaledInstance(scaledDimension.width, scaledDimension.height, Image.SCALE_FAST);%0A
+scaledImage != null) %7B
 %0A   
@@ -1318,24 +1318,29 @@
 dth - scaled
+Image
 .getWidth(th
@@ -1385,16 +1385,21 @@
 - scaled
+Image
 .getHeig
@@ -1445,16 +1445,21 @@
 e(scaled
+Image
 , x, y, 
@@ -1637,26 +1637,26 @@
       re
-paintLater
+buildImage
 ();%0A    
@@ -1681,23 +1681,403 @@
 d re
-paintLater() %7B%0A
+buildImage() %7B%0A%0A        new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Dimension scaledDimension = ImageScalerUtils.getScaledImage(scalingAlgorithm, new Dimension(image.getWidth(), image.getHeight()), getSize());%0A                scaledImage = image.getScaledInstance(scaledDimension.width, scaledDimension.height, Image.SCALE_SMOOTH);%0A%0A        
     
@@ -2128,32 +2128,40 @@
 ) %7B%0A            
+   
+     
 @Override%0A      
@@ -2158,32 +2158,40 @@
 ide%0A            
+       
+ 
 public void run(
@@ -2186,32 +2186,40 @@
 ic void run() %7B%0A
+        
                 
@@ -2245,24 +2245,66 @@
 .repaint();%0A
+                    %7D%0A                %7D);%0A
             
@@ -2311,22 +2311,30 @@
 %7D%0A        %7D)
+.start()
 ;%0A    %7D%0A%7D%0A

Optimize painting by resizing in another thread