开发者

Animating a line between two Point objects

开发者 https://www.devze.com 2023-02-11 05:04 出处:网络
What\'s the simplest way to draw a line between two Point objects in a way that will look like I am drawing that line in real time by hand?

What's the simplest way to draw a line between two Point objects in a way that will look like I am drawing that line in real time by hand?

For example:

Point a = new Point(5,20);
Point b = new Point(15,20);

How do I connect these points with a "moving" lin开发者_如何学Goe?

In other words I want to make the user feel "motion" of some sort. Is there a simple way to do that?


Given two points, you can determine the equation of the line connecting them. The equation of a line is of the form y = mx + c, where m is the slope, and c is the y intercept.

So, given your two points (5,20) and (15,20), we first determine m.

m = (y2-y1)/(x2-x1)
  = (20-20)/(15-5)
  = (0)/10
  = 0

Substituting into the equation for a straight line, we get y = 0x + c or y = c. Now that we know this, we simply need to know the points where y = c and 5<=x<=15. Simply draw each of these points in the normal way (look at this for the exact method) with a Thread.sleep() call in between drawing each point. In this case, you have only 11 points to draw, so it would make sense to draw 1 point every 100 ms. For details on Thread.sleep() see here.

EDIT: Since Thread.sleep() won't work on the EDT, look at javax.swing.Timer instead, as Uhlen suggested.


Following the answer by Chinmay Kanchi, you need to create a feeling of animation. As mentioned above in comments by Uhlen you should use Swing's Timer when working on EDT. To give you example of how to use Timer. Lets assume we have a panel and we want it to slide open on e.g. a button click, thus we need to animate it sliding open by increasing its size. Below is an example showing pretty much how you would use Timer to do the operations.

this.extendingTimer = new Timer(0, new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
       //assume sliding is vertical     
       int value = maximumHeight;
       //make sure the size will not be bigger then allowed maximum
       if(currentExtensionSize + extensionRate >= value)
       {
          currentExtensionSize = value;
          stopExtending();
       }
       else
          currentExtensionSize += extensionRate;
       setSize(new Dimension(maximumWidth, currentExtensionSize));
    }
});
extendingTimer.setInitialDelay(0);
extendingTimer.setDelay(100);
extendingTimer.setRepeats(true);


int lineCount = 0; //global
// timer calls the below

xLocation = (*a)[a->size()-1] * timeSoFar / duration ;
    if(xLocation > (*a)[lineCount+1]){
        lineCount++;        
    }
    double m = ((*b)[lineCount+1] - (*b)[lineCount])/((*a)[lineCount+1]-(*a)[lineCount]);
    double yIntercept = (*b)[lineCount]-m*(*a)[lineCount];
    yLocation = m * xLocation + yIntercept;
    xLocation = (yLocation - yIntercept) / m;

this is in c++ and using vectors but its the theory we want. This allows for multiple lines not just one.

0

精彩评论

暂无评论...
验证码 换一张
取 消