麻烦各位前辈了,
想问个问题。想做个gui,条件如下
1。我有一套计算坐标点的公式,就是计算后是x,y位置,是double型的,并且位置一直变化的,我想让这些位置动起来。
2。做出gui.让点的显示到gui上。
3。从这些点中,我指定一个点。我可以控制这个点。比如控制行动方向。一开始我能控制的是随着这一堆点点一起走动,加个中断按钮后,我就可以控制那个了
可能说的复杂很难懂,说白了就是,
我想在界面中显示一堆小点点,前提是小点点的x、y的值,我都是已知的。就是我可以从本地文件txt中直接提取到的。
然后在这一堆小点点中,有一个我能控制的。
麻烦大家乐,最好大家能给讲讲方法,和给个例子 谢谢大家。

解决方案 »

  1.   

    楼主,根据我在你另一个帖子里给你的那个程序
    你的中断可以这样实现
    用一个boolean型的标志位来判断是否对你的那个数组中的第一个元素进行计算
    需要自己控制的时候,把标志位改一下,小红点就不会参加参加计算了
      

  2.   

    好人做到底罗
    改好了现在加了个“中断”按钮
    点击之后,就可以通过上下左右键来控制红点了
    希望对楼主有帮助,
    另外,楼主注意及时结贴哦import java.awt.BorderLayout;
    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);
    }
    }
    }