import java.awt.*;
import javax.swing.*;
import java.util.Random;public class JDrawLine {
public static void main(String arags[]){
JLine jl = new JLine();
jl.show();
}
}
class JLine extends JFrame{
public JLine(){
JrawLine jl = new JrawLine();
jl.setSize(800,680);
this.setSize(850,700);
Container con = this.getContentPane();
con.add(jl);
}
}class JrawLine extends JPanel implements Runnable{
int x = 0,y = 0; //起始位置
int x1 = 0,y1 = 0; //画图的活动抽
int sumX = 0; //画多少跟时间线
int sumY = 0; //Y故障的总数 int spaceX,spaceY,gz; //x间距 y间距
Thread th;
Random r = new Random();
Graphics g;
Boolean flay = true;
public JrawLine(){
this.x1 = 100;// Integer.parseInt(getParameter("x"));
this.y = 500;//Integer.parseInt(getParameter("y"));
this.sumX = 10;// Integer.parseInt(getParameter("sumX"));
this.sumY = 5;//Integer.parseInt(getParameter("sumY"));
g = super.getGraphics();
y1 = y;
th = new Thread(this);
spaceX = (this.getWidth() - x1) / sumX;
x = x1 + spaceX;
spaceY = (y - 20) / sumY;
th.start();
}
public void run(){
try{
while(true){
gz = (r.nextInt(sumY)+1) * spaceY;
th.sleep(500);
if(x + spaceX >= this.getWidth() - 10){
flay = true;
super.repaint();
x = x1 + spaceX;
}
this.paint(g);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void update(){
}
public void paint(Graphics g){
if(flay == true){
g.drawLine(x1,y1,this.getWidth()-10,y1);
g.drawLine(x1,y1,x1,10);
huaX(sumX,spaceX);
huaY(sumY,spaceY);
flay = false;
}
g.drawLine(x,y,x+spaceX,y1-gz);
g.fillOval(x-5,y-5,10,10);
this.validate();
x += spaceX;
y = y1-gz;
}
void huaX(int space,int amount){
while(space > 0){
//g.drawLine(x1+space*amount ,y1+5,x1+space*amount,y1-5);
g.drawLine(x1+(space*amount) ,y1+5,x1+(space*amount),0);
System.out.println(x1+space*amount);
g.drawString(space + "秒",x1+space*amount,y1 + 20);
space --;
amount --;
}
}
void huaY(int space,int amount){
while(space > 0){
//g.drawLine(x1-5,(y1-10)-space*amount,x1+5,y1-10-space*amount);
g.drawLine(x1-5,(y1-10)-space*amount,this.getWidth(),y1-10-space*amount);
g.drawString(space +"故障",x1-20,y1-10-space*amount);
space --;
amount --;
}
}}
import javax.swing.*;
import java.util.Random;public class JDrawLine {
public static void main(String arags[]){
JLine jl = new JLine();
jl.show();
}
}
class JLine extends JFrame{
public JLine(){
JrawLine jl = new JrawLine();
jl.setSize(800,680);
this.setSize(850,700);
Container con = this.getContentPane();
con.add(jl);
}
}class JrawLine extends JPanel implements Runnable{
int x = 0,y = 0; //起始位置
int x1 = 0,y1 = 0; //画图的活动抽
int sumX = 0; //画多少跟时间线
int sumY = 0; //Y故障的总数 int spaceX,spaceY,gz; //x间距 y间距
Thread th;
Random r = new Random();
Graphics g;
Boolean flay = true;
public JrawLine(){
this.x1 = 100;// Integer.parseInt(getParameter("x"));
this.y = 500;//Integer.parseInt(getParameter("y"));
this.sumX = 10;// Integer.parseInt(getParameter("sumX"));
this.sumY = 5;//Integer.parseInt(getParameter("sumY"));
g = super.getGraphics();
y1 = y;
th = new Thread(this);
spaceX = (this.getWidth() - x1) / sumX;
x = x1 + spaceX;
spaceY = (y - 20) / sumY;
th.start();
}
public void run(){
try{
while(true){
gz = (r.nextInt(sumY)+1) * spaceY;
th.sleep(500);
if(x + spaceX >= this.getWidth() - 10){
flay = true;
super.repaint();
x = x1 + spaceX;
}
this.paint(g);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void update(){
}
public void paint(Graphics g){
if(flay == true){
g.drawLine(x1,y1,this.getWidth()-10,y1);
g.drawLine(x1,y1,x1,10);
huaX(sumX,spaceX);
huaY(sumY,spaceY);
flay = false;
}
g.drawLine(x,y,x+spaceX,y1-gz);
g.fillOval(x-5,y-5,10,10);
this.validate();
x += spaceX;
y = y1-gz;
}
void huaX(int space,int amount){
while(space > 0){
//g.drawLine(x1+space*amount ,y1+5,x1+space*amount,y1-5);
g.drawLine(x1+(space*amount) ,y1+5,x1+(space*amount),0);
System.out.println(x1+space*amount);
g.drawString(space + "秒",x1+space*amount,y1 + 20);
space --;
amount --;
}
}
void huaY(int space,int amount){
while(space > 0){
//g.drawLine(x1-5,(y1-10)-space*amount,x1+5,y1-10-space*amount);
g.drawLine(x1-5,(y1-10)-space*amount,this.getWidth(),y1-10-space*amount);
g.drawString(space +"故障",x1-20,y1-10-space*amount);
space --;
amount --;
}
}}
if(flay == true){
g.drawLine(x1,y1,this.getWidth()-10,y1);
g.drawLine(x1,y1,x1,10);
// 添加
this.g = g;
huaX(sumX,spaceX);
huaY(sumY,spaceY);
flay = false;
}
您好!怎么我的线都变形了,而且线程没用了,可是我在applet可以啊为什么