Permalink
Browse files

Code cleanup.

- Reformat code with IntelliJ.
- Fix warnings.
  • Loading branch information...
1 parent febf613 commit 114f2bcf8814b9b466ad18b6e77accc7ab58fc6a @avh4 committed Jun 17, 2012
@@ -9,63 +9,63 @@
public class ImageDiff extends JPanel implements MouseListener {
- private static final long serialVersionUID = 1L;
- private final ImageDiffView view;
+ private static final long serialVersionUID = 1L;
+ private final ImageDiffView view;
- public ImageDiff(final String file1, final String file2) throws IOException {
+ public ImageDiff(final String file1, final String file2) throws IOException {
setBackground(new Color(232, 232, 232));
- final FlowLayout layout = (FlowLayout) getLayout();
- layout.setHgap(0);
- layout.setVgap(0);
- addMouseListener(this);
- view = new ImageDiffView(file1, file2);
- add(view);
- }
+ final FlowLayout layout = (FlowLayout) getLayout();
+ layout.setHgap(0);
+ layout.setVgap(0);
+ addMouseListener(this);
+ view = new ImageDiffView(file1, file2);
+ add(view);
+ }
- public static void main(final String[] args) {
- final JFrame window = new JFrame();
- final ImageDiff ui = launch(args[0], args[1]);
- window.add(ui);
- window.pack();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setLocationRelativeTo(null);
- window.setVisible(true);
- }
+ public static void main(final String[] args) {
+ final JFrame window = new JFrame();
+ final ImageDiff ui = launch(args[0], args[1]);
+ window.add(ui);
+ window.pack();
+ window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+ window.setLocationRelativeTo(null);
+ window.setVisible(true);
+ }
- public static ImageDiff launch(final String file1, final String file2) {
- return launch(new File("."), file1, file2);
- }
+ public static ImageDiff launch(final String file1, final String file2) {
+ return launch(new File("."), file1, file2);
+ }
- public static ImageDiff launch(final File root, final String file1,
- final String file2) {
- final String filename1 = new File(root, file1).getPath();
- final String filename2 = new File(root, file2).getPath();
- try {
- return new ImageDiff(filename1, filename2);
- } catch (final IOException e) {
- e.printStackTrace();
- }
- return null;
- }
+ public static ImageDiff launch(final File root, final String file1,
+ final String file2) {
+ final String filename1 = new File(root, file1).getPath();
+ final String filename2 = new File(root, file2).getPath();
+ try {
+ return new ImageDiff(filename1, filename2);
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
- @Override
- public void mouseClicked(final MouseEvent e) {
- view.setShowHighlight(false);
- }
+ @Override
+ public void mouseClicked(final MouseEvent e) {
+ view.setShowHighlight(false);
+ }
- @Override
- public void mousePressed(final MouseEvent e) {
- }
+ @Override
+ public void mousePressed(final MouseEvent e) {
+ }
- @Override
- public void mouseReleased(final MouseEvent e) {
- }
+ @Override
+ public void mouseReleased(final MouseEvent e) {
+ }
- @Override
- public void mouseEntered(final MouseEvent e) {
- }
+ @Override
+ public void mouseEntered(final MouseEvent e) {
+ }
- @Override
- public void mouseExited(final MouseEvent e) {
- }
+ @Override
+ public void mouseExited(final MouseEvent e) {
+ }
}
@@ -1,59 +1,56 @@
package net.avh4.util.imagecomparison;
-import java.awt.Color;
-import java.awt.Dimension;
-import java.awt.Graphics;
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
-import javax.imageio.ImageIO;
-import javax.swing.JComponent;
-
public class ImageDiffView extends JComponent {
- private static final long serialVersionUID = 1L;
-
- private final BufferedImage image1;
- private final BufferedImage image2;
-
- private boolean showHighlight = true;
-
- public ImageDiffView(final String file1, final String file2)
- throws IOException {
- image1 = ImageIO.read(new File(file1));
- image2 = ImageIO.read(new File(file2));
- }
-
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(image1.getWidth() + image2.getWidth(), Math.max(
- image1.getHeight(), image2.getHeight()));
- }
-
- @Override
- protected void paintComponent(final Graphics g) {
- final int w1 = image1.getWidth();
- final int w2 = image2.getWidth();
- final int h1 = image1.getHeight();
- final int h2 = image2.getHeight();
- g.drawImage(image1, 0, 0, null);
- g.drawImage(image2, w1, 0, null);
-
- if (showHighlight) {
- g.setColor(Color.RED);
- for (int x = 0; x < Math.min(w1, w2); x++) {
- for (int y = 0; y < Math.min(h1, h2); y++) {
- if (image1.getRGB(x, y) != image2.getRGB(x, y)) {
- g.drawLine(x, y, x, y);
- }
- }
- }
- }
- }
-
- public void setShowHighlight(final boolean b) {
- showHighlight = b;
- repaint();
- }
+ private static final long serialVersionUID = 1L;
+
+ private final BufferedImage image1;
+ private final BufferedImage image2;
+
+ private boolean showHighlight = true;
+
+ public ImageDiffView(final String file1, final String file2)
+ throws IOException {
+ image1 = ImageIO.read(new File(file1));
+ image2 = ImageIO.read(new File(file2));
+ }
+
+ @Override
+ public Dimension getPreferredSize() {
+ return new Dimension(image1.getWidth() + image2.getWidth(), Math.max(
+ image1.getHeight(), image2.getHeight()));
+ }
+
+ @Override
+ protected void paintComponent(final Graphics g) {
+ final int w1 = image1.getWidth();
+ final int w2 = image2.getWidth();
+ final int h1 = image1.getHeight();
+ final int h2 = image2.getHeight();
+ g.drawImage(image1, 0, 0, null);
+ g.drawImage(image2, w1, 0, null);
+
+ if (showHighlight) {
+ g.setColor(Color.RED);
+ for (int x = 0; x < Math.min(w1, w2); x++) {
+ for (int y = 0; y < Math.min(h1, h2); y++) {
+ if (image1.getRGB(x, y) != image2.getRGB(x, y)) {
+ g.drawLine(x, y, x, y);
+ }
+ }
+ }
+ }
+ }
+
+ public void setShowHighlight(final boolean b) {
+ showHighlight = b;
+ repaint();
+ }
}
@@ -1,30 +1,30 @@
package net.avh4.util.imagecomparison;
-import static net.avh4.util.imagecomparison.ImageComparisonMatchers.isApproved;
-import static org.hamcrest.MatcherAssert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
+import static net.avh4.util.imagecomparison.ImageComparisonMatchers.isApproved;
+import static org.hamcrest.MatcherAssert.assertThat;
+
public class ImageDiffViewTest {
- private ImageDiffView subject;
+ private ImageDiffView subject;
- @Before
- public void setUp() throws Exception {
- subject = new ImageDiffView("src/test/resources/expected.png",
- "src/test/resources/actual.png");
- }
+ @Before
+ public void setUp() throws Exception {
+ subject = new ImageDiffView("src/test/resources/expected.png",
+ "src/test/resources/actual.png");
+ }
- @Test
- public void testShowsHighlightInitially() throws Exception {
- assertThat(subject, isApproved());
- }
+ @Test
+ public void testShowsHighlightInitially() throws Exception {
+ assertThat(subject, isApproved());
+ }
- @Test
- public void testTurnOffHighlight() throws Exception {
- subject.setShowHighlight(false);
- assertThat(subject, isApproved());
- }
+ @Test
+ public void testTurnOffHighlight() throws Exception {
+ subject.setShowHighlight(false);
+ assertThat(subject, isApproved());
+ }
}
@@ -14,33 +14,33 @@
@SuppressWarnings("UnusedDeclaration")
public class DefaultStepdefs {
- private ImageDiff ui;
-
- @Given("^two slightly different image files \"expected.png\" and \"actual.png\"$")
- public void givenTwoSlightlyDifferentImageFilesexpectedpngAndactualpng() {
- }
-
- @When("^I launch \"imagediff ([^ ]*) ([^ ]*)\"$")
- public void whenILaunchimagediffExpectedpngActualpng(final String fileA,
- final String fileB) {
- final Sandbox sandbox = new Sandbox();
- sandbox.useResource(fileA);
- sandbox.useResource(fileB);
- ui = ImageDiff.launch(sandbox.getRoot(), fileA, fileB);
- }
-
- @When("^I click the display$")
- public void whenIClickTheDisplay() {
- final MouseListener[] mouseListeners = ui.getMouseListeners();
- for (final MouseListener mouseListener : mouseListeners) {
- mouseListener.mouseClicked(null);
- }
- }
-
- @Then("^I should see (.*)$")
- public void thenIShouldSeeADisplayOfActualpngWithTheDifferencesFromExpectedpngHighlighted(
- final String what) throws Exception {
- final String hash = DigestUtils.shaHex(what);
- assertThat(ui, looksLike("/" + hash + ".png"));
- }
+ private ImageDiff ui;
+
+ @Given("^two slightly different image files \"expected.png\" and \"actual.png\"$")
+ public void givenTwoSlightlyDifferentImageFilesexpectedpngAndactualpng() {
+ }
+
+ @When("^I launch \"imagediff ([^ ]*) ([^ ]*)\"$")
+ public void whenILaunchimagediffExpectedpngActualpng(final String fileA,
+ final String fileB) {
+ final Sandbox sandbox = new Sandbox();
+ sandbox.useResource(fileA);
+ sandbox.useResource(fileB);
+ ui = ImageDiff.launch(sandbox.getRoot(), fileA, fileB);
+ }
+
+ @When("^I click the display$")
+ public void whenIClickTheDisplay() {
+ final MouseListener[] mouseListeners = ui.getMouseListeners();
+ for (final MouseListener mouseListener : mouseListeners) {
+ mouseListener.mouseClicked(null);
+ }
+ }
+
+ @Then("^I should see (.*)$")
+ public void thenIShouldSeeADisplayOfActualpngWithTheDifferencesFromExpectedpngHighlighted(
+ final String what) throws Exception {
+ final String hash = DigestUtils.shaHex(what);
+ assertThat(ui, looksLike("/" + hash + ".png"));
+ }
}

0 comments on commit 114f2bc

Please sign in to comment.