Skip to content

Commit 8627661

Browse files
authored
Merge pull request #6 from romankh3/refactoring
Refactoring
2 parents 1a85455 + be4760d commit 8627661

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

src/main/java/ua/comparison/image/ImageComparison.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public static void main( String[] args ) throws IOException, URISyntaxException
2626
// Draw rectangles on the image.
2727
BufferedImage drawnDifferences = drawTheDifference( "image1.png", "image2.png" );
2828

29-
// make dir if it's not using from Gradle.
30-
new File( "build" );
31-
ImageIO.write( drawnDifferences, "png", new File( "build/result.png" ) );
29+
saveImage( "build/result.png", drawnDifferences );
3230

3331
createGUI( drawnDifferences );
3432
}

src/main/java/ua/comparison/image/ImageComparisonTools.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ public class ImageComparisonTools {
2020
* @param image resulting image.
2121
*/
2222
public static Frame createGUI(BufferedImage image ) {
23-
JFrame frame = new JFrame("The result of the comparison");
23+
JFrame frame = new JFrame("The result of the comparison" );
2424
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
2525
JLabel label = new JLabel();
26-
label.setIcon(new ImageIcon(image, "Result"));
26+
label.setIcon( new ImageIcon( image, "Result") );
2727
frame.getContentPane().add(label, BorderLayout.CENTER);
28-
frame.setPreferredSize(new Dimension( (image.getWidth() ), image.getHeight() ) );
28+
frame.setPreferredSize(new Dimension( image.getWidth(), ( int )( image.getHeight() * 1.1 ) ) );
2929
frame.pack();
30-
frame.setLocationRelativeTo(null);
31-
frame.setVisible(true);
30+
frame.setLocationRelativeTo( null );
31+
frame.setVisible( true );
3232
return frame;
3333
}
3434

35-
static BufferedImage deepCopy( BufferedImage iamge ) {
36-
ColorModel cm = iamge.getColorModel();
35+
static BufferedImage deepCopy( BufferedImage image ) {
36+
ColorModel cm = image.getColorModel();
3737
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
38-
WritableRaster raster = iamge.copyData(null);
38+
WritableRaster raster = image.copyData(null);
3939
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
4040
}
4141

@@ -85,4 +85,16 @@ public static boolean isDifferent( int x, int y, BufferedImage image1, BufferedI
8585
public static BufferedImage readImageFromResources( String path ) throws IOException, URISyntaxException {
8686
return ImageIO.read( new File( ImageComparison.class.getClassLoader().getResource ( path ).toURI().getPath() ) );
8787
}
88+
89+
/**
90+
* Save image to the provided path.
91+
* @param path the path to the saving image.
92+
* @param image the {@code BufferedImage} object of this specific image.
93+
* @throws IOException
94+
*/
95+
public static void saveImage(String path, BufferedImage image ) throws IOException {
96+
// make dir if it's not using from Gradle.
97+
new File( path ).mkdirs();
98+
ImageIO.write( image, "png", new File( path ) );
99+
}
88100
}

src/test/java/ua/comparison/image/ImageComparisonToolsUnitTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import static ua.comparison.image.ImageComparisonTools.createGUI;
55
import static ua.comparison.image.ImageComparisonTools.readImageFromResources;
66

7+
import org.junit.Assert;
78
import org.junit.Test;
89

910
import java.awt.*;
1011
import java.awt.image.BufferedImage;
12+
import java.io.File;
1113
import java.io.IOException;
1214
import java.net.URISyntaxException;
1315

@@ -20,7 +22,23 @@ public class ImageComparisonToolsUnitTest {
2022
public void testFrameMethod() throws IOException, URISyntaxException {
2123
BufferedImage image = readImageFromResources( "result.png" );
2224
Frame resultFrame = createGUI( image );
23-
assertEquals( resultFrame.getHeight(), image.getHeight() );
25+
assertEquals( resultFrame.getHeight(), ( int ) ( image.getHeight() * 1.1 ) );
2426
assertEquals( resultFrame.getWidth(), image.getWidth() );
2527
}
28+
29+
@Test( expected = IllegalArgumentException.class )
30+
public void testCheckCorrectImageSize() {
31+
BufferedImage image1 = new BufferedImage(10, 10, 10);
32+
BufferedImage image2 = new BufferedImage(12, 12, 10);
33+
34+
ImageComparisonTools.checkCorrectImageSize( image1, image2 );
35+
}
36+
37+
@Test
38+
public void testSaveImage() throws IOException, URISyntaxException {
39+
BufferedImage image = readImageFromResources( "result.png" );
40+
String path = "build/test/correct/save/image.png";
41+
ImageComparisonTools.saveImage( path, image );
42+
Assert.assertTrue( new File( path ).exists() );
43+
}
2644
}

0 commit comments

Comments
 (0)