📄 areaviewer.java
字号:
MouseListener[] allMouseListeners = canvasPanel.getMouseListeners(); for (MouseListener mouseListener: allMouseListeners) canvasPanel.removeMouseListener(mouseListener); MouseMotionListener[] allMouseMotionListeners = canvasPanel.getMouseMotionListeners(); for (MouseMotionListener mouseMotionListener: allMouseMotionListeners) canvasPanel.removeMouseMotionListener(mouseMotionListener); canvasPanel.setCursor(Cursor.getDefaultCursor()); } }; /** * Creates a new dialog for settings background parameters */ protected AnalyzeImageDialog(Image currentImage, ChannelModel currentChannelModel, Frame owner) { super(owner, "Analyze for obstacles"); JPanel tempPanel; JLabel tempLabel; JSlider tempSlider; JButton tempButton; setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Dimension labelDimension = new Dimension(100, 20); // Convert Image to BufferedImage imageToAnalyze = new BufferedImage( currentImage.getWidth(this), currentImage.getHeight(this), BufferedImage.TYPE_INT_ARGB ); Graphics2D g = imageToAnalyze.createGraphics(); g.drawImage(currentImage, 0, 0, null); // Prepare initial obstacle image obstacleImage = new BufferedImage( currentImage.getWidth(this), currentImage.getHeight(this), BufferedImage.TYPE_INT_ARGB ); // Set layout and add components intFormat.setMinimumIntegerDigits(1); intFormat.setMaximumIntegerDigits(3); intFormat.setParseIntegerOnly(true); setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS)); // Obstacle color tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(tempLabel = new JLabel("Obstacle")); tempLabel.setPreferredSize(labelDimension); add(tempPanel); tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(tempLabel = new JLabel("Red")); tempLabel.setPreferredSize(labelDimension); tempLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT); tempPanel.add(tempSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0)); tempSlider.setMajorTickSpacing(50); tempSlider.setPaintTicks(true); tempSlider.setPaintLabels(true); add(tempPanel); redSlider = tempSlider; tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(tempLabel = new JLabel("Green")); tempLabel.setPreferredSize(labelDimension); tempLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT); tempPanel.add(tempSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0)); tempSlider.setMajorTickSpacing(50); tempSlider.setPaintTicks(true); tempSlider.setPaintLabels(true); add(tempPanel); greenSlider = tempSlider; tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(tempLabel = new JLabel("Blue")); tempLabel.setPreferredSize(labelDimension); tempLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT); tempPanel.add(tempSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0)); tempSlider.setMajorTickSpacing(50); tempSlider.setPaintTicks(true); tempSlider.setPaintLabels(true); add(tempPanel); blueSlider = tempSlider; // Tolerance tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(tempLabel = new JLabel("Tolerance")); tempLabel.setPreferredSize(labelDimension); tempLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT); tempPanel.add(tempSlider = new JSlider(JSlider.HORIZONTAL, 0, 128, 0)); tempSlider.setMajorTickSpacing(25); tempSlider.setPaintTicks(true); tempSlider.setPaintLabels(true); add(tempPanel); toleranceSlider = tempSlider; // Obstacle size tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(tempLabel = new JLabel("Obstacle size")); tempLabel.setPreferredSize(labelDimension); tempLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT); tempPanel.add(tempSlider = new JSlider(JSlider.HORIZONTAL, 1, 40, 40)); tempSlider.setInverted(true); tempSlider.setMajorTickSpacing(5); tempSlider.setPaintTicks(true); tempSlider.setPaintLabels(true); add(tempPanel); sizeSlider = tempSlider; // Buttons: Pick color, Preview obstacles etc. tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(Box.createHorizontalGlue()); tempPanel.add(tempButton = new JButton("Pick color")); tempButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Set to color picker mode (if not already there) if (canvasPanel.getMouseMotionListeners().length == 0) { canvasPanel.addMouseListener(myMouseListener); canvasPanel.addMouseMotionListener(myMouseMotionListener); canvasPanel.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); } } }); tempPanel.add(Box.createHorizontalStrut(5)); tempPanel.add(tempButton = new JButton("Preview obstacles")); tempButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { obstacleImage = createObstacleImage(); canvasPanel.repaint(); } }); add(tempPanel); add(Box.createVerticalStrut(10)); // Preview image tempPanel = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(imageToAnalyze, 0, 0, getWidth(), getHeight(), this); g.drawImage(obstacleImage, 0, 0, getWidth(), getHeight(), this); } }; tempPanel.setBorder( BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.BLACK), "Preview")); tempPanel.setPreferredSize(new Dimension(400, 400)); tempPanel.setBackground(Color.CYAN); add(tempPanel); canvasPanel = tempPanel; // Saved in canvasPanel // Buttons: Cancel, OK tempPanel = new JPanel(); tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS)); tempPanel.add(Box.createHorizontalGlue()); tempPanel.add(tempButton = new JButton("Cancel")); tempButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); tempPanel.add(Box.createHorizontalStrut(5)); tempPanel.add(tempButton = new JButton("OK")); tempButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { obstacleImage = createObstacleImage(); exitedOK = true; dispose(); } }); add(tempPanel); add(Box.createVerticalGlue()); add(Box.createVerticalStrut(10)); // Show dialog setModal(true); pack(); setLocationRelativeTo(owner); setVisible(true); } /** * Create obstacle image by analyzing current background image * and using the current obstacle color, size and tolerance. * This method also creates the boolean array obstacleArray. * * @return New obstacle image */ private BufferedImage createObstacleImage() { int nrObstacles = 0; // Create new obstacle image all transparent (no obstacles) BufferedImage newObstacleImage = new BufferedImage( imageToAnalyze.getWidth(), imageToAnalyze.getHeight(), BufferedImage.TYPE_INT_ARGB ); for (int x=0; x < imageToAnalyze.getWidth(); x++) { for (int y=0; y < imageToAnalyze.getHeight(); y++) { newObstacleImage.setRGB(x, y, 0x00000000); } } // Get target color to match against int targetRed = redSlider.getValue(); int targetGreen = greenSlider.getValue(); int targetBlue = blueSlider.getValue(); // Get obstacle resolution and size int boxSize = sizeSlider.getValue(); int tolerance = toleranceSlider.getValue(); // Divide image into boxes and check each box for obstacles int arrayWidth = (int) Math.ceil((double) imageToAnalyze.getWidth() / (double) boxSize); int arrayHeight = (int) Math.ceil((double) imageToAnalyze.getHeight() / (double) boxSize); obstacleArray = new boolean[arrayWidth][arrayHeight]; for (int x=0; x < imageToAnalyze.getWidth(); x+=boxSize) { for (int y=0; y < imageToAnalyze.getHeight(); y+=boxSize) { boolean boxIsObstacle = false; // Check all pixels in box for obstacles for (int xx=x; xx < x + boxSize && xx < imageToAnalyze.getWidth(); xx++) { for (int yy=y; yy < y + boxSize && yy < imageToAnalyze.getHeight(); yy++) { // Get current pixel color int color = imageToAnalyze.getRGB(xx, yy); int red = (color & 0x00ff0000) >> 16; int green = (color & 0x0000ff00) >> 8; int blue = color & 0x000000ff; // Calculate difference from target color int difference = Math.abs(red - targetRed) + Math.abs(green - targetGreen) + Math.abs(blue - targetBlue); // If difference is small enough make this box an obstacle if (difference <= tolerance) { boxIsObstacle = true; break; } } if (boxIsObstacle) break; } // If box is obstacle, colorize it if (boxIsObstacle) { obstacleArray[x/boxSize][y/boxSize] = true; nrObstacles++; // Colorize all pixels in the box for (int xx=x; xx < x + boxSize && xx < imageToAnalyze.getWidth(); xx++) { for (int yy=y; yy < y + boxSize && yy < imageToAnalyze.getHeight(); yy++) { newObstacleImage.setRGB(xx, yy, 0x9922ff22); } } } else obstacleArray[x/boxSize][y/boxSize] = false; } } // End of "divide into boxes" for-loop return newObstacleImage; } } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("analyze for obstacles")) { if (backgroundImage == null) return; AnalyzeImageDialog analyzer = new AnalyzeImageDialog( backgroundImage, currentChannelModel, GUI.frame ); // Check return value from analyzer if (analyzer.exitedOK) { // Remove old obstacle and register new final boolean[][] obstacleArray = analyzer.obstacleArray; final int boxSize = analyzer.sizeSlider.getValue(); // Create progress monitor final ProgressMonitor pm = new ProgressMonitor( GUI.frame, "Registering obstacles", null, 0, obstacleArray.length - 1 ); // Thread that will perform the work final Runnable runnable = new Runnable() { public void run() { try { int foundObstacles = 0; // Clear all old obstacles currentChannelModel.removeAllObstacles(); for (int x=0; x < obstacleArray.length; x++) { for (int y=0; y < (obstacleArray[0]).length; y++) { // If obstacle, register it if (obstacleArray[x][y]) { double realWidth = ((double) boxSize * backgroundWidth) / (double) backgroundImage.getWidth(null); double realHeight = ((double) boxSize * backgroundHeight) / (double) backgroundImage.getHeight(null); double realStartX = backgroundStartX + (double) x * realWidth; double realStartY = backgroundStartY + (double) y * realHeight; foundObstacles++; if (realStartX + realWidth > backgroundStartX + backgroundWidth) realWidth = backgroundStartX + backgroundWidth - realStartX; if (realStartY + realHeight > backgroundStartY + backgroundHeight) realHeight = backgroundStartY + backgroundHeight - realStartY;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -