/** * This method plots the arraylist of samples, with the Window defined as parameter * * @param samples the samples to plot * @param samplesPerPixel The number of samples in each pixel * @param color The color of the line */ public void plot(ArrayList<Float> samples, final float samplesPerPixel, final Color color) { // System.out.println("Dentro plot"); synchronized (buffImage) { // System.out.println("Dentro sync"); /* * if the size of the image is smaller than the ammount of values * (converted to pixels), then we draw a new rectangule to serve as * background */ if (buffImage.getWidth() < samples.size() / samplesPerPixel) { /* * BufferedImage tmpBuffImg = new BufferedImage((int) (samples * .size() / samplesPerPixel), frame.getHeight(), * BufferedImage.TYPE_4BYTE_ABGR); * * Graphics2D graph = tmpBuffImg.createGraphics(); * graph.setColor(Color.black); graph.fillRect(0, 0, * tmpBuffImg.getWidth(), tmpBuffImg .getHeight()); // * graph.dispose(); panel.setSize(tmpBuffImg.getWidth(), * tmpBuffImg.getHeight()); * * buffImage = tmpBuffImg; */ buffImage = new BufferedImage( (int) (samples.size() / samplesPerPixel), frame.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D graph = buffImage.createGraphics(); graph.setColor(Color.black); graph.fillRect(0, 0, buffImage.getWidth(), buffImage.getHeight()); // graph.dispose(); panel.setSize(buffImage.getWidth(), buffImage.getHeight()); } // System.out.println("Dentro sync2"); /* * when the plot is cleared, we calculate the maximum and minimum of * all the values passed, i.e., the scaling factor */ if (cleared) { for (int i = 0; i < samples.size(); i++) { this.min = Math.min(samples.get(i), min); this.max = Math.max(samples.get(i), max); } scalingFactor = max - min; cleared = false; } // System.out.println("Dentro sync 3"); Graphics2D graph = buffImage.createGraphics(); // draws the reference lines (max, min, zero) drawReferenceLines(graph, true, true, true); graph.setColor(color); // System.out.println("Dentro sync"); // float lastValue = (samples.get(0) / scalingFactor) // * image.getHeight() / 3 + image.getHeight() / 2; // Scales the first value of the array in order to be plotted float lastSampleScaled = calculateScaledValue(buffImage, samples.get(0), 0); for (int i = 1; i < samples.size(); i++) { // float value = (samples.get(i) / scalingFactor) // * image.getHeight() / 3 + image.getHeight() / 2; // Scales the ith value of the array in order to be plotted float sampleScaled = calculateScaledValue(buffImage, samples.get(i), 0); /* it draws a line between the last and this value */ graph.drawLine( (int) ((i - 1) / samplesPerPixel), buffImage.getHeight() - (int) lastSampleScaled, (int) (i / samplesPerPixel), buffImage.getHeight() - (int) sampleScaled); /* * the sample of this iteration will be next iteration's last * sample */ lastSampleScaled = sampleScaled; } // graph.dispose(); } }
/** * This plots the samples, with the Window defined as parameter. It can used the scale of the last * plot and a vertical offset. * * @param samples the samples to plot * @param samplesPerPixel The number of samples in each pixel * @param verticalOffset The vertical offSet (in order to plot several frequencies in the same * image) * @param useLastScale boolean that tells to user or not the last scale * @param color The color of the line */ public void plot( ArrayList<Float> samples, final float samplesPerPixel, final float verticalOffset, final boolean useLastScale, final Color color) { synchronized (buffImage) { /* * if the size of the image is smaller than the ammount of values * (converted to pixels), then we draw a new rectangule to serve as * background */ if (buffImage.getWidth() < samples.size() / samplesPerPixel) { buffImage = new BufferedImage( (int) (samples.size() / samplesPerPixel), frame.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D graph = buffImage.createGraphics(); graph.setColor(Color.black); graph.fillRect(0, 0, buffImage.getWidth(), buffImage.getHeight()); graph.dispose(); panel.setSize(buffImage.getWidth(), buffImage.getHeight()); } /* * When useLastScale != true, we have to recalculate the scaling * factor */ if (!useLastScale) { for (int i = 0; i < samples.size(); i++) { min = Math.min(samples.get(i), min); max = Math.max(samples.get(i), max); } scalingFactor = max - min; } Graphics2D graph = buffImage.createGraphics(); // draws the reference lines (max, min, zero) drawReferenceLines(graph, true, true, true); graph.setColor(Color.white); graph.drawLine( 0, buffImage.getHeight() / 2, (int) buffImage.getWidth(), buffImage.getHeight() / 2); graph.setColor(color); /* * float lastValue = (samples.get(0) / scalingFactor) * buffImage.getHeight() / 3 + buffImage.getHeight() / 2 - * verticalOffset * buffImage.getHeight() / 3; */ // Scales the first value of the array in order to be plotted float lastSampleScaled = calculateScaledValue(buffImage, samples.get(0), verticalOffset); for (int i = 1; i < samples.size(); i++) { /* * float value = (samples.get(i) / scalingFactor) * buffImage.getHeight() / 3 + buffImage.getHeight() / 2 - * verticalOffset * buffImage.getHeight() / 3; */ // Scales the ith value of the array in order to be plotted float sampleScaled = calculateScaledValue(buffImage, samples.get(i), verticalOffset); /* * draws a line between the value of this iteration and that of * the previous */ graph.drawLine( (int) ((i - 1) / samplesPerPixel), buffImage.getHeight() - (int) lastSampleScaled, (int) (i / samplesPerPixel), buffImage.getHeight() - (int) sampleScaled); /* * the sample of this iteration will be next iteration's last * sample */ lastSampleScaled = sampleScaled; } graph.dispose(); } }
/** * This plots the values passed, with the Window defined as parameter. It updates the image * initialized in the constructor. * * @param samples the samples to plot * @param samplesPerPixel The number of samples in each pixel * @param color The color of the line */ public void plot(float[] samples, final float samplesPerPixel, final Color color) { synchronized (buffImage) { /* * if the size of the image is smaller than the ammount of values * (converted to pixels), then we draw a new rectangule to serve as * background */ if (buffImage.getWidth() < samples.length / samplesPerPixel) { buffImage = new BufferedImage( (int) (samples.length / samplesPerPixel), frame.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D graph = buffImage.createGraphics(); graph.setColor(Color.black); graph.fillRect(0, 0, buffImage.getWidth(), buffImage.getHeight()); graph.dispose(); panel.setSize(buffImage.getWidth(), buffImage.getHeight()); } /* * when the plot is cleared, we calculate the maximum and minimum of * all the values passed, i.e., the scaling factor */ if (cleared) { float min = 0; float max = 0; for (int i = 0; i < samples.length; i++) { min = Math.min(samples[i], min); max = Math.max(samples[i], max); } scalingFactor = max - min; cleared = false; } // it starts the class that allows to draw the line graph Graphics2D graph = buffImage.createGraphics(); // draws the reference lines drawReferenceLines(graph, true, true, true); graph.setColor(color); // scales the values // float lastValue = (samples[0] / scalingFactor) * // image.getHeight() // / 3 + image.getHeight() / 2; // Scales the first value of the array in order to be plotted float lastSampleScaled = calculateScaledValue(buffImage, samples[0], 0); for (int i = 1; i < samples.length; i++) { // float value = (samples[i] / scalingFactor) * // image.getHeight() // / 3 + image.getHeight() / 2; // Scales the ith value of the array in order to be plotted float sampleScaled = calculateScaledValue(buffImage, samples[i], 0); /* it draws a line between the last and this value */ graph.drawLine( (int) ((i - 1) / samplesPerPixel), buffImage.getHeight() - (int) lastSampleScaled, (int) (i / samplesPerPixel), buffImage.getHeight() - (int) sampleScaled); /* * the sample of this iteration will be next iteration's last * sample */ lastSampleScaled = sampleScaled; } graph.dispose(); } }