Java_GUI中实现paintComponent方法并画背景图片和曲线的方法

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class PaintComponentTestData extends JPanel implements Runnable{
			Object obj = new Object();
			private static final long serialVersionUID = 1L;
			private Line2D line;
			private Point2D start;
			private Point2D end;
			
			private String dir = null;
			private String Name = null;
			double start_x = 7 ,start_y = 350;
			double end_x = 70,end_y  = 350;
			int i=0,count = 1;
			boolean flag = true;
			boolean Flag = true;
			public ArrayList<Point2D> list = new ArrayList<Point2D>();
			ImageIcon MyImage = null;
			PaintComponentTestData(String dir,String Name){
				this.dir = dir;
				this.Name = Name;
				MyImage = new ImageIcon("Face-MyWindow.gif");
			}
			public void paintComponent(Graphics g){
					super.paintComponent(g);
					g.drawImage(MyImage.getImage(),0,0,this.getWidth(),this.getHeight(),null);
					float lineWidth =2f;
					Graphics2D g2 = (Graphics2D)g;
					g2.setStroke(new BasicStroke(lineWidth));
					g2.setColor(Color.black);
					synchronized(obj){
						while(i<list.size()-2){
						start = list.get(i);
						end = list.get(i+1);
						line = new Line2D.Double(start,end);
						g2.draw(line);
						i+=1;
					}
						i = 0;
					}
				}
			public void run(){
				try {
					if(dir==null&&Name==null){
						return;
					}
						
						while(count!=0)
						{
							if(Flag==false) {list.clear();break;}
							boolean flag_1 =true;
							synchronized(obj){
								list.clear();
								i=0;
							}
							if(flag ==true){
								start_x = 7 ;start_y = 350;
								end_x = 70;end_y = 350;
								start = new Point2D.Double(start_x,start_y);
								end = new Point2D.Double(end_x,end_y);
								list.add(start);
								list.add(end);
								flag = false;
							}else{
								start_x = 7 ;start_y = 350;
								end_x = 7;end_y = 350;
								start = new Point2D.Double(start_x,start_y);
								end = new Point2D.Double(end_x,end_y);
								list.add(start);
								list.add(end);
							}
							FileReader file = null;
							try{
								file = new FileReader(dir+"\\"+Name);
							}catch (FileNotFoundException e){
								e.printStackTrace();
							}
							BufferedReader bufr = new BufferedReader(file);
							String line = null;
							int x = 0;
							int cc = 0;
							int dd = 0;
							double [] a = new double[4000];
							while((line = bufr.readLine())!=null){
								if(flag_1 == true){
									flag_1 = false;
									line = "";
									continue;
								}
								else{
									String s = null;
									Fun_Split f = new Fun_Split();
									s = f.SplitDemo_line(line," +", 1);
									double value = Integer.parseInt(s);
									if(x<4000){
										a[x++] = value;
									}
									else{
										double [] b = new double[4000];
										double [] c = new double[2000];
										double [] d = new double[1000];
										b = new FECG().ECG(a);
										for(int i=0;i<4000;i+=2){
											c[cc++] = (b[i]+b[i+1])/2;
										}
										for(int i=0;i<2000;i+=2){
											d[dd++] = (c[i]+c[i+1])/4;
										}
										for(int i=0;i<1000;i++){
											value = (d[i]+150)*1.5;
											if(value>350){
												value =700-value;
											}
											end_y= value;
											end_x +=1.5;
											if(end_x>680) break;
											list.add(new Point2D.Double(end_x,end_y));
											repaint();
											try{
												Thread.sleep(8);
											}catch(InterruptedException e){
												e.printStackTrace();
											}
										}
										if(x>=4000){
											break;
										}
									}
								}
							}
						}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			public void setFlag(boolean Flag){
				this.Flag = Flag;
			} 
		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值