在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();
}
}
答案 0 :(得分:2)
创建一个从<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="pagination-centered">
<nav>
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
延伸的类,覆盖它的JPanel
方法并在那里渲染输出
获取此面板的一个实例,并将其添加到您喜欢的容器中,例如paintComponent
,JFrame
,另一个面板,
请记住,Swing是单线程的,而不是线程安全的。 UI的所有更新必须在Event Dispatching Thread的上下文中进行。有关详细信息,请参阅Concurrency in Swing