@Override public void actionPerformed(ActionEvent event) { if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { JPEGImage = ImageIO.read(chooser.getSelectedFile()); myfile = chooser.getSelectedFile(); JPEGFileName = myfile.getPath(); String PNGOutlineFileName = JPEGFileName.replace(".JPG", "outline.png"); String PNGPointsFileName = JPEGFileName.replace(".JPG", "points.png"); File outline = new File(PNGOutlineFileName); File points = new File(PNGPointsFileName); JPEGText.setText(myfile.getName()); pointsImage = ImageIO.read(points); outlineImage = ImageIO.read(outline); cornerAndControlMarkers = ImageUtils.extractColoredMarkers(pointsImage); outlineMarkers = ImageUtils.extractColoredMarkers(outlineImage); if (ImageUtils.check == true) { JOptionPane.showMessageDialog( framePoints, "Image " + "contained non-transparent pixel of invalid color," + " browse again."); JPEGText.setText(""); } setupImageDisplayWindow(); } catch (IOException ex) { JOptionPane.showMessageDialog( framePoints, JPEGText.getText() + " does not have matching points.png or outline.png," + " browse again."); JPEGText.setText(""); } } }
private void setupImageDisplayWindow() { BufferedImage points = ImageUtils.overlay(pointsImage, outlineImage); BufferedImage allThree = ImageUtils.overlay(points, JPEGImage); allThreeShrunk = ImageUtils.makeShrunkImageCopy(allThree, SCALE_FACTOR); ImageUtils.enlargePoints(allThreeShrunk, cornerAndControlMarkers, 3, SCALE_FACTOR); ImageUtils.enlargePoints(allThreeShrunk, outlineMarkers, 0, SCALE_FACTOR); pictureLabel = new JLabel(new ImageIcon(allThreeShrunk)); framePoints = new JFrame(); MouseListener listener = new MouseListener() { @Override public void mousePressed(MouseEvent event) { try { Point pWindow = (event.getPoint()); Point pInOriginalImage = new Point((int) (pWindow.x * SCALE_FACTOR), (int) (pWindow.y * SCALE_FACTOR)); ImageMarker mClosest = ImageUtils.getNearestMarkerFromList(pInOriginalImage, cornerAndControlMarkers); if (mClosest == null) { JOptionPane.showMessageDialog(framePoints, "Please click closer" + " to points."); } String markerID = ""; if (mClosest.getType() == ImageMarker.MarkerType.CONTROL_POINT) { markerID = JOptionPane.showInputDialog("Please enter " + "the control point number."); markerID = markerID.trim(); if (!markerID.contains("1") && !markerID.contains("2") && !markerID.contains("3") && !markerID.contains("4") && !markerID.contains("5") && !markerID.contains("6") && !markerID.contains("7") && !markerID.contains("8") && !markerID.contains("9") && !markerID.contains("0")) { markerID = JOptionPane.showInputDialog("Please enter a number."); } if (markerID != null) { boolean foundDuplicate = false; for (ImageMarker m : cornerAndControlMarkers) { if (markerID.equals(m.getID()) && !markerID.startsWith("-")) { foundDuplicate = true; break; } } if (foundDuplicate) { markerID = JOptionPane.showInputDialog( "Sorry," + " control point already specified, enter" + " another (or re-enter if you are sure)."); } mClosest.setID(markerID); } } else if (mClosest.getType() == ImageMarker.MarkerType.CORNER_POINT) { markerID = JOptionPane.showInputDialog("Please enter" + " the letter of the corner."); if (markerID != null) { markerID = markerID.toUpperCase().trim(); if (markerID.contains("1") || markerID.contains("2") || markerID.contains("3") || markerID.contains("4") || markerID.contains("5") || markerID.contains("6") || markerID.contains("7") || markerID.contains("8") || markerID.contains("9") || markerID.contains("0")) { markerID = JOptionPane.showInputDialog("Please enter a letter."); markerID = markerID.toUpperCase().trim(); } for (ImageMarker m : cornerAndControlMarkers) { if (markerID.equals(m.getID())) { markerID = JOptionPane.showInputDialog( "Sorry," + " corner already specified, enter another."); markerID = markerID.toUpperCase().trim(); } } mClosest.setID(markerID); if (markerID.equals("A")) { cornerAXText.setText(String.valueOf(mClosest.getLocation().x + 0.5)); cornerAYText.setText(String.valueOf(mClosest.getLocation().y + 0.5)); } if (markerID.equals("B")) { cornerBXText.setText(String.valueOf(mClosest.getLocation().x + 0.5)); cornerBYText.setText(String.valueOf(mClosest.getLocation().y + 0.5)); } if (markerID.equals("C")) { cornerCXText.setText(String.valueOf(mClosest.getLocation().x + 0.5)); cornerCYText.setText(String.valueOf(mClosest.getLocation().y + 0.5)); } if (markerID.equals("D")) { cornerDXText.setText(String.valueOf(mClosest.getLocation().x + 0.5)); cornerDYText.setText(String.valueOf(mClosest.getLocation().y + 0.5)); } } } if (markerID != null) { Graphics2D g = allThreeShrunk.createGraphics(); g.setColor(Color.GRAY); int centerx = (int) (mClosest.getLocation().x / SCALE_FACTOR); int centery = (int) (mClosest.getLocation().y / SCALE_FACTOR); g.fillOval(centerx - 2, centery - 2, 5, 5); g.setColor(Color.BLACK); g.drawString(markerID, centerx + 4, centery + 4); g.drawString(markerID, centerx + 6, centery + 4); g.drawString(markerID, centerx + 4, centery + 6); g.drawString(markerID, centerx + 6, centery + 6); g.setColor(Color.WHITE); g.drawString(markerID, centerx + 5, centery + 5); g.dispose(); pictureLabel.repaint(); } } catch (Exception ex) { System.out.println(ex); } } @Override public void mouseClicked(MouseEvent event) {} @Override public void mouseReleased(MouseEvent event) {} @Override public void mouseEntered(MouseEvent event) {} @Override public void mouseExited(MouseEvent event) {} }; pictureLabel.addMouseListener(listener); framePoints.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); framePoints.getContentPane().add(pictureLabel); framePoints.pack(); pictureLabel.setBackground(Color.black); pictureLabel.setOpaque(true); JScrollPane scrollBar = new JScrollPane( pictureLabel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); framePoints.add(scrollBar); framePoints.setTitle("Overlayed Points and Outline"); framePoints.setLocation(350, 0); framePoints.setSize(945, 710); framePoints.setVisible(true); }