gpt4 book ai didi

java - 我如何将绘图线放入 JInternalFrame 内部

转载 作者:行者123 更新时间:2023-12-02 04:09:04 25 4
gpt4 key购买 nike

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

这是我的代码,它将在 jframe 中创建波形。我怎样才能将该波形放入 jpanel jp2 中。

Server code

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

designing code

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com