I created a piece of code that simulates a pendulum with the gravity(assuming no friction or resistance). However, it is not as smooth as I want it to be. So.. does anyone know what I can do to make it smoother?

import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.lang.Runnable;

class gui extends Canvas 
          implements Runnable{
    int x_not,

    gui(int width, int height){
       x_not = width/2;
       y_not = height - (height - 50);
    }   //end constructor

    public void paint(Graphics g) {
        double phi;

        try {
	//The sleep() method is invoked on the main thread to cause a one second delay.
	} catch (InterruptedException e) {}

        phi = (Math.PI/4)*Math.sin(Math.sqrt(9.8/200)*System.currentTimeMillis()*Math.pow(10.0, 3.0));
        buildPendulum(phi, g);
    } //end paint method

    void buildPendulum(double phi, Graphics g)
        //  build the pendulum with respect to the angle phi

        double Ax,Ay,

        Ax = x_not + 2*Math.cos(phi);
        Ay = y_not + 2*Math.sin(phi);

        g.drawLine(x_not, y_not, (int)Ax, (int)Ay);

        Bx = Ax + 200*Math.cos(phi + (Math.PI/2));
        By = Ay + 200*Math.sin(phi + (Math.PI/2));

        g.drawLine((int)Ax, (int)Ay, (int)Bx, (int)By);

        Cx = Bx + 7*Math.cos(phi);
        Cy = By + 7*Math.sin(phi);

        g.drawLine((int)Bx, (int)By, (int)Cx, (int)Cy);

        Dx = Cx + 30*Math.cos(phi + (Math.PI/2));
        Dy = Cy + 30*Math.sin(phi + (Math.PI/2));

        g.drawLine((int)Cx, (int)Cy, (int)Dx, (int)Dy);

        Ex = Dx + 20*Math.cos(phi + (Math.PI));
        Ey = Dy + 20*Math.sin(phi + (Math.PI));

        g.drawLine((int)Dx, (int)Dy, (int)Ex, (int)Ey);

        Fx = Ex + 30*Math.cos(phi - Math.PI/2);
        Fy = Ey + 30*Math.sin(phi - Math.PI/2);

        g.drawLine((int)Ex, (int)Ey, (int)Fx, (int)Fy);

        Gx = Fx + 8*Math.cos(phi);
        Gy = Fy + 8*Math.sin(phi);

        g.drawLine((int)Fx, (int)Fy, (int)Gx, (int)Gy);

        Hx = Gx + 200*Math.cos(phi - Math.PI/2);
        Hy = Gy + 200*Math.sin(phi - Math.PI/2);

        g.drawLine((int)Gx, (int)Gy, (int)Hx, (int)Hy);
        g.drawLine((int)Hx, (int)Hy, (int)x_not, (int)y_not);
    public void run()

Recommended Answers

All 2 Replies

nvm, got it to work with double buffering :D

Please mark this thread as SOLVED.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.