1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.jemmy.image; 24 25 import java.awt.BorderLayout; 26 import java.awt.Dimension; 27 import java.awt.Graphics; 28 import java.awt.Toolkit; 29 import java.awt.event.ComponentEvent; 30 import java.awt.event.ComponentListener; 31 import java.awt.image.BufferedImage; 32 import javax.swing.JDialog; 33 import javax.swing.JLabel; 34 import javax.swing.JPanel; 35 36 /** 37 * 38 * @author shura 39 */ 40 public class DiffDialog extends javax.swing.JDialog { 41 42 private final static StrictImageComparator comparator = new StrictImageComparator(); 43 private double scale = 1.0; 44 private int imageWidth, imageHeight, scaledWidth, scaledHeight; 45 private ImagePane left = null, right = null, diff = null; 46 int status = 0; 47 48 /** Creates new form ImageDiff */ 49 DiffDialog() { 50 super((JDialog)null, true); 51 initComponents(); 52 leftPane.setLayout(new BorderLayout()); 53 leftPane.add(new JLabel("Golden"), BorderLayout.NORTH); 54 rightPane.setLayout(new BorderLayout()); 55 rightPane.add(new JLabel("Result"), BorderLayout.NORTH); 56 diffPane.setLayout(new BorderLayout()); 57 diffPane.add(new JLabel("Diff"), BorderLayout.NORTH); 58 getContentPane().addComponentListener(new ComponentListener() { 59 60 public void componentResized(ComponentEvent e) { 61 lrSplit.setDividerLocation(.5); 62 dcSplit.setDividerLocation(.5); 63 tbSplit.setDividerLocation(.5); 64 } 65 66 public void componentMoved(ComponentEvent e) { 67 throw new UnsupportedOperationException("Not supported yet."); 68 } 69 70 public void componentShown(ComponentEvent e) { 71 throw new UnsupportedOperationException("Not supported yet."); 72 } 73 74 public void componentHidden(ComponentEvent e) { 75 throw new UnsupportedOperationException("Not supported yet."); 76 } 77 }); 78 setSize(400, 300); 79 80 // Get the size of the screen 81 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 82 83 // Determine the new location of the window 84 int w = getSize().width; 85 int h = getSize().height; 86 int x = (dim.width - w) / 2; 87 int y = (dim.height - h) / 2; 88 89 // Move the window 90 setLocation(x, y); 91 } 92 93 void setImages(BufferedImage leftImage, BufferedImage rightImage) { 94 if (leftImage != null && rightImage != null) { 95 copyBtn.setEnabled(true); 96 removeBtn.setEnabled(false); 97 imageWidth = leftImage.getWidth(); 98 imageHeight = leftImage.getHeight(); 99 } else { 100 if (leftImage == null) { 101 copyBtn.setEnabled(true); 102 removeBtn.setEnabled(false); 103 imageWidth = rightImage.getWidth(); 104 imageHeight = rightImage.getHeight(); 105 } else if (rightImage == null) { 106 copyBtn.setEnabled(false); 107 removeBtn.setEnabled(true); 108 imageWidth = leftImage.getWidth(); 109 imageHeight = leftImage.getHeight(); 110 } 111 } 112 if (left == null) { 113 left = new ImagePane(leftImage); 114 } else { 115 left.setImage(leftImage); 116 } 117 leftPane.add(left, BorderLayout.CENTER); 118 if (right == null) { 119 right = new ImagePane(rightImage); 120 } else { 121 right.setImage(rightImage); 122 } 123 rightPane.add(right, BorderLayout.CENTER); 124 if (diff == null) { 125 diff = new ImagePane(subtract(leftImage, rightImage)); 126 } else { 127 diff.setImage(subtract(leftImage, rightImage)); 128 } 129 diffPane.add(diff, BorderLayout.CENTER); 130 rescaleAll(); 131 } 132 133 private BufferedImage subtract(BufferedImage left, BufferedImage right) { 134 if(left != null && right != null) { 135 return ImageTool.subtractImage(left, right); 136 } else { 137 return null; 138 } 139 } 140 141 /** This method is called from within the constructor to 142 * initialize the form. 143 * WARNING: Do NOT modify this code. The content of this method is 144 * always regenerated by the Form Editor. 145 */ 146 @SuppressWarnings("unchecked") 147 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 148 private void initComponents() { 149 150 tbSplit = new javax.swing.JSplitPane(); 151 lrSplit = new javax.swing.JSplitPane(); 152 leftPane = new javax.swing.JPanel(); 153 rightPane = new javax.swing.JPanel(); 154 dcSplit = new javax.swing.JSplitPane(); 155 diffPane = new javax.swing.JPanel(); 156 controlPane = new javax.swing.JPanel(); 157 jButton1 = new javax.swing.JButton(); 158 jButton2 = new javax.swing.JButton(); 159 copyBtn = new javax.swing.JButton(); 160 removeBtn = new javax.swing.JButton(); 161 jButton6 = new javax.swing.JButton(); 162 jButton7 = new javax.swing.JButton(); 163 164 tbSplit.setDividerLocation(200); 165 tbSplit.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); 166 167 lrSplit.setDividerLocation(250); 168 169 javax.swing.GroupLayout leftPaneLayout = new javax.swing.GroupLayout(leftPane); 170 leftPane.setLayout(leftPaneLayout); 171 leftPaneLayout.setHorizontalGroup( 172 leftPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 173 .addGap(0, 250, Short.MAX_VALUE) 174 ); 175 leftPaneLayout.setVerticalGroup( 176 leftPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 177 .addGap(0, 200, Short.MAX_VALUE) 178 ); 179 180 lrSplit.setLeftComponent(leftPane); 181 182 javax.swing.GroupLayout rightPaneLayout = new javax.swing.GroupLayout(rightPane); 183 rightPane.setLayout(rightPaneLayout); 184 rightPaneLayout.setHorizontalGroup( 185 rightPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 186 .addGap(0, 293, Short.MAX_VALUE) 187 ); 188 rightPaneLayout.setVerticalGroup( 189 rightPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 190 .addGap(0, 200, Short.MAX_VALUE) 191 ); 192 193 lrSplit.setRightComponent(rightPane); 194 195 tbSplit.setTopComponent(lrSplit); 196 197 javax.swing.GroupLayout diffPaneLayout = new javax.swing.GroupLayout(diffPane); 198 diffPane.setLayout(diffPaneLayout); 199 diffPaneLayout.setHorizontalGroup( 200 diffPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 201 .addGap(0, 100, Short.MAX_VALUE) 202 ); 203 diffPaneLayout.setVerticalGroup( 204 diffPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 205 .addGap(0, 255, Short.MAX_VALUE) 206 ); 207 208 dcSplit.setLeftComponent(diffPane); 209 210 jButton1.setMnemonic('+'); 211 jButton1.setText("+"); 212 jButton1.addActionListener(new java.awt.event.ActionListener() { 213 public void actionPerformed(java.awt.event.ActionEvent evt) { 214 jButton1ActionPerformed(evt); 215 } 216 }); 217 218 jButton2.setMnemonic('-'); 219 jButton2.setText("-"); 220 jButton2.addActionListener(new java.awt.event.ActionListener() { 221 public void actionPerformed(java.awt.event.ActionEvent evt) { 222 jButton2ActionPerformed(evt); 223 } 224 }); 225 226 copyBtn.setText("Copy to golgen"); 227 copyBtn.setEnabled(false); 228 copyBtn.addActionListener(new java.awt.event.ActionListener() { 229 public void actionPerformed(java.awt.event.ActionEvent evt) { 230 copyBtnActionPerformed(evt); 231 } 232 }); 233 234 removeBtn.setText("Remove from golden"); 235 removeBtn.setEnabled(false); 236 removeBtn.addActionListener(new java.awt.event.ActionListener() { 237 public void actionPerformed(java.awt.event.ActionEvent evt) { 238 removeBtnActionPerformed(evt); 239 } 240 }); 241 242 jButton6.setText("Next"); 243 jButton6.addActionListener(new java.awt.event.ActionListener() { 244 public void actionPerformed(java.awt.event.ActionEvent evt) { 245 jButton6ActionPerformed(evt); 246 } 247 }); 248 249 jButton7.setText("Exit"); 250 jButton7.addActionListener(new java.awt.event.ActionListener() { 251 public void actionPerformed(java.awt.event.ActionEvent evt) { 252 jButton7ActionPerformed(evt); 253 } 254 }); 255 256 javax.swing.GroupLayout controlPaneLayout = new javax.swing.GroupLayout(controlPane); 257 controlPane.setLayout(controlPaneLayout); 258 controlPaneLayout.setHorizontalGroup( 259 controlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 260 .addGroup(controlPaneLayout.createSequentialGroup() 261 .addContainerGap(289, Short.MAX_VALUE) 262 .addGroup(controlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 263 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlPaneLayout.createSequentialGroup() 264 .addComponent(jButton6) 265 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 266 .addComponent(jButton7)) 267 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlPaneLayout.createSequentialGroup() 268 .addComponent(jButton1) 269 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 270 .addComponent(jButton2)) 271 .addComponent(copyBtn, javax.swing.GroupLayout.Alignment.TRAILING) 272 .addComponent(removeBtn, javax.swing.GroupLayout.Alignment.TRAILING)) 273 .addContainerGap()) 274 ); 275 controlPaneLayout.setVerticalGroup( 276 controlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 277 .addGroup(controlPaneLayout.createSequentialGroup() 278 .addContainerGap() 279 .addGroup(controlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 280 .addComponent(jButton1) 281 .addComponent(jButton2)) 282 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 283 .addComponent(copyBtn) 284 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 285 .addComponent(removeBtn) 286 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE) 287 .addGroup(controlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 288 .addComponent(jButton7) 289 .addComponent(jButton6)) 290 .addContainerGap()) 291 ); 292 293 dcSplit.setRightComponent(controlPane); 294 295 tbSplit.setRightComponent(dcSplit); 296 297 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 298 getContentPane().setLayout(layout); 299 layout.setHorizontalGroup( 300 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 301 .addComponent(tbSplit, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE) 302 ); 303 layout.setVerticalGroup( 304 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 305 .addComponent(tbSplit, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE) 306 ); 307 308 pack(); 309 }// </editor-fold>//GEN-END:initComponents 310 311 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed 312 scale *= .9; 313 rescaleAll(); 314 }//GEN-LAST:event_jButton2ActionPerformed 315 316 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed 317 scale *= 1.1; 318 rescaleAll(); 319 }//GEN-LAST:event_jButton1ActionPerformed 320 321 private void rescaleAll() { 322 scaledWidth = (int) (imageWidth * scale); 323 scaledHeight = (int) (imageHeight * scale); 324 left.reScale(); 325 right.reScale(); 326 diff.reScale(); 327 getContentPane().repaint(); 328 } 329 330 private void copyBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyBtnActionPerformed 331 status = -1; 332 setVisible(false); 333 }//GEN-LAST:event_copyBtnActionPerformed 334 335 private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeBtnActionPerformed 336 status = 1; 337 setVisible(false); 338 }//GEN-LAST:event_removeBtnActionPerformed 339 340 private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed 341 setVisible(false); 342 }//GEN-LAST:event_jButton6ActionPerformed 343 344 private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed 345 status = -2; 346 setVisible(false); 347 }//GEN-LAST:event_jButton7ActionPerformed 348 349 private class ImagePane extends JPanel { 350 351 BufferedImage img; 352 java.awt.Image scaled; 353 354 public ImagePane(BufferedImage img) { 355 this.img = img; 356 } 357 358 @Override 359 protected void paintComponent(Graphics g) { 360 if (img != null) { 361 g.drawImage(scaled, 0, 0, this); 362 } else { 363 super.paintComponent(g); 364 } 365 } 366 367 void setImage(BufferedImage img) { 368 this.img = img; 369 reScale(); 370 } 371 372 void reScale() { 373 if (img != null) { 374 scaled = img.getScaledInstance(scaledWidth, scaledHeight, java.awt.Image.SCALE_DEFAULT); 375 } 376 } 377 } 378 // Variables declaration - do not modify//GEN-BEGIN:variables 379 private javax.swing.JPanel controlPane; 380 private javax.swing.JButton copyBtn; 381 private javax.swing.JSplitPane dcSplit; 382 private javax.swing.JPanel diffPane; 383 private javax.swing.JButton jButton1; 384 private javax.swing.JButton jButton2; 385 private javax.swing.JButton jButton6; 386 private javax.swing.JButton jButton7; 387 private javax.swing.JPanel leftPane; 388 private javax.swing.JSplitPane lrSplit; 389 private javax.swing.JButton removeBtn; 390 private javax.swing.JPanel rightPane; 391 private javax.swing.JSplitPane tbSplit; 392 // End of variables declaration//GEN-END:variables 393 }