-1

我想对音频信号进行图形表示。我在将数据数组输入到方法paint(图形g)时遇到问题。在 setData(int intValue) 方法中输入的数据可以正常工作。但是,如果我想在方法 paint() 中打印一个数据数组,我的值为零。为什么?

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class MyPlotter extends JPanel{
    int width = 320;
    int height = 130;
    int frameSize;
    int[] data;

public MyPlotter(int fSize){
    setSize(width,height);      
    setPreferredSize(this.getSize());
    this.frameSize = fSize;
    data = new int[fSize+1];
} 


public void setData(int[] intValue){
    data = intValue;

// this works fine:
        for (int i=0; i<440; i++)
            System.out.println("setData "+data[i]);

    repaint();
}

public void paint (Graphics g){ 

   // some code:
   // g.drawLine(...)
   // g.setColor(...)
   // etc...    

 for (int i = 0; i< frameSize-1;i++)
    { 
     //ZERO values:
     System.out.println("paint() "+(data[i]));

     // g.drawline(...);
    }
 }
 }

编辑:

从 MyPanel.class 输入数组数据

import javax.swing.JPanel;

public class MyPanel extends JPanel {

    private MyPlotter plotter;

    public MyPanel(){
      setSize(320,210);
      plotter = new MyPlotter(440);
      add(this.plotter,0);
    }

    public void setData(int[] data){
      plotter.setData(data);  
    }   
}
4

4 回答 4

1
data = intValue;

在这里,您将 intValue 引用到数据。如果您更改数据 intValue 数组也会更改。

试试这个 :

data = System.arraycopy( intValue, 0, data, 0, intValue.length);

代替 :

data = intValue;
于 2013-02-21T11:29:37.800 回答
0

我认为您调用的“repaint()”方法可能会修改原始数组“intValue”

您所做的分配data = intValue;仅使“data []”成为原始数组的引用,因此调用 repaint 似乎数据已重置或引用丢失。如果要复制数组,您可以执行以下任何操作a & b 是两个数组:

b = Arrays.copyOf(a, a.length);

0r

b = new int[a.length];
System.arraycopy(a, 0, b, 0, b.length);

或者

b = (int[]) a.clone();

或者

int b[] = new int[a.length];

        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
于 2013-02-21T11:59:59.157 回答
0

你的代码没问题。问题出在主要方法上。确保您只向面板添加了一台绘图仪。

public static void main(String[]args){
    int[] mass =  new int[]{1,2,3,4,5,6,7,8,9,10};
    JFrame frame =  new JFrame();
    MyPanel mp = new MyPanel();
    frame.add(mp);
    frame.setSize(300, 300);         
    mp.setData(mass);         
    frame.setVisible(true);  

 }

效果很好。

于 2013-02-21T13:57:30.263 回答
0

很抱歉回答我自己的帖子,但我完全改变了程序。我不知道我是否可以从第一篇文章中删除代码。所以我发了一个新帖子。

我更改了一个应用程序,但在将数组输入到另一种方法时仍然存在问题。通过“Arrays.copyOf b = (a, a.length)”复制数组并将数组更改为静态并不能解决问题。我无法在 OscilloscopeController 类中输入从 setSignal() 到 getSignal() 的数组。

示波器控制器.java

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 < 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 < 44100; i++) {
        int randomInt = randomGenerator.nextInt(32675 * 2) - (32675);
        if (i % 4 == 0) {
            j = randomInt;
        }
     in = j;
    }


    if (idx++ > frameSize * numFramesToSkip) 
    { 
        idx = 0;
    } 
    else 
    {
        if (idx < 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 < 440; i++) {
         int randomInt = randomGenerator.nextInt(32675*2)-(32675);
         if (i%4==0) j = randomInt;
         toDraw[i]=j; 
    }
    */

for (int i = 0; i < frameSize; i++) 
        System.out.println("Controller.getSignal() = "+ toDraw[i]);

        return toDraw;
    }
}

MainPanel包含主要方法:

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);
  }
}

示波器视图.java

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);  
    }  
}

OscPlotter.java

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<height;i+=height/8) 
            g.drawLine(0,i,width,i);            
        for (int i = 1; i<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< 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))); 
        }
    }
}
于 2013-02-22T13:00:27.347 回答