PopUp类似乎可以加快动画速度

时间:2017-10-27 17:20:52

标签: java swing timer jpanel graphics2d

我有一个名为PopUp的类,它是一个JPanel,当激活时,根据中心给出的参数扩展到给定的大小和位置,并在点击时以相同的方式收缩。

出于某种原因,当PopUp正在扩展和收缩时,共享相同JPanel的动画会加速。我在我使用过PopUp课程的两个程序中见过这个。

以下是我认为的相关代码:

/**
 * The {@code PopUp} class is a JPanel that expands to the rectangle
 * created from the given x, y, width and height that expands from
 * the center of the rectangle.
 * <p>
 * Here is an example of how the {@code PopUp} object can be initialized:
 * <blockquote><pre>
 *     PopUp pu = new PopUp(25, 25, 575, 575, 25, Color.GRAY);
 * </pre></blockquote>
 * <p>
 * The class {@code PopUp} includes methods for drawing the pop-up;
 * choosing whether the pop-up is expanding or not; getting the
 * percentage that the pop-up is expanded; and getting the maximum x, y,
 * width, and height
 *
 * @author  Gigi Bayte 2
 */
public class PopUp extends JPanel implements MouseListener {
    private static final long serialVersionUID = 1L;

    /**
     * Expanded x coordinate
     */
    private double x;
    /**
     * Expanded y coordinate
     */
    private double y;
    /**
     * Expanded width value
     */
    private double width;
    /**
     * Expanded height value
     */
    private double height;

    /**
     * Number of steps until fully expanded
     */
    private int steps;
    /**
     * This divided by steps is the percentage the pop-up is expanded
     */
    private int expansionStage = 0;

    /**
     * Whether or not the pop-up is expansing
     */
    private boolean isExpanding = false;

    /**
     * Color of the pop-up
     */
    private Color color;

    /**
     * The rectangle that represents the bounds of the pop-up
     */
    private Rectangle2D popUp;

    /**
     * Initializes a newly created {@code PopUp} with a uniform color
     * @param x                 The x coordinate of the expanded pop-up
     * @param y                 The y coordinate of the expanded pop-up
     * @param w                 The width of the expanded pop-up
     * @param h                 The height of the expanded pop-up
     * @param expansionSteps    The number of steps until fully expanded
     * @param popUpColor        The color of the pop-up
     */
    public PopUp(double x, double y, double w, double h, int expansionSteps, Color popUpColor) {
        this.x = x;
        this.y = y;
        width = w;
        height = h;
        color = popUpColor;
        steps = expansionSteps;
        popUp = new Rectangle2D.Double(0, 0, width, height);
        addMouseListener(this);
    }

    /**
     * Draws the pop-up
     * @param g     Graphics object from paintComponent
     */
    public final void draw(Graphics g) {
        if(isExpanding)
            expansionStage = Math.min(expansionStage + 1, steps);
        else
            expansionStage = Math.max(expansionStage - 1, 0);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform trans = new AffineTransform();
        trans.translate(x + width/2 * (1 - (double) expansionStage/steps), y + height/2 * (1 - (double) expansionStage/steps));
        trans.scale((double) expansionStage/steps, (double) expansionStage/steps);
        setBounds((int) trans.getTranslateX(), (int) trans.getTranslateY(), (int) (width * expansionStage/steps), (int) (height * expansionStage/steps));
        g2d.setColor(color);
        Shape transformed = trans.createTransformedShape(popUp);
        g2d.fill(transformed);
    }

    /**
     * Sets whether the pop-up is expanding or not
     * @param expanding    Whether or not the pop-up should expand
     */
    public final void setExpanding(boolean expanding) {
        isExpanding = expanding;
    }

    @Override
    public final void mouseClicked(MouseEvent e) {
        isExpanding = false;
    }
}

这是一个要运行的测试类:

public class Test extends JPanel implements ActionListener, MouseListener {
    private static final long serialVersionUID = 1L;

    private static PopUp popUp;
    private int stringX = 610;
    private int stringCounter = 0;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(600, 600);

        Test t = new Test();
        t.setBounds(0, 0, 600, 600);
        frame.add(t);
        t.setVisible(true);

        Timer timer = new Timer(5, t);

        popUp = new PopUp(100, 100, 400, 400, 100, Color.WHITE);
        frame.add(popUp);
        popUp.setVisible(true);

        timer.start();

        frame.addMouseListener(t);
        frame.setLayout(null);
        frame.setUndecorated(true);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 600, 600);
        popUp.draw(g);
        g.setColor(Color.WHITE);
        g.drawString("This is a test", stringX, 580);
        if(++stringCounter % 3 == 0) {
            --stringX;
            stringCounter = 0;
        }
        if(stringX == -10 - g.getFontMetrics().stringWidth("This is a test"))
            stringX = 610;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        popUp.setExpanding(!popUp.getExpanding());
    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

}

从上面的示例中可以看出,每次弹出窗口扩展或收缩时,从右向左滚动的文本都会加速。

2 个答案:

答案 0 :(得分:2)

这是接收调用paintComponent()的重复更新事件时的预期行为;调整此AnimationTest的大小以重现效果。

  

为什么重复更新事件确实会导致这种情况?它背后的逻辑是什么?

draw()方法中对setBounds()的每次调用都会使组件层次结构无效。“ Component API可确保

  

当层次结构失效时,例如在更改组件边界或向容器添加/删除组件之后,必须通过最顶层调用的Container.validate()方法验证整个层次结构。层次结构的无效容器。

因为validate()方法“可能是一个非常耗时的操作,”你可以“推迟层次结构的验证,直到一组与布局相关的操作完成,”当你展示here时;或者您可以使用javax.swing.Timer来说明动画,图示为here

答案 1 :(得分:1)

好吧,我发现了问题。罪魁祸首就在这里:

router.post('/authenticate', function(req, res, next){
    User.findOne({username: req.body.username}, function(err, user){
        //handling errors
        if(!bcrypt.compareSync(req.body.password, user.password)){
            return res.status(401).json({
                success: false,
                message: 'Invalid login credentials!'
            });
        }
        const token = jwt.sign({user: user}, 'secret', {expiresIn: 7200});
        res.status(200).json({
            success: true,
            message: 'Successfully logged in',
            token: token,
            userId: user._id
        });
    });
});

显然,快速连续地将JPanel缩放到不同的大小会导致一些速度变形,原因我不知道。我很欣赏这个答案的编辑,并对这种现象有更好的解释。

我只是为JPanel设置了一个静态大小,让图形完成剩下的工作。