我正在为我的学士论文设计一个程序,该程序根据z位置(电缆中的位置)显示钢丝绳的内部几何形状。

为了测试我用这段代码“遍历”了一段电缆(sl是一个链表,已经初始化,可以正常工作):

    Cable c = new Cable(sl);
    ImageFrame ts = new ImageFrame(c);

    try {
        while (location <2 ) {
            location = location + 0.01;
            Thread.sleep(100);
            c.update(location);
            ts.repaint();
        }
        System.exit(0);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }


电缆功能update仅应重新计算电缆的几何形状。我的类电缆有一个线束列表,此update函数的作用是仅针对所有这些线束调用update函数:

public void update(double z) {
for(int i=0;i<strandList.size() ;i++) {
            strandList.get(i).updateStrand(z);

        }

    }


对象Strand看起来像这样:

public abstract class Strand {

    Punt middenCable;
    final Punt nulpoint_midden;
    Punt midden;
    Double angle;
    Double d_to_core;
    Image img;
    int img_size;
    BufferedImage bf;

    /**
     *
     * @param c centre of the cable
     * @param p middle of the strand
     * @param ang angle at this point
     * @param dtc distance to core
     * @param i image
     */
    public Strand(Punt c,Punt p,Image i) {
        nulpoint_midden = p; //location of strand at z=0
        middenCable =c;
        midden = p; // for drawing
        img = i;
        img_size = 135; //TODO adapt
        bf = new BufferedImage(img_size, img_size, BufferedImage.TRANSLUCENT );
        bf.getGraphics().drawImage(img, 0, 0, null);
}



    /**
     * angle goed zetten tov de z
     * @param z
     */
    abstract public  void updateStrand(Double z);






    public void paint(Graphics g){
        g.setColor(Color.RED); //cirkels rood
        //rotate around itself
        int x = (int) (this.midden.getX() - img_size/2  );
        int y = (int) (this.midden.getY() - img_size/2 );
        int st = (int) this.img_size;
          Graphics2D g2d = (Graphics2D) g.create();

          g2d.setColor(Color.RED);

          AffineTransform at = new AffineTransform();
          at.setToRotation(Math.toRadians(angle), midden.getX(), midden.getY());
          g2d.setTransform(at);
          g2d.drawImage(bf, x, y, null);
          g2d.dispose();
        }


在绘画功能中,它绕自身旋转一定角度。但是问题是抽象函数“更新链”。这应该做的是将平底船Midden的坐标更改为它实际所在的位置。用于计算的参数nulpoint_midden不应更改且应恒定。对于对象类型外部链(扩展链),updatestrand如下所示:

import java.awt.Image;


public class OuterStrand extends Strand{
    private double rotateconstant1 = 10; //tweaken tot perfecte laylength
    private double rotateconstant2 = (2*Math.PI/34); //tweaken tot perfecte laylength


    public OuterStrand(Punt c,Punt p, Image i) {
        super(c, p,i);
    }

    @Override
    public void updateStrand(Double z) {
        //       midden updaten
        double xpoint = nulpoint_midden.getX(); //original point
        double ypoint = nulpoint_midden.getY(); //original point
        double cCableX = super.middenCable.getX();
        double cCableY = super.middenCable.getY();
        double dx = nulpoint_midden.getX() - cCableX;
        double dy = ypoint - cCableY;
        System.out.println(xpoint+ "   " +ypoint) ;

        double curangle = Math.atan2(dy, dx);

        double dist     = Math.sqrt(dx*dx + dy*dy);

        double dangle = rotateconstant2 * z;
        double x1 = cCableX + dist * Math.cos(dangle + curangle);
        double y1 = cCableY + dist * Math.sin(dangle + curangle);

        super.midden.setX(x1);
        super.midden.setY(y1);

        //      rotate around itself
        super.angle = z * Math.PI * rotateconstant1;

    }


}


因此,println给出xpoint和ypoint,这应该是恒定的。但实际上,它会发生变化(因此punt nulpoint_midden会更改值)。我不知道为什么这个值会改变。这是我的印刷品:

> 500.0   200.0
> 500.55439838802135   200.00051226305845
> 501.66318759079536   200.00461035702926
> 503.32632406233347   200.0184412864145
> 505.54367148773895   200.0512248625842
> 508.3149156018249   200.11525184075379
> 511.63945065512087   200.22587972200301
> 515.5162375992163   200.40152475227
> 519.9436341271526   200.66364828540742
> 524.9191967987913   201.03673531535162
> 530.439455611731   201.54826262515832
> 536.4996615512757   202.22865365075 (etcetera)


由于此值的更改,表示形式不正确。由于穿过电缆,电缆应以恒定的速度旋转,但实际上正在加速。

对于冗长的解释,我感到抱歉,但是如果有人看到我的错误并告诉我我在做错什么,那太好了。谢谢!

最佳答案

final关键字表示只能将引用分配一次,但这并不意味着以某种方式阻止了所引用的对象更改其值。



您的构造函数中存在潜在的错误:

nulpoint_midden = p;
midden = p;


这两个实例变量引用同一对象:如果在midden上调用setter,则nulpoint_midden也会发生变化。

10-04 13:08
查看更多