刺激重力介绍编码分配

时间:2017-12-15 04:29:03

标签: java

问候语。我正在尝试制作一个代码,显示一个从顶部掉落的蓝色球,反弹并每次减小其最大点,直到它停留在“地板”上。下面的代码以某种方式允许球反弹而不能降低其最大点。这个项目的最终目标是刺激重力环境,如果球每次弹跳都能改变颜色,那将会很棒。谢谢你的时间:)

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.*;
import static java.lang.Math.sin;

public class FINAL extends Applet implements Runnable {
    Thread t;
    int y = 0;
    int a = 1;
    int h;
    // int p = 5;

    // Setting size of AppletViewer in the init method
    public void init() {
        setSize(400, 300);

    }

    public void start() {
        if (t == null) {
            t = new Thread(this, "New Thread");// New side Thread created on
                                                // start of applet.
            t.start();
        }
    }

    public void stop() {
        if (t != null) {
            t = null;// On stop of applet the created thread is destroyed.
        }
    }

    // Implementation of method run() of Runnable interface.
    public void run() {
        Thread t1 = Thread.currentThread();
        while (t == t1) {
            repaint();
            try {
                Thread.sleep(100); // slepp 100 ms
            } catch (Exception e) {
            }
        }
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.BLUE);
        g.fillOval(100, h, 20, 20);

        System.out.println("y =" + y);
        System.out.println("a =" + a);
        // System.out.println("p = " +p);

        if (y < 290) {
            a = a + 1;
            y = y + a;
            h = y;
        } else if (y > 290) {
            a = a - 1;
            h = h - a;
            y = y + a;
        }
        // else if (y>=574){
        // a = a+1;
        // h = h+a;
        // }
    }

}

1 个答案:

答案 0 :(得分:0)

也许尝试这样的事情:

I/O