Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm sorry to answer my own post, but I changed the program completely. I do not know if I can remove the code from the first post. So I put in a new post.</p> <p>I changed a application but still have a problem with entering the array to another method. Copy arrays by "Arrays.copyOf b = (a, a.length)" and changing the array as static does not fix a problem. I can't enter array from setSignal() to getSignal() in OscilloscopeController class.</p> <p><strong>OscilloscopeController.java</strong></p> <pre><code>import java.util.Arrays; import java.util.Random; public class OscilloscopeController { private static int frameSize = (int) (44100 / 100F); private int idx, numFramesToSkip = 10; private static int[] toDraw = new int[frameSize]; private static int[] data = new int[frameSize]; public OscilloscopeController() { for (int i = 0; i &lt; frameSize; i++) { toDraw[i] = 0; data[i] = 0; } } public void setSignal(int in) { // Because the Model layer is off, I generate a test signal Random randomGenerator = new Random(); int j = 0; for (int i = 0; i &lt; 44100; i++) { int randomInt = randomGenerator.nextInt(32675 * 2) - (32675); if (i % 4 == 0) { j = randomInt; } in = j; } if (idx++ &gt; frameSize * numFramesToSkip) { idx = 0; } else { if (idx &lt; frameSize) { data[idx] = in; } if (idx == frameSize) { // HERE IS PROBLEM. I'D LIKE THIS toDraw in getSignal() toDraw = Arrays.copyOf(data, data.length); } } } public int[] getSignal() { // IF UNCOMMENT BELOW WORKS FINE, OTHERWISE ZERO VALUES /* Random randomGenerator = new Random(); int j=0; for (int i = 0; i &lt; 440; i++) { int randomInt = randomGenerator.nextInt(32675*2)-(32675); if (i%4==0) j = randomInt; toDraw[i]=j; } */ for (int i = 0; i &lt; frameSize; i++) System.out.println("Controller.getSignal() = "+ toDraw[i]); return toDraw; } } </code></pre> <p><strong>MainPanel</strong> contains main method:</p> <pre><code>import java.awt.*; import javax.swing.*; public class MainPanel extends JPanel { public static void main(String args[]) throws Exception { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new MainPanel()); frame.pack(); frame.setVisible(true); } }); } private final OscilloscopeView oscView = new OscilloscopeView(); private final OscilloscopeController oscController = new OscilloscopeController(); public MainPanel() { setPreferredSize(new Dimension(400, 200)); // a lot of other components here oscView.setData(oscController.getSignal()); add(oscView); } } </code></pre> <p><strong>OscilloscopeView.java</strong></p> <pre><code>import javax.swing.JFrame; import javax.swing.JPanel; public class OscilloscopeView extends JPanel { private OscPlotter plotter = new OscPlotter(441);; public OscilloscopeView() { setSize(320,210); add(this.plotter); } public void setData(int[] data){ plotter.setData(data); } } </code></pre> <p><strong>OscPlotter.java</strong></p> <pre><code>import java.awt.Color; import java.awt.Graphics; import java.util.Arrays; import javax.swing.JPanel; public class OscPlotter extends JPanel{ private int width = 320; private int height = 130; private float widthC; private float heightC; private int sampleSize; private int frameSize; private int[] toDraw; private float x,prevX; public OscPlotter(int fSize){ setSize(width,height); setPreferredSize(this.getSize()); this.frameSize = fSize; this.toDraw = new int[441]; sampleSize = 1; } public void setData(int[] data){ toDraw = Arrays.copyOf(data, data.length); repaint(); } @Override public void paintComponent(Graphics g){ widthC = ((float)width/frameSize); sampleSize = (16 == 16 ? 65536 : 256); heightC = (((float)height-15)/sampleSize); g.setColor(Color.black); g.fillRect(0,0,width,height); g.setColor(Color.gray); for (int i = 1; i&lt;height;i+=height/8) g.drawLine(0,i,width,i); for (int i = 1; i&lt;width;i+=width/8) g.drawLine(i,0,i,height); g.setColor(Color.lightGray); g.drawLine(0,(int)(height/2),width,(int)((height/2))); g.setColor(Color.green); x = 0; for (int i = 0; i&lt; frameSize-1;i++) { prevX = x; x +=widthC; // draw the read waveform data g.drawLine ((int)prevX,(int)((height/2+toDraw[i]*heightC)),(int)x,(int)((height/2+toDraw[i+1]*heightC))); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload