您的位置:首页 > 产品设计 > UI/UE

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

2015-10-21 14:41 190 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: