📄 areaviewer.java
字号:
private boolean analyzeBitmapForObstacles() { if (backgroundImage == null) { return false; } /* Show obstacle finder dialog */ ObstacleFinderDialog obstacleFinderDialog; Container parentContainer = GUI.getTopParentContainer(); if (parentContainer instanceof Window) { obstacleFinderDialog = new ObstacleFinderDialog( backgroundImage, currentChannelModel, (Window) GUI.getTopParentContainer() ); } else if (parentContainer instanceof Frame) { obstacleFinderDialog = new ObstacleFinderDialog( backgroundImage, currentChannelModel, (Frame) GUI.getTopParentContainer() ); } else if (parentContainer instanceof Dialog) { obstacleFinderDialog = new ObstacleFinderDialog( backgroundImage, currentChannelModel, (Dialog) GUI.getTopParentContainer() ); } else { logger.fatal("Unknown parent container"); return false; } if (!obstacleFinderDialog.exitedOK) { return false; } /* Register obstacles */ final boolean[][] obstacleArray = obstacleFinderDialog.obstacleArray; final int boxSize = obstacleFinderDialog.sizeSlider.getValue(); // Create progress monitor final ProgressMonitor pm = new ProgressMonitor( GUI.getTopParentContainer(), "Registering obstacles", null, 0, obstacleArray.length - 1 ); // Thread that will perform the work final Runnable runnable = new Runnable() { public void run() { try { /* Remove already existing obstacles */ currentChannelModel.removeAllObstacles(); int foundObstacles = 0; for (int x=0; x < obstacleArray.length; x++) { for (int y=0; y < (obstacleArray[0]).length; y++) { if (obstacleArray[x][y]) { /* Register obstacle */ double realWidth = (boxSize * backgroundWidth) / backgroundImage.getWidth(null); double realHeight = (boxSize * backgroundHeight) / backgroundImage.getHeight(null); double realStartX = backgroundStartX + x * realWidth; double realStartY = backgroundStartY + y * realHeight; foundObstacles++; if (realStartX + realWidth > backgroundStartX + backgroundWidth) { realWidth = backgroundStartX + backgroundWidth - realStartX; } if (realStartY + realHeight > backgroundStartY + backgroundHeight) { realHeight = backgroundStartY + backgroundHeight - realStartY; } currentChannelModel.addRectObstacle( realStartX, realStartY, realWidth, realHeight, false ); } } /* Check if user has aborted */ if (pm.isCanceled()) { return; } /* Update progress monitor */ pm.setProgress(x); pm.setNote("After/Before merging: " + currentChannelModel.getNumberOfObstacles() + "/" + foundObstacles); } currentChannelModel.notifySettingsChanged(); thisPlugin.repaint(); } catch (Exception ex) { if (pm.isCanceled()) { return; } logger.fatal("Obstacle adding exception: " + ex.getMessage()); ex.printStackTrace(); pm.close(); return; } pm.close(); } }; /* Start thread */ Thread thread = new Thread(runnable); thread.start(); return true; } }; class ObstacleFinderDialog extends JDialog { private NumberFormat intFormat = NumberFormat.getIntegerInstance(); private BufferedImage imageToAnalyze = null; private BufferedImage obstacleImage = null; private JPanel canvasPanel = null; private boolean[][] obstacleArray = null; private boolean exitedOK = false; private JSlider redSlider, greenSlider, blueSlider, toleranceSlider, sizeSlider; /** * Listens to preview mouse motion event (when picking color) */ private MouseMotionListener myMouseMotionListener = new MouseMotionListener() { public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { // Convert from mouse to image pixel position Point pixelPoint = new Point( (int) (e.getX() * (((double) imageToAnalyze.getWidth()) / ((double) canvasPanel.getWidth()))), (int) (e.getY() * (((double) imageToAnalyze.getHeight()) / ((double) canvasPanel.getHeight()))) ); // Fetch color int color = imageToAnalyze.getRGB(pixelPoint.x, pixelPoint.y); int red = (color & 0x00ff0000) >> 16; int green = (color & 0x0000ff00) >> 8; int blue = color & 0x000000ff; // Update sliders redSlider.setValue(red); redSlider.repaint(); greenSlider.setValue(green); greenSlider.repaint(); blueSlider.setValue(blue); blueSlider.repaint(); } }; /** * Listens to preview mouse event (when picking color) */ private MouseListener myMouseListener = new MouseListener() { public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { // Stop picking color again; remove mouse listeners and reset mouse cursor 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 ObstacleFinderDialog(Image currentImage, ChannelModel currentChannelModel, Frame frame) { super(frame, "Analyze for obstacles"); setupDialog(currentImage, currentChannelModel); } protected ObstacleFinderDialog(Image currentImage, ChannelModel currentChannelModel, Window window) { super(window, "Analyze for obstacles"); setupDialog(currentImage, currentChannelModel); } protected ObstacleFinderDialog(Image currentImage, ChannelModel currentChannelModel, Dialog dialog) { super(dialog, "Analyze for obstacles"); setupDialog(currentImage, currentChannelModel); } private void setupDialog(Image currentImage, ChannelModel currentChannelModel) { JPanel mainPanel = new JPanel(); mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 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); mainPanel.setLayout(new BoxLayout(mainPanel, 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); mainPanel.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); mainPanel.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); mainPanel.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); mainPanel.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); mainPanel.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); mainPanel.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(); } }); mainPanel.add(tempPanel); mainPanel.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));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -