Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        4年前关闭。
                    
                
        

JFrame内部创建医疗波形。但我想将该波形放在jpanel中以用于多线程概念。我不知道该怎么做,任何人都可以建议我。

这是我的代码,它将在jframe中创建波形。我该如何将该波形放入jpanel jp2中。


  服务器代码


public class samplehex extends JFrame {
JPanel jp2;
samplehex() {
    jp2 = new JPanel();
}
private static final int BSIZE = 1024;
int flag,spo2_wave,ecg1_wave,ecg2_wave,xpos_ecg1=20,xpos_ecg2=20,xpos_spo2=20;
IntBuffer ecg = IntBuffer.allocate(1024);
ByteBuffer bb = ByteBuffer.allocate(BSIZE);
Label l;
Font fpi = new Font("",Font.BOLD,10);
int x=0, p, q, r, y, px, pp, pq, pr, py = 0,s,buff_reset=0;
public class samplehexa implements Runnable {
    Graphics g2d = (getGraphics());
    public void run()
    {
        int port = 4444;
        try
        {
            ServerSocket server = new ServerSocket(port);
            System.out.println("server's IP adress: "+InetAddress.getLocalHost()+"\nPort Number: "+server.getLocalPort());
            System.out.println("waiting for connection...");
            while(true)
            {
                Socket connect=server.accept();
                System.err.println("Connected...");
                System.out.println("Connected to ..." + connect.getInetAddress() +" with port number: "+connect.getPort());
                ArrayList<String> lst = new ArrayList<String>();
                short[] ECG=new short[100];
                while(connect.isConnected())
                {
                    int red = -1;
                    byte[] buffer = new byte[64*1024];
                    while ((red = connect.getInputStream().read(buffer)) > -1)
                    {
                        StringBuilder data1 = new StringBuilder();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        baos.write(buffer, 0, red);
                        byte[] bytes = baos.toByteArray();
                        StringBuilder newdata = new StringBuilder();
                        for (byte b : bytes) {
                            newdata=data1.append(String.format("%02X ", b));
                        }
                        String content=newdata.toString();
                        int count=0;
                        String[] array=content.split(" ");
                        for(int m=0;m<array.length;m++) {
                            String inside_loop=array[m];
                            lst.add(inside_loop);
                        }
                        while (lst.contains("02") && lst.contains("03")) {
                            if(!(lst.get(0).contains("02"))) {
                                int value = lst.indexOf("02");
                                lst.subList(0, value).clear();
                            } else {
                                int start =lst.indexOf("02");
                                int end =lst.indexOf("03");
                                if(start<end) {
                                    ArrayList<String> sub_list = new ArrayList<String>();
                                    for(int k=start;k<=end;k++) {
                                        String hex=lst.get(k);
                                        sub_list.add(hex);
                                    }
                                    String[] newres = new String[sub_list.size()];
                                    for(int h=0;h<sub_list.size();h++) {
                                        newres[h]=sub_list.get(h);
                                    }
                                    lst.subList(start, end+1).clear();
                                    int convert=Integer.parseInt(newres[1],16);
                                    int check=convert-1;
                                    int i=0;
                                    if(check==newres.length-1) {
                                        try {
                                            Thread.sleep(4);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        if(newres[0].contains("02") && newres[check].contains("03")) {
                                            i=i+3;
                                            if(newres[i].contains("21")){
                                                i++;
                                                int[] Spo2_wave=new int[newres.length];
                                                Spo2_wave[i]=Integer.parseInt(newres[i],16);
                                                spo2_wave=Spo2_wave[i];
                                                r=spo2_wave;
                                                //-->SPO2 WaveForm Design Check This
                                                for(int k=0;k<10;k++){
                                                    g2d.setColor(Color.black);
                                                    g2d.drawLine(xpos_spo2+k,431,xpos_spo2+k,555);
                                                }
                                                g2d.setColor(Color.white);
                                                g2d.drawLine(xpos_spo2,555-pr,xpos_spo2+1,555-r);
                                                pr=r;
                                                xpos_spo2=xpos_spo2+2;
                                                if(xpos_spo2==828){
                                                    xpos_spo2=16;
                                                }
                                                //--<SPO2 WaveForm Design Check This
                                                i++;
                                                int[] Bar_graph=new int[newres.length];
                                                Bar_graph[i]=Integer.parseInt(newres[i],16);
                                                i++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (IOException e)
        {
            System.err.println("Error in connection");
            System.exit(0);
        }
    }
}



  
    设计代码
  


public class sample extends samplehex {

public static List<int[]> map = new ArrayList<int[]>();
static Button Connect;
int temp=0,move=0,xpos=20,j=0;
Stroke str = new BasicStroke();

public sample() {
    super();
    l = new Label("ECG Lead1");
    l.setBackground(Color.black);
    l.setForeground(Color.green);
    l.setFont(fpi);
    l.setBounds(0,0,60,8);
    setSize(1025, 730);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.DARK_GRAY);
    setLayout(null);
    setVisible(true);
    setLocationRelativeTo(null);
    jp2.setSize(830,250);
    jp2.setLocation(5,335);
    jp2.setLayout(null);
    jp2.setBackground(Color.black);
    jp2.setVisible(true);
    jp2.getGraphics();
    add(jp2);
    Connect = new Button("Connect");
    Connect.setBounds(850,660,80,30);
    Connect.setVisible(true);
    Connect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            new Thread(new samplehexa()).start();
            //drawLines(getGraphics());
            //screen1.getGraphics();
        }
    });
    add(Connect);
}
public static void main(String[] args) {
    @SuppressWarnings("unused")
    sample s = new sample();
}


}

最佳答案

创建一个从JPanel扩展的类,覆盖它的paintComponent方法并在此处呈现输出

以该面板的一个实例为例,将其添加到所需的任何容器中,例如JFrameJInternalFrame,另一个面板,

记住,Swing是单线程的,不是线程安全的。 UI的所有更新都必须在事件分发线程的上下文中进行。有关更多详细信息,请参见Concurrency in Swing

关于java - 我如何将画线放入JInternalFrame内,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33968240/

10-09 05:09