一个动态显示问题,GUI高手请进!(有源码)

yanlinlin 2005-02-28 03:28:56
下面的程序希望通过一个算法来动态画出一个简单图形
可是在开始按钮里加循环去不能够实现,请高手指教!!
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import javax.swing.*;
import java.util.Vector;
import java.util.Date;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.lang.*;


//定义myQuadCurve类,扩展Frame类,加入myQuadCurvePanel
public class ShowArithmeticTest1 extends JFrame
{
public ShowArithmeticTest1(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});


JPanel showPanel = createShowPanel();

this.setSize(500,400);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().setBackground(Color.black);
this.getContentPane().add(showPanel,BorderLayout.CENTER);
this.show();
}
public JPanel createShowPanel()//**实时动画显示面板
{
JPanel showPanel = new JPanel();
myQuadCurvePaneltest p = new myQuadCurvePaneltest();
p.setSize(400,300);
p.setBackground(Color.black);

showPanel.setLayout(new BorderLayout());
showPanel.setBorder(BorderFactory.createTitledBorder("实时动画显示"));


showPanel.add(p,BorderLayout.CENTER);
return showPanel;
}

//定义mian()方法,显示窗体
public static void main(String[] args){
ShowArithmeticTest1 f = new ShowArithmeticTest1();

}
};


//定义myQuadCurvePanel类,扩展JPanel类!
class myQuadCurvePaneltest extends JPanel implements ActionListener
{


Vector queue = new Vector();
BorderLayout borderLayout = new BorderLayout();
Panel pdown = new Panel();
Checkbox check1,check2;
JButton btn;
JLabel p1,p2;
JLabel L1,L2,L3;
Point[] x = new Point[2];
int i,j = 0,k,flag = 0;
String ts1,ts2,ts3;
////////////////////////////////////////////////////////////
public double x1,y1,x2,y2;
public int countT,count;
public double newx1,newy1,newx2,newy2;
public double t1,v1,w1,t2,v2,w2;
public double d1,d2,k1,k2,ko,kf,T;
public double pi = Math.PI;
public boolean s = true;//对应s=0

public double l12d,f12d,l12,f12,f,e1,e2,e3;

double sign0,rd,ld,fai,r;

public void init(){

T = 1.5;
countT = 50;
count = 1;

l12d =3;
f12d = pi*150/100;
d1 = 0;
d2 = -0.5;
k1=0.2;k2 =0.2;
x1 = 2; y1 =1; t1 = pi*1/2;
x2 = 1; y2 =0; t2 = pi*3/2;

v1 = 0.1;w1 = 0.05;v2 = 0;w2 = 0;
rd = f12d;
ld = l12d;
fai = rd;



ko = 1;kf =1;


newx1 = x1+d1*Math.cos(pi-t1);
newy1 = y1-d1*Math.sin(pi-t1);
newx2 = x2+d2*Math.cos(pi-t2);
newy2 = y2-d2*Math.sin(pi-t2);

// x[0] = new Point(int(x1*10),int(y1*10));
//x[1] = new Point(int(x2*10),int(y2*10));


}

//**控制算法
public void ArithmaticForTwo1(){



//for (count=1;count<=countT;count++){


if (count<50){
v1=0.1;
w1=0;}
else if (count<71){
v1=0.1;
w1=-0.05;}
else if (count<110){
v1 = 0.1;
w1=0;}
else if (count<180){
v1=0.1;
w1=-0.05;}
else{
v1=0.1;
w1=0;}




if(ko*kf>0)
sign0 = 1;
else if(ko*kf==0)
sign0 = 0;
else
sign0 = -1;

if ( (sign0 < 0)&&(Math.abs(ko-kf)>30) )
s =!s;

if(s == true)
fai = Math.atan((newy2-newy1)/(newx2-newx1)) - t1;
else
fai = pi+Math.atan((newy2-newy1)/(newx2-newx1)) - t1;


while (fai>=2*pi){ fai = fai-2*pi;}

while (fai<0){fai = fai+2*pi;}

r = fai+t1-t2;

l12 = Math.sqrt((newx1-newx2)*(newx1-newx2)+(newy1-newy2)*(newy1-newy2));


f = rd-fai;
while (f>pi)
f = f-2*pi;

while (f<=-pi)
f = f+2*pi;

System.out.println("fai ="+fai+"\n\n");
System.out.println("l12 ="+l12+"\n\n");
System.out.println("r ="+r+"\n\n");
System.out.println("f ="+f+"\n\n");




...全文
140 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanlinlin 2005-03-01
  • 打赏
  • 举报
回复
angues1980(石头心) 批评的对!
我把关键地方说一下!分当然可以加,400分都给也毫无问题,这是小事情!
这段程序是由一个算法来循环给出图形的坐标值,我希望按下 开始按钮后就能动态的将图形显示出来,按钮事件 的程序我这么写的:
public void actionPerformed(ActionEvent e)
{

if (e.getSource()==btn)
{
if(count<=countT)//countT是算法周期数,count是计数器
{

ArithmaticForTwo1();//算法

try{
Thread.sleep(300);
}catch(InterruptedException ex){}
repaint();//画图
count = count + 1;
}

else
{
return;
}
}
}
这样写时,每点击一次就画一次,没有什么问题。
因为学要连续动态显示,所以我将if换成while循环来实现,程序如下:
public void actionPerformed(ActionEvent e)
{

if (e.getSource()==btn)
{
while(count<=countT)
{

ArithmaticForTwo1();

try{
Thread.sleep(300);
}catch(InterruptedException ex){}
repaint();
count = count + 1;
}


}
}

这样却不能够实现动态显示,请教?

yanlinlin 2005-03-01
  • 打赏
  • 举报
回复
谢谢大家,搞定了!
yanlinlin 2005-03-01
  • 打赏
  • 举报
回复
真诚感谢chinajava的帮助!!
现在能够动态的画了,但还有一个很奇怪的问题,程序在while内有个repaint();同时在ArithmaticForTwo1();程序段中也有个repaint(),这样可以动态显示,但好像显示的有问题!
当我把ArithmaticForTwo1();中的renpaint()去掉后,又不能够动态显示了,实在让我不明白?
还请指教!!!
fightboy 2005-03-01
  • 打赏
  • 举报
回复
剩下的如chinajava(chinajava)所说
fightboy 2005-03-01
  • 打赏
  • 举报
回复
友情提醒:

表忘了在

public void paint(Graphics g)

的第一句加上:

super.paint(g);
chinajava 2005-03-01
  • 打赏
  • 举报
回复
public void actionPerformed(ActionEvent e)
{

if (e.getSource()==btn)
{
new Thread(new Runnable(){
public void run(){
while(count<=countT)
{

ArithmaticForTwo1();

try{
Thread.sleep(1000);
}catch(InterruptedException ex){}
repaint();
count = count + 1;
}
}
}).start();
}
}
angues1980 2005-02-28
  • 打赏
  • 举报
回复
才给30分,还要看这么长的程序,难怪不会有人回。
建议楼主把关键的地方找出来再贴(比如算法那里贴出来谁看啊)
yanlinlin 2005-02-28
  • 打赏
  • 举报
回复
up一下,请高手帮忙!
yanlinlin 2005-02-28
  • 打赏
  • 举报
回复
//**接上面的程序**
v2 =( (-d2*Math.cos(r)/l12)*(k1*(ld-l12)+Math.cos(fai)*v1-d1*Math.sin(fai)*w1)+( d2*Math.sin(r)*(k2*f-v1*Math.sin(fai)/l12+w1-d1*w1*Math.cos(fai)/l12 ) ) )/(d2/l12*((-Math.cos(fai)*Math.cos(fai))-Math.sin(fai)*Math.sin(fai)));
w2 =( (Math.sin(r)/l12)*(k1*(ld-l12)+Math.cos(fai)*v1-d1*Math.sin(fai)*w1)+( Math.cos(r)*(k2*f-v1*Math.sin(fai)/l12-d1*w1*Math.cos(fai)/l12 ) ) )/(d2/l12*((-Math.cos(fai)*Math.cos(fai))-Math.sin(fai)*Math.sin(fai)));
e1 = Math.abs(ld-l12);
e2 = Math.abs(rd-fai);
e3 = Math.abs(t2-t1);

kf = (newy2-newy1)/(newx2-newx1);

if (Math.abs(w1) < 0.005)
{
x1 = x1+v1*T*Math.cos(t1);
System.out.println("x1 ="+x1+"\n\n");
y1 = y1+v1*T*Math.sin(t1);
}
else
{

x1 = x1 + 2*(v1/w1)*Math.sin(0.5*w1*T)*Math.cos(0.5*w1*T+t1);
System.out.println("x1 ="+x1+"\n\n");
y1 = y1 + 2*(v1/w1)*Math.sin(0.5*w1*T)*Math.sin(0.5*w1*T+t1);
}
newx1 = x1+d1*Math.cos(pi-t1);
newy1 = y1-d1*Math.sin(pi-t1);

t1= t1+w1*T;

if(Math.abs(w2)<0.005)
{
x2 = x2+v2*T*Math.cos(t2);
//System.out.println("x2 ="+x2+"\n\n");
y2 = y2+v2*T*Math.sin(t2);
}
else
{
x2 = x2 + 2*(v2/w2)*Math.sin(0.5*w2*T)*Math.cos(0.5*w2*T+t2);
y2 = y2 + 2*(v2/w2)*Math.sin(0.5*w2*T)*Math.sin(0.5*w2*T+t2);

}
newx2 = x2+d2*Math.cos(pi-t2);
newy2 = y2-d2*Math.sin(pi-t2);
ko = (newy2-newy1)/(newx2-newx1);

t2 = t2 + T*w2;

System.out.println("count ="+count+"\n"+
"x1 ="+x1+"\n"+
"e1 ="+e1+"\n"+
"e2 ="+e2+"\n"+
"v2 ="+v2+"\n"+
"w2 ="+w2 );
ts1 = "Point1:"+"x1:"+x1+","+"y1:"+y1;
ts2 = "Point1:"+"x2:"+x1+","+"y2:"+y2;
// ts3 = curve.getX2()+","+curve.getY2();
p1.setText(ts1);
p2.setText(ts2);
repaint();
//t3.setText(ts3);
// x[0] = new Point(int(x1*10),int(y1*10));
//x[1] = new Point(int(x2*10),int(y2*10));



//}//对应for循环
}//对应构造方法
//**************************************************************************//



//定义myQuadCurvePanel的构造器!
public myQuadCurvePaneltest(){

init();
/* x[0] = new Point(10,150);
x[1] = new Point(100,50);
x[2] = new Point(200,150);*/
this.setLayout(borderLayout);
btn = new JButton("run");
p1 = new JLabel();
p2 = new JLabel();
//t3 = new JLabel();
L1 = new JLabel();
L2 = new JLabel();
// L3 = new JLabel();
L1.setText("Point1:");
L2.setText("Point2:");
//L3.setText("Point3:");
//ts1 = x[0].x + "," + x[0].y;
//ts2 = x[1].x + "," + x[1].y;
// ts3 = x[2].x + "," + x[2].y;
p1.setText("0.0");
p2.setText("0.0");
// t3.setText(ts3);1

pdown.setBackground(Color.pink);
this.add(pdown,BorderLayout.SOUTH);
pdown.add(btn);
btn.addActionListener(this);
pdown.add(L1);
pdown.add(p1);
pdown.add(L2);
pdown.add(p2);
// pdown.add(L3);
//pdown.add(t3);



//ArithmeticForTwo1();

}

public void actionPerformed(ActionEvent e)
{

if (e.getSource()==btn)
{
if(count<=countT)
{

ArithmaticForTwo1();

try{
Thread.sleep(300);
}catch(InterruptedException ex){}
// repaint();
count = count + 1;
}

else
{
return;
}
}
}

/*
* 画图
*/
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
// Ellipse2D.Double robot1 = new Ellipse2D.Double(x1*10,y1*10,6,6);
//robot1.isEmpty() = false;
g2.draw(new Ellipse2D.Double(x1*10,y1*10,6,6));
// Ellipse2D.Double robot2 = new Ellipse2D.Double(x2*10,y2*10,6,6);
//robot2.isEmpty() = false;
g2.draw(new Ellipse2D.Double(x2*10,y2*10,6,6));
//g2.setColor(Color.green);
//Line2D.Double line12= new Line2D.Double(x1*10+2,y1*10+2,x2*10,y2*10);
g2.draw(new Line2D.Double(x1*10+2,y1*10+2,x2*10,y2*10));


}
};

//**程序结束

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧