package ui;

import rgb.InvertRGBStrategy;
import rgb.RGBStrategy;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by nick on 10/22/15.
 */
public class RGBPanel extends JPanel {

    private BufferedImage image;
    private RGBStrategy converter;

    public RGBPanel(){
        this(new InvertRGBStrategy());
    }

    public RGBPanel(RGBStrategy converter){
        super();
        this.converter = converter;
        setPreferredSize(new Dimension(600, 400));
        image = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);
    }

    public void setRGBStrategy(RGBStrategy strategy){
        this.converter = strategy;
    }

    public void convert(){
        for(int i = 0; i < image.getWidth(); i++){
            for(int j = 0; j < image.getHeight(); j++){
                Color color = new Color(image.getRGB(i, j));
                color = converter.convert(color);
                image.setRGB(i, j, color.getRGB());
            }
        }
        invalidate();
        repaint();
    }

    public void setImage(BufferedImage img){
        this.image = img;
        setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
        invalidate();
        repaint();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }

}

@@ -220,16 +220,57 @@
 anel %7B%0A%0A
+    private BufferedImage originalImage;%0A
     priv
@@ -622,16 +622,47 @@
 E_BGR);%0A
+        originalImage = image;%0A
     %7D%0A%0A 
@@ -779,24 +779,143 @@
  convert()%7B%0A
+        BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);%0A
         for(
@@ -929,17 +929,25 @@
  0; i %3C 
-i
+originalI
 mage.get
@@ -992,17 +992,25 @@
  0; j %3C 
-i
+originalI
 mage.get
@@ -1065,17 +1065,25 @@
 w Color(
-i
+originalI
 mage.get
@@ -1156,25 +1156,28 @@
             
-i
+newI
 mage.setRGB(
@@ -1215,32 +1215,58 @@
     %7D%0A        %7D%0A
+        image = newImage;%0A
         invalida
@@ -1338,24 +1338,58 @@
 Image img)%7B%0A
+        this.originalImage = img;%0A
         this

Reset image when choosing new filter