绘制在java中曲折(春季) [英] Drawing a zigzag (Spring) in java

查看:123
本文介绍了绘制在java中曲折(春季)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做节目时,被移动该动画质量弹簧的简谐运动。我把一切都从不是绘制像一个春天,我的计划目前使用graphics.drawline方法绘制一条直线,重新present春天的事实相距工作。我最好要像但是我不是很有经验的图形和真的不知道如何处理它,我试图让一个算法自己,但它一直分崩离析。有谁知道我可以利用这里的任何现有的算法?如果弹簧的拉伸神情逼真那么这将是巨大的(如果可能)。

I am currently making a program which animates the simple harmonic motion of a mass-spring when it is displaced. I have everything working apart from the fact that instead of drawing something like a spring, my program currently uses the graphics.drawline method to draw a straight line to represent the spring. I ideally want something like this however I am not very experienced with graphics and don't really know how to approach it, I tried to make an algorithm myself but it kept falling apart. Does anyone know of any existing algorithms which I could utilise here? If the stretching of the spring looked realistic then that would be great too (if possible).

下面是我目前的code:

Here is my current code:

    g.fillRect(width/10 - 2, height/2 - 10, 4, 20);
    g.fillRect(9*width/10 - 2, height/2 - 10, 4, 20);

    g.drawLine(width/10, height/2, (int) (width/2 - (sCoefficientH * s)), height/2);
    g.fillOval((int) (width/2 - (sCoefficientH * s)) -5, height/2 - 5, 10, 10);

正如你可以看到有连接壁(小矩形)的椭圆形(从而重新presents弹簧上的质量)的线路。如果我能在一个新的方法,在这个类需要2个坐标和轻松的大小(它不会看COM pressed)添加并返回图形对象(注意,我没有使用的Graphics2D)与在正确的位置绘制的春天那么我认为它看起来要好很多。 是什么样子现在。

推荐答案

试试这个:

void drawSpring(double x1, double y1, double x2, double y2, double w, int N, Graphics g)
{
   // vector increment
   double inv = 0.25 / (double)N;
   double dx = (x2 - x1) * inv,
          dy = (y2 - y1) * inv;

   // perpendicular direction
   double inv2 = w / sqrt(dx * dx + dy * dy);
   double px =  dy * inv2, 
          py = -dx * inv2;

   // loop
   double x = x1, y = y1;
   for (int i = 0; i < N; i++)
   {
      g.drawLine(x                , y                , 
                 x +       dx + px, y +       dy + py);
      g.drawLine(x +       dx + px, y +       dy + py,
                 x + 3.0 * dx - px, y + 3.0 * dy - py);
      g.drawLine(x + 3.0 * dx - px, y + 3.0 * dy - py,
                 x + 4.0 * dx     , y + 4.0 * dy     );
      x += 4.0 * dx;
      y += 4.0 * dy;
   }
}

也许改变图形到任何相当于在Java。

Maybe change Graphics to whatever the equivalent is in Java.

编辑:我在VB.NET有:

what I got in VB.NET:

这篇关于绘制在java中曲折(春季)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆