我这个pressok按钮应加到哪个循环呢?
我是想先按中断的按钮,然后在文本框输入数字,然后点pressok按钮 把文本框中的数值传递到shipSET[0].speedNaviK这个中。为什么实现不了呢 大家有明白的给指点下吗?
public class kaku extends JFrame{
private static final long serialVersionUID = 1L;
/** Simulation Basic Data **/
private int maximumVESSELS = 16;
private int simFIN = 300;// 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 JButton pressok;
private JLabel label1;
private JTextField textField1;
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 = 5.0 ;
shipSET[is].courseRadNOW = pi/2 ; /* Dummy */
shipSET[is].speedKtNOW = 5.0 ;
shipSET[is].courseNaviRAD = pi/2 ; /* Dummy */
shipSET[is].speedNaviKT = 5.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());
DocumentListener listener = new ClockFieldListener();
pnlMain = new MyPanel();
pnlMain.add(new JLabel("shiprad"));
textField1=new JTextField(10);
pnlMain.add(textField1);
// textField1.addT
textField1.getDocument().addDocumentListener(listener);
pressok=new JButton("pressok");
pressok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
shipSET[0].speedNaviKT=Integer.parseInt(textField1.getText().trim());
}
});
cont.add(pressok, BorderLayout.PAGE_END);
cont.add(pnlMain, BorderLayout.CENTER);
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);
}
class ClockFieldListener implements DocumentListener
{
public void insertUpdate(DocumentEvent event){shipSET[0].speedNaviKT=Integer.parseInt(textField1.getText().trim());}
public void removeUpdate(DocumentEvent event){shipSET[0].speedNaviKT=Integer.parseInt(textField1.getText().trim());}
public void changedUpdate(DocumentEvent event){}
}
public void transform() {
// Random rand = new Random(2146351065) ;
timer = new Timer();
task = new TimerTask() {
public void run() {
if (clock >= simFIN) {
timer.cancel();
return;
}
for( int givenSHIP= 0 ; givenSHIP < maximumVESSELS ; givenSHIP ++ ){
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();
}
[解决办法]
汗~这是你那个程序中的一小部分
别人没有相关的类肯定没法帮你嘛
我看了下你刚才发给我的程序
改了下哦,好多地方直接引用了数组名,
而不是数组成员,这个估计是出错的主要原因
输出结束要注意将工作流close()掉
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;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.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Timer;import java.util.TimerTask;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class kaku extends JFrame { private static final long serialVersionUID = 1L; /** Simulation Basic Data **/ private int maximumVESSELS = 16; private int simFIN = 10000;// 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 JTextField textField1; private JButton pressok; private boolean movable = false; private String out; public kaku() { super("操船シミュレータ"); initialize(); } private void initialize() { out = "out.txt"; 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; // -10 shipSET[is].expandSigB = 1.0; // 1 shipSET[is].metaWeightClusterAVOID = 100.0; shipSET[is].clusterAvoidSigA = -10.0; // -10 shipSET[is].clusterAvoidSigB = 3.0; // 3 shipSET[is].decisionMD = 6.0; // 6 shipSET[is].dcpaJUDGE = 0.75; shipSET[is].headOnDEG = 50.0; shipSET[is].courseRadOLD = pi / 2; /* Dummy */ shipSET[is].speedKtOLD = 8.0; shipSET[is].courseRadNOW = pi / 2; /* Dummy */ shipSET[is].speedKtNOW = 8.0; shipSET[is].courseNaviRAD = pi / 2; /* Dummy */ shipSET[is].speedNaviKT = 8.0; shipSET[is].goalRad = pi / 2; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { shipSET[i * 3 + j + 1].xMileOLD = j * 2 + i; shipSET[i * 3 + j + 1].yMileOLD = i * 1.7320508076; shipSET[i * 3 + j + 1].xMileNOW = shipSET[i * 3 + j + 1].xMileOLD; shipSET[i * 3 + j + 1].yMileNOW = shipSET[i * 3 + j + 1].yMileOLD; } } for (int i = 3; i < 5; i++) { for (int j = 0; j < 3; j++) { shipSET[i * 3 + j + 1].xMileOLD = j * 2 + (4 - i); shipSET[i * 3 + j + 1].yMileOLD = i * 1.7320508076; shipSET[i * 3 + j + 1].xMileNOW = shipSET[i * 3 + j + 1].xMileOLD; shipSET[i * 3 + j + 1].yMileNOW = shipSET[i * 3 + j + 1].yMileOLD; } } } this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("操船シミュレータ"); this.setSize(800, 300); Container cont = this.getContentPane(); cont.setLayout(new BorderLayout()); pnlMain = new MyPanel(); pnlMain.setBackground(Color.BLACK); pnlMain.add(new JLabel("shiprad")); textField1 = new JTextField(10); pnlMain.add(textField1); pressok = new JButton("preeok"); pressok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { shipSET[8].goalRad = (180 - (Integer.parseInt(textField1 .getText().trim()))) / 180.0 * pi;// outputPos();在你想输出的地方使用这个函数 pnlMain.requestFocus(); } }); pressok.setLayout(new FlowLayout()); pressok.setBackground(Color.WHITE); cont.add(pressok, BorderLayout.PAGE_END); cont.add(pnlMain, BorderLayout.CENTER); outputPos(); } //将每个点的坐标输出到文件 private void outputPos() { PrintWriter output; try { output = new PrintWriter(new BufferedWriter(new FileWriter(out))); for (int i = 1; i < maximumVESSELS; i++) { output.println(shipSET[i].xMileNOW + "," + shipSET[i].yMileNOW); } output.flush(); output.close(); } catch (IOException e1) { e1.printStackTrace(); } } public void transform() { timer = new Timer(); task = new TimerTask() { public void run() { if (clock >= simFIN) { timer.cancel(); return; } 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); } 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[j].xMileNEW; double y1 = shipSET[j].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[8].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[8].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[8].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[8].xMileNEW += 0.1; repaint((int) Math.round(shipSET[0].xMileNEW * 10) - 1, (int) Math.round(shipSET[0].yMileNEW * 10) + 50, 4, 3); break; } } } }); } 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[8].xMileNEW * 10), (int) Math .round(shipSET[8].yMileNEW * 10) + 50, 3, 3); } }}