bouncepanel.java

来自「Usefull sample codes for Java. Containt 」· Java 代码 · 共 65 行

JAVA
65
字号
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class BouncePanel extends JPanel implements Runnable {
    Image ball, court;
    float current = 0F;
    Thread runner;
    int xPosition = 10;
    int xMove = 1;
    int yPosition = -1;
    int ballHeight = 185;
    int ballWidth = 190;
    int height;

    public BouncePanel() {
        super();
        Toolkit kit = Toolkit.getDefaultToolkit();
        ball = kit.getImage("tennis.gif");
        court = kit.getImage("court.jpg");
        runner = new Thread(this);
        runner.start();
    }

    public void paintComponent(Graphics comp) {
        Graphics2D comp2D = (Graphics2D) comp;
        height = getSize().height - ballHeight;
        if (yPosition == -1) {
            yPosition = height - 20;
        }
        if ((court != null) && (ball != null)) {
            comp2D.drawImage(court, 0, 0, this);
            comp2D.drawImage(ball,
               (int) xPosition,
               (int) yPosition,
               this);
        }
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (runner == thisThread) {
            current += (float) 0.1;
            if (current > 3) {
                current = (float) 0;
            }
            xPosition += xMove;
            if (xPosition > (getSize().width - ballWidth)) {
                xMove *= -1;
            }
            if (xPosition < 1) {
                xMove *= -1;
            }
            double bounce = Math.sin(current) * height;
            yPosition = (int) (height - bounce);
            repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // do nothing
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?