-1
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JPanel;

public class Circle extends JPanel {

    int x = 75;
    int y = 100;
    int diameter = 50;

    public void setAnimationY(int y) {
        this.y = y;
    }

    public int getAnimationY() {
        return y;
    }

    public int getDiameter() {
        return diameter;
    }

    public void setDiameter(int startDiameter) {
        diameter = startDiameter;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(x, y, diameter, diameter);
    }
}


import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BouncingBall extends JApplet {

    private int speed = 5;
    private Timer timer;
    private Circle draw;

    @Override
    public void init() {
        super.init();
        setLayout(new BorderLayout());
        draw = new Circle();
        add(draw);
        timer = new Timer(30, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int y = draw.getAnimationY();
                int diameter = draw.getDiameter();
                int roof = getHeight();
                y += speed;
                if (y < 0) {
                    y = 0;
                    speed *= -1;
                } else if (y + diameter > roof) {
                    y = roof - diameter;
                    speed *= -1;
                }
                draw.setAnimationY(y);
                repaint();
            }
        });
    }

    @Override
    public void start() {
        super.start();
        timer.start();
    }

    @Override
    public void stop() {
        timer.stop();
        super.stop();
    }
}

我正在尝试创建一个 JApplet,其中包含一个上下弹跳的球。到目前为止,我已经能够让球上下移动,但现在我正在努力让球更“栩栩如生”,所以我希望每次球反弹时球的高度都会降低,直到最终停止。

我试图使用我为 getHeight() 方法创建的屋顶变量做一个 while 循环,但由于某种原因,当我尝试使用它时,球根本没有移动,或者循环对球没有影响。

我也尝试了 for 循环,但遇到了与 while 循环相同的问题。我认为问题在于我没有将这个 for 循环放在正确的位置以使其正常工作。

提前致谢。

4

2 回答 2

0

好吧,让我们继续使用您其他相关问题中的@MadProgrammer解决方案

在您的类中,DrawPane我们可以轻松地定义height,getAnimationHeight()setAnimationHeight(int)控制高度在接触地面时立即减小。请记住,在java中左上坐标是(0, 0)右下坐标是(getWidth(), getHeight())。假设它从height = 0(顶部)开始。然后它将从y = height(顶部)开始并最终移动到getHeight()容器的(底部)。我们将通过向 current (which will return)添加一个 amount(say ) 来增加height(top y) using 。setAnimationHeight()30heightgetAnimationHeight()

因此,在您的另一个问题中对@MadeProgrammer 的解决方案所做的这个小调整将是以下演示

在此处输入图像描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

public class Circle extends JApplet {

    private int delta = 8;

    private Timer timer;
    private DrawPane drawPane;

    @Override
    public void init() {
        super.init();
        setLayout(new BorderLayout());
        drawPane = new DrawPane();
        add(drawPane);
        timer = new Timer(100, new ActionListener() {

            int frameCount = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                int y = drawPane.getAnimationY();
                int diameter = drawPane.getDiameter();
                y += delta;
                if (y < drawPane.getAnimationHeight()) {
                    y = drawPane.getAnimationHeight();

                    delta *= -1;
                } else if (y + diameter > getHeight()) {
                    y = getHeight()- diameter;
                    delta *= -1;

                    int animationHeight =  drawPane.getAnimationHeight();
                    animationHeight = animationHeight + (getHeight() - diameter - animationHeight)/2;

                    drawPane.setAnimationHeight(animationHeight);

                    if(animationHeight + diameter + 2 >= getHeight())
                    {
                        System.out.println("true");
                        drawPane.setAnimationY(getHeight() - diameter);
                       repaint();
                        timer.stop();
                        return;
                    }


                }
                drawPane.setAnimationY(y);
               repaint();



            }
        });
    }

    @Override
    public void start() {
        super.start();
        timer.start();
    }

    @Override
    public void stop() {
        timer.stop();
        super.stop();
    }

    public class DrawPane extends JPanel {

        int x = 100;
        int y = 0;
        int diameter = 50;
        int height = 0;


        public void setAnimationX(int x) {
            this.x = x;
        }

        public void setAnimationY(int y) {
            this.y = y;
        }



        public void setAnimationHeight(int h)
        {
            height = h;
        }

        public int getAnimationHeight()
        {
           return  height;
        }

        public int getAnimationX() {
            return x;
        }

        public int getAnimationY() {
            return y;
        }

        public int getDiameter() {
            return diameter;
        }

        public void setDiameter(int startDiameter) {
            diameter = startDiameter;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(x, y, diameter, diameter);
        }
    }
}

注意:一旦它最终触及底部,您应该停止计时器以消除球的闪烁。此任务留给您作为练习

于 2013-10-29T20:12:26.567 回答
0

对您的代码进行少量修改,可以为您提供一些线索:

            @Override
            public void actionPerformed(ActionEvent e) {
                int y = draw.getAnimationY();
                int diameter = draw.getDiameter();
                int roof = getHeight();
                y += speed;
                //
                // Reduce the ball size at the bottom of the screen
                //
                if(y + diameter > roof) {
                    if(diameter > minDiameter) {
                        diameter -= (roof - y);                 
                    } else {
                        diameter = minDiameter;
                    }                   
                } else if (diameter < maxDiameter) {
                    diameter++;
                }
                draw.setDiameter(diameter);
                if (y < 0) {
                    y = 0;
                    speed *= -1;    
                } else if (y + diameter > roof) {
                    y = roof - diameter;
                    speed *= -1;                    
                }                   
                //  Simulates a little gravity
                speed += 0.5;
                draw.setAnimationY(y);
                repaint();
            }

为了更真实,最好的方法是找到一个方程,它是球位置和球硬度系数的函数,并会给出球的大小。

于 2013-10-29T18:55:19.953 回答