Filling up (gradually) ellispe2D objects with colo

2019-08-29 05:03发布

问题:

My question is simple as that:

1)I have main class which extends a JFrame.
2)Inside that class there is a JPanel with a BufferedImage on top of it.
3)Finally there is also a JButton which I call "Fire"..that's all for the design..

Now here's the deal:

When pressing the button there's a little method which returns me an array of 5 Ellipse2D objects. (The array is called "points" and is in essence an array of simple circles..).

All I want to do when pressing "Fire" is get these objects show up on the BufferedImage which I call "bf" and gradually get them filled up with color in a way that would give a sense of animation. What's the simplest way to do that?

P.S. I have tried using the swing Timer class but the problem with that approach is that I can't pass parameters into the method call(inside the actionPerformed) which I need if I want to get my array through...

Thank you in advance

回答1:

Construct your ActionListener object (the one that you pass to the Timer) so it has access to the data that's needed to perform the animation. The Timer is just to let the ActionListener when to take the next step in the animation.



回答2:

Here is a provisional "SSCCE"...

import java.awt.; import java.awt.geom.; import javax.swing.*;

public class myApp extends JFrame{

private JPanel myPanel;
private JButton myButton;
private Ellipse2D e[];

public myApp(){
    //the objects I need to draw
    e = new Ellipse2D.Double[2];
    e[0] = new Ellipse2D.Double(50.0, 50.0, 50.0, 50.0);
    e[1] = new Ellipse2D.Double(120.0, 120.0, 50.0, 50.0);
    //--------------------------
    setSize(400,300);
    myPanel = new JPanel(new BorderLayout());
    myButton = new JButton("Fire");
    setLayout(new BorderLayout());
    this.add(myPanel, BorderLayout.SOUTH);
    this.add(myButton, BorderLayout.NORTH);
}
public static void main(String args[])  {
    myApp my = new myApp();
    my.setVisible(true);
}

}

The problem is not only that I don't know HOW to use the timer class but I also don't know WHAT to put in the method that the timer will be repeatedly calling so that I can get the animation..!