麻烦各位前辈了,
想问个问题。想做个gui,条件如下
1。我有一套计算坐标点的公式,就是计算后是x,y位置,是double型的,并且位置一直变化的,我想让这些位置动起来。
2。做出gui.让点的显示到gui上。
3。从这些点中,我指定一个点。我可以控制这个点。比如控制行动方向。一开始我能控制的是随着这一堆点点一起走动,加个中断按钮后,我就可以控制那个了
可能说的复杂很难懂,说白了就是,
我想在界面中显示一堆小点点,前提是小点点的x、y的值,我都是已知的。就是我可以从本地文件txt中直接提取到的。
然后在这一堆小点点中,有一个我能控制的。
附上我的程序,我现在可以得到我想要的点,ship0是我能控制的,我就是不会做gui 还有如何加上中断,程序比较长 麻烦懂行的帮看看public class AttributionJudge { public int gID ;
public int tID ;
public double gCo, gSp ;
public double tCo, tSp ;
public AttributionJudge( double gCourse, double gSpeed, int gVesselID,
double tCourse, double tSpeed, int tVesselID){
gCo = gCourse ;
gSp = gSpeed ;
gID = gVesselID ;
tCo = tCourse ;
tSp = tSpeed ;
tID = tVesselID ;
}
public int judge(){
int sameCluster = 0 ;
int sameCo = 0 ;
int sameSp = 0 ;
/* THINK */
if( Math.abs(gCo-tCo) <= 2.0
|| Math.abs(gCo-tCo) >= 358.0 ) sameCo = 1;
if( Math.abs(gSp-tSp) <= 3.0 ) sameSp = 1;
if( sameCo == 1 && sameSp == 1 ) sameCluster = 1 ;
return sameCluster;
}
}public class ClusterAvoidanceForce {
public static ClusterAvoidReturn clusterAvoidanceForceFunc
( ShipCondition[] shipSET, int givenSHIP, double dtMIN )
{
int it = 0 ;
int tcpaI ;
int avoidanceID = -1 ;
int caseId = 0 ;
int[] ship = new int[shipSET.length] ;
double pi = 3.14159265358979323846 ;
double pid2 = pi / 2.0 ;
double degToRAD = pi / 180.0 ;
double baseX, baseY ;
double deltaX, deltaY ;
double alphaI ;
double rI ;
double dX, dY ;
double cR, vR ;
double gamma ;
double tcpaMin ;
double dist = 0.0, rad = 0.0 ;
double avoidanceDist, avoidanceRad ;
double deg5, deg1125, deg2475 ;
double diff ;
double x1, y1, x2, y2 ;
double dt ;
double clusterForce = 0.0 ;
double clusterRad = 0.0 ;
double u, w ;
double ts, ts2, tc, tc2;
double[] dcpa = new double [shipSET.length] ;
double[] tcpa = new double [shipSET.length] ; dt = dtMIN / 60.0 ;
deg5 = 5.0 * degToRAD ;
deg1125 = 112.5 * degToRAD ;
deg2475 = 247.5 * degToRAD ;
baseX = shipSET[givenSHIP].xMileOLD ;
baseY = shipSET[givenSHIP].yMileOLD ;for( int i = 0 ; i < shipSET.length ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){ if( givenSHIP != i ) {
x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
x2 = shipSET[i].xMileNOW ;
y2 = shipSET[i].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
if( dist < shipSET[givenSHIP].decisionMD ){
AttributionJudge aj ;
aj = new AttributionJudge( shipSET[givenSHIP].courseNaviRAD,
shipSET[givenSHIP].speedNaviKT,
shipSET[givenSHIP].vesselID,
shipSET[i].courseNaviRAD,
shipSET[i].speedNaviKT,
shipSET[i].vesselID ) ;
int aJudge = aj.judge() ;
if( aJudge == 0 ){
deltaX = ( shipSET[i].xMileNOW - baseX )
-( shipSET[givenSHIP].xMileNOW - baseX ) ;
deltaY = ( shipSET[i].yMileNOW - baseY )
-( shipSET[givenSHIP].yMileNOW - baseY ) ;
if( deltaX == 0.0 && deltaY == 0.0 ){
alphaI = 0.0 ;
}else{
alphaI = Math.atan2(deltaX,deltaY) ;
}
rI = Math.sqrt(deltaX*deltaX+deltaY*deltaY ) ;
dX = deltaX - ( shipSET[i].xMileOLD - baseX ) ;
dY = deltaY - ( shipSET[i].yMileOLD - baseY ) ;
if( dX == 0.0 && dY == 0.0 ){
cR = 0.0 ;
}else{
cR = Math.atan2(dX,dY) ;
}
vR = Math.sqrt(dX*dX+dY*dY)/dt ;
gamma = cR-alphaI-pi ;
tc=Math.cos(gamma);
tc2=Math.abs(tc);
if(tc2<0.0000001) tc=0.0;
tcpa[it] = rI*tc/vR ;
if( tcpa[it] >= 0.0 ){
ts = Math.sin(gamma);
ts2 = Math.abs(ts);
if( ts2<0.0000001 ) ts = 0.0;
dcpa[it] = rI*Math.abs(ts) ;
}else{
dcpa[it] = rI ;
}
ship[it] = i ;
it = it + 1 ;
}
}
}}
} tcpaMin = 100000000000000.0 ;
tcpaI = -1;
for( int i = 0 ; i < it ; i++ ){
if( dcpa[i] < shipSET[givenSHIP].dcpaJUDGE ){
if( tcpa[i] >= 0.0 && tcpa[i] < tcpaMin ){
tcpaMin = tcpa[i] ;
tcpaI = i ;
}
}
} if( tcpaI == -1 ){
clusterForce = -1.0;
clusterRad = -1.0;
}else{
avoidanceID = ship[tcpaI] ;
x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
x2 = shipSET[avoidanceID].xMileNOW ;
y2 = shipSET[avoidanceID].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
rad = p1.bearing( p2, shipSET[givenSHIP].courseNaviRAD ) ;
avoidanceDist = dist ;
avoidanceRad = rad ;
diff = avoidanceRad - shipSET[givenSHIP].courseNaviRAD ;
if( diff >= 0.0 ) {
if( diff <= deg5 ) caseId = 0 ;
if( diff > deg5 && diff <= deg1125 ) caseId = 1 ;
if( diff > deg1125 && diff <= deg2475 ) caseId = 2 ;
if( diff > deg2475 ) caseId = 3 ;
}else{
diff = Math.abs( diff ) ;
if( diff <= deg5 ) caseId = 0 ;
if( diff > deg5 && diff <= deg1125 ) caseId = 3 ;
if( diff > deg1125 && diff <= deg2475 ) caseId = 2 ;
if( diff > deg2475 ) caseId = 1 ;
}
clusterForce = avoidanceDist ;
switch( caseId ){
case 0:
clusterRad = shipSET[givenSHIP].courseNaviRAD
+ shipSET[givenSHIP].headOnDEG * degToRAD ;
if( clusterRad >= (pi*2.0) ) clusterRad = clusterRad - pi*2.0 ;
break;
case 1:
clusterRad = avoidanceRad ;
break;
case 2:
clusterRad = shipSET[givenSHIP].courseNaviRAD ;
break;
case 3:
clusterRad = shipSET[givenSHIP].courseNaviRAD ;
break;
default:
break;
}
}
想问个问题。想做个gui,条件如下
1。我有一套计算坐标点的公式,就是计算后是x,y位置,是double型的,并且位置一直变化的,我想让这些位置动起来。
2。做出gui.让点的显示到gui上。
3。从这些点中,我指定一个点。我可以控制这个点。比如控制行动方向。一开始我能控制的是随着这一堆点点一起走动,加个中断按钮后,我就可以控制那个了
可能说的复杂很难懂,说白了就是,
我想在界面中显示一堆小点点,前提是小点点的x、y的值,我都是已知的。就是我可以从本地文件txt中直接提取到的。
然后在这一堆小点点中,有一个我能控制的。
附上我的程序,我现在可以得到我想要的点,ship0是我能控制的,我就是不会做gui 还有如何加上中断,程序比较长 麻烦懂行的帮看看public class AttributionJudge { public int gID ;
public int tID ;
public double gCo, gSp ;
public double tCo, tSp ;
public AttributionJudge( double gCourse, double gSpeed, int gVesselID,
double tCourse, double tSpeed, int tVesselID){
gCo = gCourse ;
gSp = gSpeed ;
gID = gVesselID ;
tCo = tCourse ;
tSp = tSpeed ;
tID = tVesselID ;
}
public int judge(){
int sameCluster = 0 ;
int sameCo = 0 ;
int sameSp = 0 ;
/* THINK */
if( Math.abs(gCo-tCo) <= 2.0
|| Math.abs(gCo-tCo) >= 358.0 ) sameCo = 1;
if( Math.abs(gSp-tSp) <= 3.0 ) sameSp = 1;
if( sameCo == 1 && sameSp == 1 ) sameCluster = 1 ;
return sameCluster;
}
}public class ClusterAvoidanceForce {
public static ClusterAvoidReturn clusterAvoidanceForceFunc
( ShipCondition[] shipSET, int givenSHIP, double dtMIN )
{
int it = 0 ;
int tcpaI ;
int avoidanceID = -1 ;
int caseId = 0 ;
int[] ship = new int[shipSET.length] ;
double pi = 3.14159265358979323846 ;
double pid2 = pi / 2.0 ;
double degToRAD = pi / 180.0 ;
double baseX, baseY ;
double deltaX, deltaY ;
double alphaI ;
double rI ;
double dX, dY ;
double cR, vR ;
double gamma ;
double tcpaMin ;
double dist = 0.0, rad = 0.0 ;
double avoidanceDist, avoidanceRad ;
double deg5, deg1125, deg2475 ;
double diff ;
double x1, y1, x2, y2 ;
double dt ;
double clusterForce = 0.0 ;
double clusterRad = 0.0 ;
double u, w ;
double ts, ts2, tc, tc2;
double[] dcpa = new double [shipSET.length] ;
double[] tcpa = new double [shipSET.length] ; dt = dtMIN / 60.0 ;
deg5 = 5.0 * degToRAD ;
deg1125 = 112.5 * degToRAD ;
deg2475 = 247.5 * degToRAD ;
baseX = shipSET[givenSHIP].xMileOLD ;
baseY = shipSET[givenSHIP].yMileOLD ;for( int i = 0 ; i < shipSET.length ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){ if( givenSHIP != i ) {
x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
x2 = shipSET[i].xMileNOW ;
y2 = shipSET[i].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
if( dist < shipSET[givenSHIP].decisionMD ){
AttributionJudge aj ;
aj = new AttributionJudge( shipSET[givenSHIP].courseNaviRAD,
shipSET[givenSHIP].speedNaviKT,
shipSET[givenSHIP].vesselID,
shipSET[i].courseNaviRAD,
shipSET[i].speedNaviKT,
shipSET[i].vesselID ) ;
int aJudge = aj.judge() ;
if( aJudge == 0 ){
deltaX = ( shipSET[i].xMileNOW - baseX )
-( shipSET[givenSHIP].xMileNOW - baseX ) ;
deltaY = ( shipSET[i].yMileNOW - baseY )
-( shipSET[givenSHIP].yMileNOW - baseY ) ;
if( deltaX == 0.0 && deltaY == 0.0 ){
alphaI = 0.0 ;
}else{
alphaI = Math.atan2(deltaX,deltaY) ;
}
rI = Math.sqrt(deltaX*deltaX+deltaY*deltaY ) ;
dX = deltaX - ( shipSET[i].xMileOLD - baseX ) ;
dY = deltaY - ( shipSET[i].yMileOLD - baseY ) ;
if( dX == 0.0 && dY == 0.0 ){
cR = 0.0 ;
}else{
cR = Math.atan2(dX,dY) ;
}
vR = Math.sqrt(dX*dX+dY*dY)/dt ;
gamma = cR-alphaI-pi ;
tc=Math.cos(gamma);
tc2=Math.abs(tc);
if(tc2<0.0000001) tc=0.0;
tcpa[it] = rI*tc/vR ;
if( tcpa[it] >= 0.0 ){
ts = Math.sin(gamma);
ts2 = Math.abs(ts);
if( ts2<0.0000001 ) ts = 0.0;
dcpa[it] = rI*Math.abs(ts) ;
}else{
dcpa[it] = rI ;
}
ship[it] = i ;
it = it + 1 ;
}
}
}}
} tcpaMin = 100000000000000.0 ;
tcpaI = -1;
for( int i = 0 ; i < it ; i++ ){
if( dcpa[i] < shipSET[givenSHIP].dcpaJUDGE ){
if( tcpa[i] >= 0.0 && tcpa[i] < tcpaMin ){
tcpaMin = tcpa[i] ;
tcpaI = i ;
}
}
} if( tcpaI == -1 ){
clusterForce = -1.0;
clusterRad = -1.0;
}else{
avoidanceID = ship[tcpaI] ;
x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
x2 = shipSET[avoidanceID].xMileNOW ;
y2 = shipSET[avoidanceID].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
rad = p1.bearing( p2, shipSET[givenSHIP].courseNaviRAD ) ;
avoidanceDist = dist ;
avoidanceRad = rad ;
diff = avoidanceRad - shipSET[givenSHIP].courseNaviRAD ;
if( diff >= 0.0 ) {
if( diff <= deg5 ) caseId = 0 ;
if( diff > deg5 && diff <= deg1125 ) caseId = 1 ;
if( diff > deg1125 && diff <= deg2475 ) caseId = 2 ;
if( diff > deg2475 ) caseId = 3 ;
}else{
diff = Math.abs( diff ) ;
if( diff <= deg5 ) caseId = 0 ;
if( diff > deg5 && diff <= deg1125 ) caseId = 3 ;
if( diff > deg1125 && diff <= deg2475 ) caseId = 2 ;
if( diff > deg2475 ) caseId = 1 ;
}
clusterForce = avoidanceDist ;
switch( caseId ){
case 0:
clusterRad = shipSET[givenSHIP].courseNaviRAD
+ shipSET[givenSHIP].headOnDEG * degToRAD ;
if( clusterRad >= (pi*2.0) ) clusterRad = clusterRad - pi*2.0 ;
break;
case 1:
clusterRad = avoidanceRad ;
break;
case 2:
clusterRad = shipSET[givenSHIP].courseNaviRAD ;
break;
case 3:
clusterRad = shipSET[givenSHIP].courseNaviRAD ;
break;
default:
break;
}
}
ClusterAvoidReturn caf = new ClusterAvoidReturn() ;
if( clusterForce >= 0.0 ){
u = shipSET[givenSHIP].clusterAvoidSigA
* ( clusterForce - shipSET[givenSHIP].clusterAvoidSigB ) ;
w = 1.0/(1.0+Math.exp(-u)) ;
if( w < 0.0001 ) w = 0.0 ;
if( w > 0.9999 ) w = 1.0 ;
caf.clusterAvoidForce = clusterForce ;
caf.clusterAvoidRad = clusterRad ;
caf.clusterAvoidWeight = w ;
caf.clusterAvoidTarget = avoidanceID ;
}else{
caf.clusterAvoidForce = 0.0 ;
caf.clusterAvoidRad = 0.0 ;
caf.clusterAvoidWeight = 0.0 ;
caf.clusterAvoidTarget = 0 ;
}
return caf;
}
}public class ClusterAvoidReturn { int clusterAvoidTarget ;
double clusterAvoidForce ;
double clusterAvoidRad ;
double clusterAvoidWeight ;
}public class ContractingForce { public static ContractReturn contractingForceFunc
( ShipCondition[] shipSET, int givenSHIP )
{
int iNo = 1 ;
double x1, y1, x2, y2 ;
double centerX, centerY ;
double rad=0.0, dist=0.0 ;
double u, w ;x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
centerX = x1 ;
centerY = y1 ;
for( int i = 0 ; i < shipSET.length ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){if( i != givenSHIP ){
AttributionJudge aj ;
aj = new AttributionJudge( shipSET[givenSHIP].courseNaviRAD,
shipSET[givenSHIP].speedNaviKT,
shipSET[givenSHIP].vesselID,
shipSET[i].courseNaviRAD,
shipSET[i].speedNaviKT,
shipSET[i].vesselID ) ;
int aJudge = aj.judge() ;
if( aJudge == 1 ){
x2 = shipSET[i].xMileNOW ;
y2 = shipSET[i].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
rad = p1.bearing( p2, shipSET[givenSHIP].courseNaviRAD ) ;
if( dist <= shipSET[givenSHIP].decisionMD
&& aJudge == 1 ){
iNo = iNo + 1 ;
centerX = centerX + x2 ;
centerY = centerY + y2 ;
}
}
} }
}
if( iNo > 1 ){
centerX = centerX / (double)iNo ;
centerY = centerY / (double)iNo ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( centerX, centerY ) ;
dist = p1.distance2D( p2 ) ;
rad = p1.bearing( p2, shipSET[givenSHIP].courseNaviRAD ) ;
}else{
dist = -1.0 ;
rad = shipSET[givenSHIP].courseNaviRAD ;
}ContractReturn cf = new ContractReturn() ;
if( dist >= 0.0 ){
u = shipSET[givenSHIP].contractSigA
* (dist-shipSET[givenSHIP].contractSigB) ;
w = 1.0/(1.0+Math.exp(-u)) ;
if( w < 0.0001 ) w = 0.0 ;
if( w > 0.9999 ) w = 1.0 ;
cf.contractWeight = w ;
cf.contractForce = dist ;
cf.contractRad = rad ;
}else{
cf.contractWeight = 0.0 ;
cf.contractForce = 0.0 ;
cf.contractRad = 0.0 ;
}
return cf;
}
}public class ContractReturn {
double contractForce ;
double contractRad ;
double contractWeight ;}public class DecisionMakingBody {
public static DecisionMakingReturn agentDecisionMaking
( ShipCondition[] shipSET, int givenSHIP, double dtMIN )
{
double pi = 3.14159265358979323846 ;
double pid2 = pi / 2.0 ;
double dmCourseRad = shipSET[givenSHIP].courseNaviRAD ;
double dmSpeedKt = shipSET[givenSHIP].speedNaviKT ;
double fx, fy ;
double fGoal, fContract, fExpand, fCluster ;
double rGoal, rContract, rExpand, rCluster ;GoalReturn goalF
= GoalForce.goalForceFunc( shipSET, givenSHIP ) ;fGoal = shipSET[givenSHIP].metaWeightGOAL ;
rGoal = shipSET[givenSHIP]. goalRad ;ContractReturn contF
= ContractingForce.contractingForceFunc( shipSET, givenSHIP );
fContract = shipSET[givenSHIP].metaWeightCONTRACT
* contF.contractWeight ;
rContract = contF.contractRad ;ExpandReturn expandF
= ExpandingForce.expandingForceFunc( shipSET, givenSHIP );
fExpand = shipSET[givenSHIP].metaWeightEXPAND * expandF.expandWeight ;
rExpand = expandF.expandRad ;ClusterAvoidReturn clusterAF
= ClusterAvoidanceForce.clusterAvoidanceForceFunc
( shipSET, givenSHIP, dtMIN );
fCluster = shipSET[givenSHIP].metaWeightClusterAVOID
* clusterAF.clusterAvoidWeight ;
rCluster = clusterAF.clusterAvoidRad ;/* Composition of Fundamental Forces */
fx = fGoal * Math.sin( rGoal )
+ fContract * Math.sin( rContract )
+ fExpand * Math.sin( rExpand )
+ fCluster * Math.sin( rCluster ) ;
fy = fGoal * Math.cos( rGoal )
+ fContract * Math.cos( rContract )
+ fExpand * Math.cos( rExpand )
+ fCluster * Math.cos( rCluster ) ;int flag = 2 ;
if( fx == 0.0 ) flag = 0 ;
if( fy == 0.0 ) flag = 1 ;/*************** NEW COURSE ***************/
switch( flag ) {
case 0:
if( fy == 0.0 ) dmCourseRad = shipSET[givenSHIP].courseNaviRAD ;
if( fy > 0.0 ) dmCourseRad = 0.0 ;
if( fy < 0.0 ) dmCourseRad = pi ;
break;
case 1:
if( fx == 0.0 ) dmCourseRad = shipSET[givenSHIP].courseNaviRAD ;
if( fx > 0.0 ) dmCourseRad = pid2 ;
if( fx < 0.0 ) dmCourseRad = pi + pid2 ;
break;
case 2:
dmCourseRad = Math.atan2( fy, fx );
if( fx > 0.0 && fy > 0.0 ) dmCourseRad = pid2 - dmCourseRad ;
if( fx > 0.0 && fy < 0.0 ) dmCourseRad = pid2 - dmCourseRad ;
if( fx < 0.0 && fy < 0.0 ) dmCourseRad = pid2 - dmCourseRad ;
if( fx < 0.0 && fy > 0.0 ) dmCourseRad = pi*2.0-(dmCourseRad-pid2) ;
break;
default:
break;
}/*************** JUDGE of Speed Adjustment (start) ***************/SpeedAdjustReturn sd
= SpeedAdjust.SpeedAdjustFunc( shipSET, givenSHIP );
dmSpeedKt = sd.cooperateSpeed ;/*************** JUDGE of Speed Adjustment (end) ***************/DecisionMakingReturn dm = new DecisionMakingReturn() ;
dm.dmCourseRad = dmCourseRad ;
dm.dmSpeedKt = dmSpeedKt ;
return dm ;
}
}public class DecisionMakingReturn { double dmCourseRad ;
double dmSpeedKt ;
}public class ExpandingForce { public static ExpandReturn expandingForceFunc
( ShipCondition[] shipSET, int givenSHIP )
{
double x1, y1, x2, y2 ;
double rad=0.0, dist=0.0 ;
double distDummy = 1000000000.0 ;
double exDistance = 0.0, exRad = 0.0 ;
double pi = 3.14159265358979323846 ;x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
for( int i = 0; i < shipSET.length; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){if( i != givenSHIP ) {
AttributionJudge aj ;
aj = new AttributionJudge( shipSET[givenSHIP].courseNaviRAD,
shipSET[givenSHIP].speedNaviKT,
shipSET[givenSHIP].vesselID,
shipSET[i].courseNaviRAD,
shipSET[i].speedNaviKT,
shipSET[i].vesselID ) ;
int aJudge = aj.judge() ;
if( aJudge == 1 ){
x2 = shipSET[i].xMileNOW ;
y2 = shipSET[i].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
rad = p1.bearing( p2, shipSET[givenSHIP].courseNaviRAD ) ;
if( dist < shipSET[givenSHIP].decisionMD
&& aJudge == 1 ){
if( dist < distDummy ){
exDistance = dist ;
exRad = rad+pi ;
if( exRad > pi*2.0 ) exRad = exRad-pi*2.0 ;
distDummy = dist ;
}
}
}
} }
}
ExpandReturn ef = new ExpandReturn() ;
if( distDummy != 1000000000.0 ){
double u, w ;
u = shipSET[givenSHIP].expandSigA
* (exDistance-shipSET[givenSHIP].expandSigB) ;
w = 1.0/(1.0+Math.exp(-u)) ;
if( w < 0.0001 ) w = 0.0 ;
if( w > 0.9999 ) w = 1.0 ;
ef.expandWeight = w ;
ef.expandForce = exDistance ;
ef.expandRad = exRad ;
}else{
ef.expandForce = 0.0 ;
ef.expandRad = 0.0 ;
ef.expandWeight = 0.0 ;
}
return ef ;
}
}public class ExpandReturn {
double expandForce ;
double expandRad ;
double expandWeight ;
}public class GoalForce { public static GoalReturn goalForceFunc
( ShipCondition[] shipSET, int givenSHIP )
{
double rad=0.0, dist=0.0 ;
double u, w ;
double pi = 3.14159265358979323846 ;
dist = 1.0 ;
rad = shipSET[givenSHIP].courseRadNOW;GoalReturn gr = new GoalReturn() ;
if( dist >= 0.0 ){
u = 1.0;
w = 1.0/(1.0+Math.exp(-u)) ;
if(w < 0.0001) w = 0.0 ;
if(w > 0.9999) w = 1.0 ;
gr.goalWeight = w ;
gr.goalForce = dist ;
gr.goalRad = rad ;
}else{
gr.goalWeight = 0.0 ;
gr.goalForce = 0.0 ;
gr.goalRad = 0.0 ;
}
return gr;
}
}
public class GoalReturn { double goalForce ;
double goalRad ;
double goalWeight ;
}public class Point2D { public double x, y ;
public Point2D( double xx, double yy ){
x=xx ;
y=yy ;
}
public double distance2D( Point2D point2 ){
double dx = ( this.x - point2.x ) ;
double dy = ( this.y - point2.y ) ;
return Math.sqrt( dx*dx + dy*dy ) ;
}
public double bearing( Point2D point2, double co ){
int flag ;
double pi = 3.14159265358979323846;
double pid2 = pi / 2.0 ;
double lx, ly ;
double radian = 0.0 ; lx = point2.x - this.x ;
ly = point2.y - this.y ;
flag = 2 ;
if( lx == 0.0 ) flag = 0 ;
if( ly == 0.0 ) flag = 1 ;
switch( flag ) {
case 0:
if( ly == 0.0 ) radian = co ;
if( ly > 0.0 ) radian = 0.0 ;
if( ly < 0.0 ) radian = pi ;
break;
case 1:
if( lx == 0.0 ) radian = co ;
if( lx > 0.0 ) radian = pid2 ;
if( lx < 0.0 ) radian = pi + pid2 ;
break;
case 2:
radian = Math.atan2( ly, lx );
if( lx > 0.0 && ly > 0.0 ) radian = pid2 - radian ;
if( lx > 0.0 && ly < 0.0 ) radian = pid2 - radian ;
if( lx < 0.0 && ly < 0.0 ) radian = pid2 - radian ;
if( lx < 0.0 && ly > 0.0 ) radian = pi*2.0
- (radian - pid2) ;
break;
default:
break;
}
if( radian >= ( pi*2.0 ) ) radian = radian - ( pi*2.0 ) ;
if( radian < 0.0 ) radian = radian + ( pi*2.0 ) ;
return radian;
}
}public class ShipCondition { int vesselID ; /* Ship Identification Number */
int vesselSTATUS ; /* Ship Status Number */
int goalPortID ; /* Intended Port Destination */
double xMileGOAL, yMileGOAL ;
double payLOAD ; /* Freight of a Ship */
double handlingLOAD ;
double xMileOLD, yMileOLD, courseRadOLD, speedKtOLD ;
double xMileNOW, yMileNOW, courseRadNOW, speedKtNOW ;
double xMileNEW, yMileNEW, courseRadNEW, speedKtNEW ;
double courseNaviRAD, speedNaviKT ;
double metaWeightGOAL, goalSigA, goalSigB ;
double metaWeightCONTRACT, contractSigA, contractSigB ;
double metaWeightEXPAND, expandSigA, expandSigB ;
double metaWeightClusterAVOID, clusterAvoidSigA, clusterAvoidSigB ;
double decisionMD ;
double dcpaJUDGE ;
double headOnDEG ;
double goalRad;
}public class SpeedAdjust { public static SpeedAdjustReturn SpeedAdjustFunc
( ShipCondition[] shipSET, int givenSHIP )
{
int iNo = 1 ;
double x1, y1, x2, y2 ;
double rad=0.0, dist=0.0 ;
double meanSpeed = shipSET[givenSHIP].speedKtNOW ;x1 = shipSET[givenSHIP].xMileNOW ;
y1 = shipSET[givenSHIP].yMileNOW ;
for( int i = 0 ; i < shipSET.length ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){if( i != givenSHIP ){
AttributionJudge aj ;
aj = new AttributionJudge( shipSET[givenSHIP].courseNaviRAD,
shipSET[givenSHIP].speedNaviKT,
shipSET[givenSHIP].vesselID,
shipSET[i].courseNaviRAD,
shipSET[i].speedNaviKT,
shipSET[i].vesselID ) ;
int aJudge = aj.judge() ;
if( aJudge == 1 ){
x2 = shipSET[i].xMileNOW ;
y2 = shipSET[i].yMileNOW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
dist = p1.distance2D( p2 ) ;
rad = p1.bearing( p2, shipSET[givenSHIP].courseNaviRAD ) ;
if( dist <= shipSET[givenSHIP].decisionMD ){
iNo = iNo + 1 ;
meanSpeed = meanSpeed + shipSET[i].speedKtNOW ;
}
}
}}
}
if( iNo > 1 ) meanSpeed = meanSpeed / (double)iNo ;
SpeedAdjustReturn sd = new SpeedAdjustReturn() ;
sd.cooperateSpeed = meanSpeed ;
return sd ;
}
}public class SpeedAdjustReturn { double cooperateSpeed ;
}
import java.util.Random;
public class kaku { public static void main(String[] args){ /** Simulation Basic Data **/
int maximumVESSELS = 21 ;
// int simFIN = 11521 ; /** 8 Days **/
int simFIN = 100 ;
double dtMIN = 1.0 ; /** minute **/
double pi = 3.14159265358979323846 ;
double degToRAD = pi / 180.0 ; ShipCondition[] shipSET = new ShipCondition[maximumVESSELS] ;
for( int i = 0 ; i < maximumVESSELS ; i++ ){
shipSET[i] = new ShipCondition() ;
}
for( int is = 0 ; is < maximumVESSELS ; is++ ){
shipSET[is].vesselID = is ;
shipSET[is].metaWeightGOAL = 10.0 ;
shipSET[is].goalSigA = 5.0 ;
shipSET[is].goalSigB = 1.0 ;
shipSET[is].metaWeightCONTRACT = 1.0 ;
shipSET[is].contractSigA = 10.0 ;
shipSET[is].contractSigB = 1.0 ;
shipSET[is].metaWeightEXPAND = 1.0 ;
shipSET[is].expandSigA = -10.0 ;
shipSET[is].expandSigB = 1.0 ;
shipSET[is].metaWeightClusterAVOID = 100.0 ;
shipSET[is].clusterAvoidSigA = -10.0 ;
shipSET[is].clusterAvoidSigB = 3.0 ; shipSET[is].decisionMD = 6.0 ;
shipSET[is].dcpaJUDGE = 0.75 ;
shipSET[is].headOnDEG = 50.0 ;
shipSET[is].courseRadOLD = pi/2 ; /* Dummy */
shipSET[is].speedKtOLD = 15.0 ;
shipSET[is].courseRadNOW = pi/2 ; /* Dummy */
shipSET[is].speedKtNOW = 15.0 ;
shipSET[is].courseNaviRAD = pi/2 ; /* Dummy */
shipSET[is].speedNaviKT = 15.0 ;
shipSET[is]. goalRad=pi/2;
}
double shipRad=360.0/((double)maximumVESSELS-1)*degToRAD;
for(int i=1;i<maximumVESSELS;i++){
shipSET[i].xMileOLD = shipSET[0].xMileOLD+2.0*Math.cos(shipRad*(double)i) ;
shipSET[i].yMileOLD = shipSET[0].yMileOLD+2.0*Math.sin(shipRad*(double)i) ;
shipSET[i].xMileNOW =shipSET[0].xMileNOW+2.0*Math.cos(shipRad*(double)i) ;
shipSET[i].yMileNOW = shipSET[0].yMileNOW+2.0*Math.sin(shipRad*(double)i) ;
}
Random rand = new Random() ;
// Random rand = new Random(2146351065) ;
for( int clock = 0 ; clock < simFIN ; clock++ ){
if(clock>=20&&clock<25)
shipSET[0].goalRad=pi/3;
if(clock>=25&&clock<70)
shipSET[0].goalRad=pi/2;
if(clock>=70&&clock<75)
shipSET[0].goalRad=pi;
if(clock>=75)
shipSET[0].goalRad=pi/2;
for( int givenSHIP= 0 ; givenSHIP < maximumVESSELS ; givenSHIP ++ ){
/*** Navigation -> Navigation (status = 0 --> 0) ***/
/********************************************************/
/***** Ship Cluster Behavior Model Calculation *****/
/********************************************************/
DecisionMakingReturn dmCoSp
= DecisionMakingBody.agentDecisionMaking(shipSET, givenSHIP, dtMIN); /*** Quick Response ***/
shipSET[givenSHIP].xMileNEW
= shipSET[givenSHIP].xMileNOW + dmCoSp.dmSpeedKt
* dtMIN / 60.0 * Math.sin( dmCoSp.dmCourseRad ) ;
shipSET[givenSHIP].yMileNEW
= shipSET[givenSHIP].yMileNOW + dmCoSp.dmSpeedKt
* dtMIN / 60.0 * Math.cos( dmCoSp.dmCourseRad ) ;
shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad ;
shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt ;
} /** FOR givenSHIP **/
double collisionJudgeDIST = 0.1 ;
for( int i = 0 ; i < maximumVESSELS ; i++ ){
int flagCC = 0 ;
if( shipSET[i].vesselSTATUS == 0 ){
for( int j = i+1 ; j < maximumVESSELS ; j++ ){
if( shipSET[j].vesselSTATUS == 0 ){
double x1 = shipSET[i].xMileNEW ;
double y1 = shipSET[i].yMileNEW ;
double x2 = shipSET[j].xMileNEW ;
double y2 = shipSET[j].yMileNEW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
double dist = p1.distance2D( p2 ) ;
if( dist < collisionJudgeDIST ) flagCC = 1 ;
}
}
}
/* System.out.println( clock + "\u0009" + i + "\u0009" +
shipSET[i].xMileNEW + "\u0009" +
shipSET[i].yMileNEW + "\u0009" +
flagCC+ "\u0009" + shipSET[0].goalRad) ;*/
System.out.println( clock + "," + i + "," +
shipSET[i].xMileNEW + "," +
shipSET[i].yMileNEW + "," +
flagCC+ "," + i); }
/** State Transition **/
for( int i = 0 ; i < maximumVESSELS ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){
shipSET[i].xMileOLD = shipSET[i].xMileNOW ;
shipSET[i].yMileOLD = shipSET[i].yMileNOW ;
shipSET[i].courseRadOLD = shipSET[i].courseRadNOW ;
shipSET[i].speedKtOLD = shipSET[i].speedKtNOW ;
shipSET[i].xMileNOW = shipSET[i].xMileNEW ;
shipSET[i].yMileNOW = shipSET[i].yMileNEW ;
shipSET[i].courseRadNOW = shipSET[i].courseRadNEW ;
shipSET[i].speedKtNOW = shipSET[i].speedKtNEW ;
}
} } /** FOR clock **/ }
}
楼主要画什么?把kaku的计算结果画出来?
每次变化之间间隔100ms
如果楼主想控制其中的那个小红点,
那就要把你的这个点不参加计算才行,
然后用键盘事件来改变这个点的坐标
具体的你也可以琢磨琢磨
另外,这个效果是我把你的坐标放大了10倍画出来的
因为graphics是以屏幕上的像素点为坐标画的
这些double的坐标绝对值太小,根本没法画
必须放大了转成整型的坐标来画import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JFrame;
import javax.swing.JPanel;public class kaku extends JFrame{
private static final long serialVersionUID = 1L;
/** Simulation Basic Data **/
private int maximumVESSELS = 21;
private int simFIN = 100;// int simFIN = 11521 ; /** 8 Days **/
private double dtMIN = 1.0; /** minute **/
private double pi = 3.14159265358979323846;
private double degToRAD = pi / 180.0;
private ShipCondition[] shipSET;
private Timer timer;
private TimerTask task;
private int clock;
private MyPanel pnlMain;
public kaku() {
initialize();
}
private void initialize() {
clock = 0;
shipSET = new ShipCondition[maximumVESSELS];
for( int i=0;i<maximumVESSELS;i++){
shipSET[i] = new ShipCondition() ;
}
for( int is = 0 ; is < maximumVESSELS ; is++ ){
shipSET[is].vesselID = is ;
shipSET[is].metaWeightGOAL = 10.0 ;
shipSET[is].goalSigA = 5.0 ;
shipSET[is].goalSigB = 1.0 ;
shipSET[is].metaWeightCONTRACT = 1.0 ;
shipSET[is].contractSigA = 10.0 ;
shipSET[is].contractSigB = 1.0 ;
shipSET[is].metaWeightEXPAND = 1.0 ;
shipSET[is].expandSigA = -10.0 ;
shipSET[is].expandSigB = 1.0 ;
shipSET[is].metaWeightClusterAVOID = 100.0 ;
shipSET[is].clusterAvoidSigA = -10.0 ;
shipSET[is].clusterAvoidSigB = 3.0 ;
shipSET[is].decisionMD = 6.0 ;
shipSET[is].dcpaJUDGE = 0.75 ;
shipSET[is].headOnDEG = 50.0 ;
shipSET[is].courseRadOLD = pi/2 ; /* Dummy */
shipSET[is].speedKtOLD = 15.0 ;
shipSET[is].courseRadNOW = pi/2 ; /* Dummy */
shipSET[is].speedKtNOW = 15.0 ;
shipSET[is].courseNaviRAD = pi/2 ; /* Dummy */
shipSET[is].speedNaviKT = 15.0 ;
shipSET[is]. goalRad=pi/2;
}
double shipRad=360.0/((double)maximumVESSELS-1)*degToRAD;
for(int i=1;i<maximumVESSELS;i++){
shipSET[i].xMileOLD = shipSET[0].xMileOLD+2.0*Math.cos(shipRad*(double)i);
shipSET[i].yMileOLD = shipSET[0].yMileOLD+2.0*Math.sin(shipRad*(double)i);
shipSET[i].xMileNOW =shipSET[0].xMileNOW+2.0*Math.cos(shipRad*(double)i);
shipSET[i].yMileNOW = shipSET[0].yMileNOW+2.0*Math.sin(shipRad*(double)i);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
Container cont = this.getContentPane();
pnlMain = new MyPanel();
cont.add(pnlMain);
}
public void transform() {
// Random rand = new Random(2146351065) ;
timer = new Timer();
task = new TimerTask() {
public void run() {
if (clock >= simFIN) {
timer.cancel();
return;
}
if(clock>=20&&clock<25)
shipSET[0].goalRad=pi/3;
if(clock>=25&&clock<70)
shipSET[0].goalRad=pi/2;
if(clock>=70&&clock<75)
shipSET[0].goalRad=pi;
if(clock>=75)
shipSET[0].goalRad=pi/2;
for( int givenSHIP= 0 ; givenSHIP < maximumVESSELS ; givenSHIP ++ ){
/*** Navigation -> Navigation (status = 0 --> 0) ***/
/********************************************************/
/***** Ship Cluster Behavior Model Calculation *****/
/********************************************************/
DecisionMakingReturn dmCoSp =
DecisionMakingBody.agentDecisionMaking(shipSET, givenSHIP, dtMIN);
/*** Quick Response ***/
shipSET[givenSHIP].xMileNEW =
shipSET[givenSHIP].xMileNOW + dmCoSp.dmSpeedKt
* dtMIN / 60.0 * Math.sin( dmCoSp.dmCourseRad );
shipSET[givenSHIP].yMileNEW =
shipSET[givenSHIP].yMileNOW + dmCoSp.dmSpeedKt
* dtMIN / 60.0 * Math.cos( dmCoSp.dmCourseRad ) ;
shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad ;
shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt ;
pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileOLD*10),
(int)Math.round(shipSET[givenSHIP].yMileOLD*10)+50, 4, 4);
pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileNEW*10),
(int)Math.round(shipSET[givenSHIP].yMileNEW*10)+50, 4, 4);
}/** FOR givenSHIP **/
double collisionJudgeDIST = 0.1 ;
for( int i = 0 ; i < maximumVESSELS ; i++ ) {
int flagCC = 0 ;
if( shipSET[i].vesselSTATUS == 0 ){
for( int j = i+1 ; j < maximumVESSELS ; j++ ){
if( shipSET[j].vesselSTATUS == 0 ){
double x1 = shipSET[i].xMileNEW ;
double y1 = shipSET[i].yMileNEW ;
double x2 = shipSET[j].xMileNEW ;
double y2 = shipSET[j].yMileNEW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
double dist = p1.distance2D( p2 ) ;
if( dist < collisionJudgeDIST ) flagCC = 1 ;
}
}
}
}
/** State Transition **/
for( int i = 0 ; i < maximumVESSELS ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){
shipSET[i].xMileOLD = shipSET[i].xMileNOW ;
shipSET[i].yMileOLD = shipSET[i].yMileNOW ;
shipSET[i].courseRadOLD = shipSET[i].courseRadNOW;
shipSET[i].speedKtOLD = shipSET[i].speedKtNOW ;
shipSET[i].xMileNOW = shipSET[i].xMileNEW ;
shipSET[i].yMileNOW = shipSET[i].yMileNEW ;
shipSET[i].courseRadNOW = shipSET[i].courseRadNEW ;
shipSET[i].speedKtNOW = shipSET[i].speedKtNEW ;
}
}
clock++;
}
};
timer.schedule(task, 0,100);
} public static void main(String[] args){
kaku ka = new kaku();
ka.setVisible(true);
ka.transform();
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L; @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (ShipCondition point : shipSET) {
g.fillRect((int)Math.round(point.xMileNEW*10),
(int)Math.round(point.yMileNEW*10)+50, 4, 4);
}
g.setColor(Color.RED);
g.fillRect((int)Math.round(shipSET[0].xMileNEW*10),
(int)Math.round(shipSET[0].yMileNEW*10)+50, 4, 4);
}
}
}
那么是不是就是时间驱动转换成事件驱动了呢。我现在的程序是时间来控制的,是不是要改成用事件控制,就是每次按键才触发程序的循环呢?按一下腱子,循环一次。
是这个意思吗?另外说一句,你太专业了
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class kaku extends JFrame{
private static final long serialVersionUID = 1L;
/** Simulation Basic Data **/
private int maximumVESSELS = 21;
private int simFIN = 100;// int simFIN = 11521 ; /** 8 Days **/
private double dtMIN = 1.0; /** minute **/
private double pi = 3.14159265358979323846;
private double degToRAD = pi / 180.0;
private ShipCondition[] shipSET;
private Timer timer;
private TimerTask task;
private int clock;
private MyPanel pnlMain;
private JButton btnInterrupt;
private boolean movable = false;
public kaku() {
initialize();
}
private void initialize() {
clock = 0;
shipSET = new ShipCondition[maximumVESSELS];
for( int i=0;i<maximumVESSELS;i++){
shipSET[i] = new ShipCondition() ;
}
for( int is = 0 ; is < maximumVESSELS ; is++ ){
shipSET[is].vesselID = is ;
shipSET[is].metaWeightGOAL = 10.0 ;
shipSET[is].goalSigA = 5.0 ;
shipSET[is].goalSigB = 1.0 ;
shipSET[is].metaWeightCONTRACT = 1.0 ;
shipSET[is].contractSigA = 10.0 ;
shipSET[is].contractSigB = 1.0 ;
shipSET[is].metaWeightEXPAND = 1.0 ;
shipSET[is].expandSigA = -10.0 ;
shipSET[is].expandSigB = 1.0 ;
shipSET[is].metaWeightClusterAVOID = 100.0 ;
shipSET[is].clusterAvoidSigA = -10.0 ;
shipSET[is].clusterAvoidSigB = 3.0 ;
shipSET[is].decisionMD = 6.0 ;
shipSET[is].dcpaJUDGE = 0.75 ;
shipSET[is].headOnDEG = 50.0 ;
shipSET[is].courseRadOLD = pi/2 ; /* Dummy */
shipSET[is].speedKtOLD = 15.0 ;
shipSET[is].courseRadNOW = pi/2 ; /* Dummy */
shipSET[is].speedKtNOW = 15.0 ;
shipSET[is].courseNaviRAD = pi/2 ; /* Dummy */
shipSET[is].speedNaviKT = 15.0 ;
shipSET[is]. goalRad=pi/2;
}
double shipRad=360.0/((double)maximumVESSELS-1)*degToRAD;
for(int i=1;i<maximumVESSELS;i++){
shipSET[i].xMileOLD = shipSET[0].xMileOLD+2.0*Math.cos(shipRad*(double)i);
shipSET[i].yMileOLD = shipSET[0].yMileOLD+2.0*Math.sin(shipRad*(double)i);
shipSET[i].xMileNOW =shipSET[0].xMileNOW+2.0*Math.cos(shipRad*(double)i);
shipSET[i].yMileNOW = shipSET[0].yMileNOW+2.0*Math.sin(shipRad*(double)i);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
Container cont = this.getContentPane();
cont.setLayout(new BorderLayout());
pnlMain = new MyPanel();
btnInterrupt = new JButton("中断");
btnInterrupt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
movable = true;
pnlMain.requestFocus();
}
});
cont.add(btnInterrupt, BorderLayout.NORTH);
cont.add(pnlMain, BorderLayout.CENTER);
}
public void transform() {
// Random rand = new Random(2146351065) ;
timer = new Timer();
task = new TimerTask() {
public void run() {
if (clock >= simFIN) {
timer.cancel();
return;
}
if(clock>=20&&clock<25)
shipSET[0].goalRad=pi/3;
if(clock>=25&&clock<70)
shipSET[0].goalRad=pi/2;
if(clock>=70&&clock<75)
shipSET[0].goalRad=pi;
if(clock>=75)
shipSET[0].goalRad=pi/2;
for( int givenSHIP= 0 ; givenSHIP < maximumVESSELS ; givenSHIP ++ ){
/*** Navigation -> Navigation (status = 0 --> 0) ***/
/********************************************************/
/***** Ship Cluster Behavior Model Calculation *****/
/********************************************************/
if (movable && givenSHIP == 0) {
continue;
}
DecisionMakingReturn dmCoSp =
DecisionMakingBody.agentDecisionMaking(shipSET, givenSHIP, dtMIN);
/*** Quick Response ***/
shipSET[givenSHIP].xMileNEW =
shipSET[givenSHIP].xMileNOW + dmCoSp.dmSpeedKt
* dtMIN / 60.0 * Math.sin( dmCoSp.dmCourseRad );
shipSET[givenSHIP].yMileNEW =
shipSET[givenSHIP].yMileNOW + dmCoSp.dmSpeedKt
* dtMIN / 60.0 * Math.cos( dmCoSp.dmCourseRad ) ;
shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad ;
shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt ;
pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileOLD*10),
(int)Math.round(shipSET[givenSHIP].yMileOLD*10)+50, 3, 3);
pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileNEW*10),
(int)Math.round(shipSET[givenSHIP].yMileNEW*10)+50, 3, 3);
}/** FOR givenSHIP **/
double collisionJudgeDIST = 0.1 ;
for( int i = 0 ; i < maximumVESSELS ; i++ ) {
int flagCC = 0 ;
if( shipSET[i].vesselSTATUS == 0 ){
for( int j = i+1 ; j < maximumVESSELS ; j++ ){
if( shipSET[j].vesselSTATUS == 0 ){
double x1 = shipSET[i].xMileNEW ;
double y1 = shipSET[i].yMileNEW ;
double x2 = shipSET[j].xMileNEW ;
double y2 = shipSET[j].yMileNEW ;
Point2D p1, p2 ;
p1 = new Point2D( x1, y1 ) ;
p2 = new Point2D( x2, y2 ) ;
double dist = p1.distance2D( p2 ) ;
if( dist < collisionJudgeDIST ) flagCC = 1 ;
}
}
}
}
/** State Transition **/
for( int i = 0 ; i < maximumVESSELS ; i++ ){
if( shipSET[i].vesselSTATUS == 0 ){
shipSET[i].xMileOLD = shipSET[i].xMileNOW ;
shipSET[i].yMileOLD = shipSET[i].yMileNOW ;
shipSET[i].courseRadOLD = shipSET[i].courseRadNOW;
shipSET[i].speedKtOLD = shipSET[i].speedKtNOW ;
shipSET[i].xMileNOW = shipSET[i].xMileNEW ;
shipSET[i].yMileNOW = shipSET[i].yMileNEW ;
shipSET[i].courseRadNOW = shipSET[i].courseRadNEW ;
shipSET[i].speedKtNOW = shipSET[i].speedKtNEW ;
}
}
clock++;
}
};
timer.schedule(task, 0,100);
} public static void main(String[] args){
kaku ka = new kaku();
ka.setVisible(true);
ka.transform();
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel() {
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {//事件监听
if (movable) {
int code=e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
// shipSET[0].yMileOLD = shipSET[0].yMileNEW;
shipSET[0].yMileNEW -= 0.1;
repaint((int)Math.round(shipSET[0].xMileNEW*10),
(int)Math.round(shipSET[0].yMileNEW*10)+50,
3, 4);//控制重绘区域
break;
case KeyEvent.VK_DOWN:
// shipSET[0].yMileOLD = shipSET[0].yMileNEW;
shipSET[0].yMileNEW += 0.1;
repaint((int)Math.round(shipSET[0].xMileNEW*10),
(int)Math.round(shipSET[0].yMileNEW*10)+49,
3, 4);//控制重绘区域
break;
case KeyEvent.VK_LEFT:
// shipSET[0].xMileOLD = shipSET[0].xMileNEW;
shipSET[0].xMileNEW -= 0.1;
repaint((int)Math.round(shipSET[0].xMileNEW*10),
(int)Math.round(shipSET[0].yMileNEW*10)+50,
4, 3);//控制重绘区域
break;
case KeyEvent.VK_RIGHT:
// shipSET[0].xMileOLD = shipSET[0].xMileNEW;
shipSET[0].xMileNEW += 0.1;
repaint((int)Math.round(shipSET[0].xMileNEW*10)-1,
(int)Math.round(shipSET[0].yMileNEW*10)+50,
4, 3);//控制重绘区域
break;
}
}
}
});
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (ShipCondition point : shipSET) {
g.fillRect((int)Math.round(point.xMileNEW*10),
(int)Math.round(point.yMileNEW*10)+50, 3, 3);
}
g.setColor(Color.RED);
g.fillRect((int)Math.round(shipSET[0].xMileNEW*10),
(int)Math.round(shipSET[0].yMileNEW*10)+50, 3, 3);
}
}
}