简体   繁体   English

Java动画以及覆盖绘制和更新方法

[英]Java animation and overriding the paint and update methods

So at the moment I'm trying to animate different orders of bezier curves and the midpoints that make them up, but I am pretty new to graphics in Java. 因此,目前,我正在尝试对不同阶数的贝塞尔曲线及其构成的中点进行动画处理,但是对于Java图形我还是很陌生。 I understand how paint and repaint work but I can't figure out how to get out of this situation. 我了解绘画和重新绘画的工作原理,但是我不知道如何摆脱这种情况。

The bezier curve points are decided by user clicks here, and repaint() is called at the mouseEvent. 贝塞尔曲线点由用户单击此处确定,并且在mouseEvent处调用repaint()。

public void paint(Graphics g) {
    initgr();
    int left = iX(-rWidth / 2), right = iX(rWidth / 2), bottom = iY(-rHeight / 2), top = iY(rHeight / 2);
    g.drawRect(left, top, right - left, bottom - top);

    for (int i = 0; i < np; i++) {
        // Show tiny rectangle around point:
        g.drawRect(iX(P[i].x) - 2, iY(P[i].y) - 2, 4, 4);
        if (i > 0)
            // Draw line P[i-1]P[i]:
            g.drawLine(iX(P[i - 1].x), iY(P[i - 1].y), iX(P[i].x),
                    iY(P[i].y));
    }

    if (np == 2 && order == 1)
        bezier1(g, P, gran);
    if (np == 3 && order == 2)
        bezier2(g, P, gran);
    if (np == 4 && order == 3)
        bezier3(g, P, gran);
    if (np == 5 && order == 4)
        bezier4(g, P, gran);
    if (np == 6 && order == 5)
        bezier5(g, P, gran);
}

The functions called at the bottom go to the calculated and drawn bezier curves here. 底部调用的函数将转到此处计算和绘制的贝塞尔曲线。

void bezier3(Graphics g, Point2D[] p, int n) {
    javax.swing.Timer timer = new javax.swing.Timer(100,
            new TimerListener());
    timer.setDelay(39);
    timer.start();
    float dt = 1.0F / n, cx3 = -p[0].x + 3 * (p[1].x - p[2].x) + p[3].x, cy3 = -p[0].y
            + 3 * (p[1].y - p[2].y) + p[3].y, cx2 = 3 * (p[0].x - 2
            * p[1].x + p[2].x), cy2 = 3 * (p[0].y - 2 * p[1].y + p[2].y), cx1 = 3 * (p[1].x - p[0].x), cy1 = 3 * (p[1].y - p[0].y), cx0 = p[0].x, cy0 = p[0].y, x = p[0].x, y = p[0].y, x0, y0, x2, y2;
    for (int i = 1; i <= n; i++) {

        float t = i * dt;

        x0 = x;
        y0 = y;
        x = ((cx3 * t + cx2) * t + cx1) * t + cx0;
        y = ((cy3 * t + cy2) * t + cy1) * t + cy0;
        // x2 = ((cx3 * (.5F*t) + cx2) * (.5F*t) + cx1) * (.5F*t) + cx0;
        // y2 = ((cy3 * (.5F*t) + cy2) * (.5F*t) + cy1) * (.5F*t) + cy0;
        x2 = p[1].x * t;
        y2 = p[1].y * t;

        Point2D A = tcalc(P[0], P[1], t), B = tcalc(P[2], P[3], t), C = tcalc(
                P[1], P[2], t), A1 = tcalc(A, C, t), B1 = tcalc(C, B, t);

        g.setColor(Color.red);
        g.drawLine(iX(x0), iY(y0), iX(x), iY(y));
        // paint(g);
        g.setColor(Color.green);
        g.drawLine(iX(A.x), iY(A.y), iX(C.x), iY(C.y));
        g.drawLine(iX(C.x), iY(C.y), iX(B.x), iY(B.y));
        g.setColor(Color.blue);
        g.drawLine(iX(A1.x), iY(A1.y), iX(B1.x), iY(B1.y));
    }
}

So I know I shouldn't be drawing inside of these methods, but rather in paint. 所以我知道我不应该在这些方法中画图,而应该在绘画中。 However, I have 5 of these functions that I'm not sure how to put in paint, and if I change the other method to update, it's just never going to erase the points the user clicked when I want to move on to the next selection of points. 但是,我不确定其中有5个功能,以及如何更改绘画方法,如果我更改另一种方法进行更新,那么当我继续下一步操作时,它永远不会消除用户单击的点。选择点。 You can see I tried to put a basic swing timer in, but I'm not even sure if that will work in this situation for the animation. 您可以看到我尝试加入一个基本的摆动计时器,但是我不确定在这种情况下动画是否可以正常工作。

Is there any way to get this to work inside of the bezier functions? 有什么方法可以使它在bezier函数中起作用? I just don't see how I can get the drawlines out. 我只是看不到如何画出轮廓线。 My 5th order one has something like 11 midpoints being constantly calculated. 我的5阶有11个中点在不断地计算。 Obviously, my understanding of this part of java graphics is shaky at best, so any point in the right direction would be greatly appreciated. 显然,我对Java图形这一部分的理解充其量是不稳定的,因此,朝着正确方向的任何一点将不胜感激。 If I find anything in my research I will update the question. 如果我在研究中发现任何问题,我将更新问题。

Thanks for any help. 谢谢你的帮助。

Forget the timer. 忘了计时器。 Try paintComponent. 试试paintComponent。

Object Oriented would help keeping an overview, simplifying the number of variables. 面向对象将有助于保持概览,简化变量的数量。

  • Make a base class Bezier with a paint(Graphics) {} and onMouse(MouseEvent){}. 使用paint(Graphics){}和onMouse(MouseEvent){}创建基类Bezier。
  • Derive Bezier1, ~2, ~3 and so on. 派生Bezier1,〜2,〜3等。
  • And have variables Bezier bezier2 = new Bezier2(); 并且具有变量Bezier bezier2 = new Bezier2(); ... ...
  • And in your paintCompont call bezier2.paint(g). 并在您的paintCompont中调用bezier2.paint(g)。

Then try repaint(10L) or so in your mouse handling. 然后在鼠标处理中尝试repaint(10L)左右。 Experiment to learn (paintImmediate and so on). 实验学习(paintImmediate等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM