Commits

Rafał Selewońko committed e505c8b

dodawanie i odejmowanie obrazów

Comments (0)

Files changed (5)

Binary file modified.

src/selershop/Plotno.java

 import java.awt.image.BufferedImage;
 import java.awt.image.ByteLookupTable;
 import java.awt.image.ColorConvertOp;
+import java.awt.image.ColorModel;
 import java.awt.image.ConvolveOp;
 import java.awt.image.Kernel;
 import java.awt.image.LookupOp;
  */
 public class Plotno extends javax.swing.JPanel {
     File plik;
+    File drugiplik;
     BufferedImage obrazek;
+    BufferedImage drugiobrazek;
     int typ;
+    int drugityp;
     int[] histogram = new int[256];
     int[] histogramR = new int[256];
     int[] histogramG = new int[256];
     /** Creates new form Plotno */
     public Plotno() {
         obrazek = null;
+        drugiobrazek = null;
         plik = null;
+        drugiplik = null;
         setSize(800, 800);
         initComponents();
     }
         repaint();
     }
     
+    public void OtworzDrugi() throws IOException{
+            JFileChooser chooser = new JFileChooser();
+            FileNameExtensionFilter filtr = new FileNameExtensionFilter("Pliki *.jpg *.png","jpg", "jpeg", "png");
+            chooser.setFileFilter(filtr);
+            int wynik = chooser.showOpenDialog(this);        
+            if (wynik == JFileChooser.APPROVE_OPTION) {
+                drugiplik = chooser.getSelectedFile();
+                drugiobrazek = ImageIO.read(drugiplik);
+            }
+    }
+    
     public void Resetuj() throws IOException
     {
         obrazek = ImageIO.read(plik);
         obrazek =  toCreate.getAsBufferedImage();
         repaint();
     }
+    
+    public void DwaObrazy(boolean operacja){
+        
+        ColorModel cm = obrazek.getColorModel();
+        int w1 = obrazek.getWidth();
+        int h1 = obrazek.getHeight();
+        int w2 = drugiobrazek.getWidth();
+        int h2 = drugiobrazek.getHeight();
+        int width = w1;
+        int height = h1;
+        if (w2 < w1) width = w2;
+        if (h2 < h1) height = h2;
+        Color c1, c2;
+        
+        BufferedImage tempobraz = new BufferedImage(cm, cm.createCompatibleWritableRaster(width, height), cm.isAlphaPremultiplied(), null);
+
+        int r=0, g=0, b=0;
+        for(int i = 0; i < height; i++)
+            for(int j = 0; j < width; j++){
+                c1 = new Color(obrazek.getRGB(j, i));
+                c2 = new Color(drugiobrazek.getRGB(j, i));
+                if(operacja){
+                    r = DodajKolory(c1.getRed(), c2.getRed());
+                    g = DodajKolory(c1.getGreen(), c2.getGreen());
+                    b = DodajKolory(c1.getBlue(), c2.getBlue());
+                } else {
+                    r = OdejmijKolory(c1.getRed(), c2.getRed());
+                    g = OdejmijKolory(c1.getGreen(), c2.getGreen());
+                    b = OdejmijKolory(c1.getBlue(), c2.getBlue());
+                }
+                
+                tempobraz.setRGB(j, i, new Color(r,g,b,255).getRGB());
+            }
+        obrazek = tempobraz;
+        repaint();
+    }
+    
+    public void OdejmijObrazy(){
+        
+    }
+    
+    private int DodajKolory(int c1, int c2){
+        int c3 = c1+c2;
+        if(c3>255)c3=255;
+        return c3;
+    }
+    private int OdejmijKolory(int c1, int c2){
+        int c3 = c1-c2;
+        if(c3>255)c3=0;
+        return c3;
+    }
+    
 }

src/selershop/SelerShopView.form

             </MenuItem>
           </SubComponents>
         </Menu>
+        <Menu class="javax.swing.JMenu" name="jMenu5">
+          <Properties>
+            <Property name="text" type="java.lang.String" resourceKey="jMenu5.text"/>
+            <Property name="name" type="java.lang.String" value="jMenu5" noResource="true"/>
+          </Properties>
+          <SubComponents>
+            <MenuItem class="javax.swing.JMenuItem" name="dodajobraz">
+              <Properties>
+                <Property name="text" type="java.lang.String" resourceKey="dodajobraz.text"/>
+                <Property name="name" type="java.lang.String" value="dodajobraz" noResource="true"/>
+              </Properties>
+              <Events>
+                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="dodajobrazActionPerformed"/>
+              </Events>
+            </MenuItem>
+            <MenuItem class="javax.swing.JMenuItem" name="odejmijobraz">
+              <Properties>
+                <Property name="text" type="java.lang.String" resourceKey="odejmijobraz.text"/>
+                <Property name="name" type="java.lang.String" value="odejmijobraz" noResource="true"/>
+              </Properties>
+              <Events>
+                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="odejmijobrazActionPerformed"/>
+              </Events>
+            </MenuItem>
+          </SubComponents>
+        </Menu>
         <Menu class="javax.swing.JMenu" name="helpMenu">
           <Properties>
             <Property name="text" type="java.lang.String" resourceKey="helpMenu.text"/>

src/selershop/SelerShopView.java

         wyrownanie = new javax.swing.JMenuItem();
         binaryzacja = new javax.swing.JMenuItem();
         obliczHistogram = new javax.swing.JMenuItem();
+        jMenu5 = new javax.swing.JMenu();
+        dodajobraz = new javax.swing.JMenuItem();
+        odejmijobraz = new javax.swing.JMenuItem();
         javax.swing.JMenu helpMenu = new javax.swing.JMenu();
         javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
         statusPanel = new javax.swing.JPanel();
 
         menuBar.add(jMenu4);
 
+        jMenu5.setText(resourceMap.getString("jMenu5.text")); // NOI18N
+        jMenu5.setName("jMenu5"); // NOI18N
+
+        dodajobraz.setText(resourceMap.getString("dodajobraz.text")); // NOI18N
+        dodajobraz.setName("dodajobraz"); // NOI18N
+        dodajobraz.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                dodajobrazActionPerformed(evt);
+            }
+        });
+        jMenu5.add(dodajobraz);
+
+        odejmijobraz.setText(resourceMap.getString("odejmijobraz.text")); // NOI18N
+        odejmijobraz.setName("odejmijobraz"); // NOI18N
+        odejmijobraz.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                odejmijobrazActionPerformed(evt);
+            }
+        });
+        jMenu5.add(odejmijobraz);
+
+        menuBar.add(jMenu5);
+
         helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
         helpMenu.setName("helpMenu"); // NOI18N
 
         ustawInputy();
     }//GEN-LAST:event_rozmiarYSpinnerStateChanged
 
+    private void dodajobrazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dodajobrazActionPerformed
+        try {
+            plotno.OtworzDrugi();
+        } catch (IOException ex) {
+            Logger.getLogger(SelerShopView.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        if(plotno.drugiplik != null){
+            plotno.DwaObrazy(true); // true to dodawanie, false - odejmowanie
+        }
+    }//GEN-LAST:event_dodajobrazActionPerformed
+
+    private void odejmijobrazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odejmijobrazActionPerformed
+        try {
+            plotno.OtworzDrugi();
+        } catch (IOException ex) {
+            Logger.getLogger(SelerShopView.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        if(plotno.drugiplik != null){
+            plotno.DwaObrazy(false); // true to dodawanie, false - odejmowanie
+        }
+    }//GEN-LAST:event_odejmijobrazActionPerformed
+
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JMenuItem binaryzacja;
+    private javax.swing.JMenuItem dodajobraz;
     private javax.swing.JMenuItem dodawanie;
     private javax.swing.JMenuItem dylatacja;
     private javax.swing.JMenuItem erozja;
     private javax.swing.JMenu jMenu2;
     private javax.swing.JMenu jMenu3;
     private javax.swing.JMenu jMenu4;
+    private javax.swing.JMenu jMenu5;
     private javax.swing.JPanel jPanel1;
     private javax.swing.JScrollPane jScrollPane;
     private javax.swing.JPopupMenu.Separator jSeparator1;
     private javax.swing.JMenuBar menuBar;
     private javax.swing.JMenuItem mnozenie;
     private javax.swing.JMenuItem obliczHistogram;
+    private javax.swing.JMenuItem odejmijobraz;
     private javax.swing.JMenuItem otwarcie;
     private javax.swing.JMenuItem otworz;
     private javax.swing.JPanel panelMacierzy;

src/selershop/resources/SelerShopView.properties

 macierz61.text=1
 macierz62.text=1
 macierz63.text=1
+jMenu5.text=Obraz
+dodajobraz.text=Dodaj
+odejmijobraz.text=Odejmij