📄 areaviewer.java
字号:
tempPanel.setBackground(Color.CYAN); mainPanel.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(); } }); mainPanel.add(tempPanel); mainPanel.add(Box.createVerticalGlue()); mainPanel.add(Box.createVerticalStrut(10)); add(mainPanel); // Show dialog setModal(true); pack(); setLocationRelativeTo(this.getParent()); /* Make sure dialog is not too big */ Rectangle maxSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); if (maxSize != null && (getSize().getWidth() > maxSize.getWidth() || getSize().getHeight() > maxSize.getHeight())) { Dimension newSize = new Dimension(); newSize.height = Math.min((int) maxSize.getHeight(), (int) getSize().getHeight()); newSize.width = Math.min((int) maxSize.getWidth(), (int) getSize().getWidth()); setSize(newSize); } 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; } } /** * Listens to settings changes in the radio medium. */ private Observer radioMediumSettingsObserver = new Observer() { public void update(Observable obs, Object obj) { // Clear selected radio (if any selected) and radio medium coverage selectedRadio = null; channelImage = null; canvas.repaint(); } }; /** * Listens to settings changes in the radio medium. */ private Observer radioMediumActivityObserver = new Observer() { public void update(Observable obs, Object obj) { // Just remove any selected radio (it may have been removed) canvas.repaint(); } }; /** * Listens to settings changes in the channel model. */ private Observer channelModelSettingsObserver = new Observer() { public void update(Observable obs, Object obj) { needToRepaintObstacleImage = true; canvas.repaint(); } }; /** * Returns a color corresponding to given value where higher values are more green, and lower values are more red. * * @param value Signal strength of received signal (dB) * @param lowBorder * @param highBorder * @return Integer containing standard ARGB color. */ private int getColorOfSignalStrength(double value, double lowBorder, double highBorder) { double upperLimit = highBorder; // Best signal adds green double lowerLimit = lowBorder; // Bad signal adds red double intervalSize = (upperLimit - lowerLimit) / 2; double middleValue = lowerLimit + (upperLimit - lowerLimit) / 2; if (value > highBorder) { return 0xCC00FF00; } if (value < lowerLimit) { return 0xCCFF0000; } int red = 0, green = 0, blue = 0, alpha = 0xCC; // Upper limit (green) if (value > upperLimit - intervalSize) { green = (int) (255 - 255*(upperLimit - value)/intervalSize); } // Medium signal adds blue if (value > middleValue - intervalSize && value < middleValue + intervalSize) { blue = (int) (255 - 255*Math.abs(middleValue - value)/intervalSize); } // Bad signal adds red if (value < lowerLimit + intervalSize) { red = (int) (255 - 255*(value - lowerLimit)/intervalSize); } return (alpha << 24) | (red << 16) | (green << 8) | blue; } /** * Helps user adjust and calculate the channel propagation formula */ private ActionListener formulaHandler = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("recalculate visible area")) { // Get resolution of new image final Dimension resolution = new Dimension( resolutionSlider.getValue(), resolutionSlider.getValue() ); // Abort if no radio selected if (selectedRadio == null) { channelImage = null; canvas.repaint(); return; } // Get new location/size of area to attenuate final double startX = -currentPanX; final double startY = -currentPanY; final double width = canvas.getWidth() / currentZoomX; final double height = canvas.getHeight() / currentZoomY; // Get sending radio position Position radioPosition = currentRadioMedium.getRadioPosition(selectedRadio); final double radioX = radioPosition.getXCoordinate(); final double radioY = radioPosition.getYCoordinate(); // Create temporary image final BufferedImage tempChannelImage = new BufferedImage(resolution.width, resolution.height, BufferedImage.TYPE_INT_ARGB); // Save time for later analysis final long timeBeforeCalculating = System.currentTimeMillis(); // Create progress monitor final ProgressMonitor pm = new ProgressMonitor( GUI.getTopParentContainer(), "Calculating channel attenuation", null, 0, resolution.width - 1 ); // Thread that will perform the work final Runnable runnable = new Runnable() { public void run() { try { // Available signal strength intervals double lowestImageValue = Double.MAX_VALUE; double highestImageValue = -Double.MAX_VALUE; // Create image values (calculate each pixel) double[][] imageValues = new double[resolution.width][resolution.height]; for (int x=0; x < resolution.width; x++) { for (int y=0; y < resolution.height; y++) { if (dataTypeToVisualize == ChannelModel.TransmissionData.SIGNAL_STRENGTH) { // Attenuate double[] signalStrength = currentChannelModel.getReceivedSignalStrength( radioX, radioY, startX + width * x/resolution.width, startY + height * y/resolution.height ); // Collecting signal strengths if (signalStrength[0] < lowestImageValue) { lowestImageValue = signalStrength[0]; } if (signalStrength[0] > highestImageValue) { highestImageValue = signalStrength[0]; } imageValues[x][y] = signalStrength[0]; } else if (dataTypeToVisualize == ChannelModel.TransmissionData.SIGNAL_STRENGTH_VAR) { // Attenuate double[] signalStrength = currentChannelModel.getReceivedSignalStrength( radioX, radioY, startX + width * x/resolution.width, startY + height * y/resolution.height ); // Collecting variances if (signalStrength[1] < lowestImageValue) { lowestImageValue = signalStrength[1]; } if (signalStrength[1] > highestImageValue) { highestImageValue = signalStrength[1]; } imageValues[x][y] = signalStrength[1]; } else if (dataTypeToVisualize == ChannelModel.TransmissionData.SNR) { // Get signal to noise ratio double[] snr = currentChannelModel.getSINR( radioX, radioY, startX + width * x/resolution.width, startY + height * y/resolution.height, -Double.MAX_VALUE ); // Collecting signal to noise ratio if (snr[0] < lowestImageValue) { lowestImageValue = snr[0]; } if (snr[0] > highestImageValue) { highestImageValue = snr[0]; } imageValues[x][y] = snr[0]; } else if (dataTypeToVisualize == ChannelModel.TransmissionData.SNR_VAR) { // Get signal to noise ratio double[] snr = currentChannelModel.getSINR( radioX, radioY, startX + width * x/resolution.width, startY + he
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -