搜尋
首頁Javajava教程如何在 Java 應用程式中顯示多個彈跳球而不互相覆蓋?

How Can I Display Multiple Bouncing Balls in a Java Application Without Overwriting Each Other?

Java 中的彈跳球

問題: 將多個球加入 Java應用程式會產生初始球被第二個覆蓋,導致只有一個可見

解決方案:

要解決此問題並在螢幕上繪製多個彈跳球,請考慮以下方法:

  • 刪除不透明佈局管理器以防止組件重疊並確保正確控制球的大小和位置。
  • 實現您自己的佈局管理器來控制球的大小和位置

2.隨機化球屬性:
  • 隨機化每個球的速度和起始位置,以減少它們碰撞或從同一位置開始的可能性。

3。使用事件調度執行緒 (EDT):

  • 在 EDT 的上下文中更新球,以確保平滑一致的圖形更新。

< ;h4>4。使用球的容器類:

  • 不要將球視為不透明組件,而是創建一個表示每個球的屬性的類,例如其顏色、速度和位置,不渲染任何視覺效果。

5。為球運動建立單獨的線程:

  • 實作此方法可能會佔用大量資源,因此首先探索其他替代方案。

程式碼範例(使用容器類別Balls):

public class Balls extends JPanel {

    private List<ball> ballsUp;

    // ... (Constructor and other methods)

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for (Ball ball : ballsUp) {
            ball.paint(g2d);
        }
        g2d.dispose();
    }

    public List<ball> getBalls() {
        return ballsUp;
    }
}

public class Ball {

    private Color color;
    private Point location;
    private Dimension size;
    private Point speed;

    // ... (Constructor and other methods)

    protected void paint(Graphics2D g2d) {

        Point p = getLocation();
        if (p != null) {
            g2d.setColor(getColor());
            Dimension size = getSize();
            g2d.fillOval(p.x, p.y, size.width, size.height);
        }

    }
}

public class BounceEngine implements Runnable {

    private Balls parent;

    // ... (Constructor and other methods)

    public void run() {

        // Randomize ball properties and starting positions
        for (Ball ball : getParent().getBalls()) {
            // ... (Randomization logic)
        }

        while (getParent().isVisible()) {

            // Repaint the balls
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    getParent().repaint();
                }
            });

            // Move each ball
            for (Ball ball : getParent().getBalls()) {
                move(ball);
            }

            // Delay between updates
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }

        }

    }

    // ... (Remaining code)

}</ball></ball>

這種使用容器類別和單獨執行緒進行球運動的方法可以更好地控制球的行為,並允許更複雜的球動畫。

以上是如何在 Java 應用程式中顯示多個彈跳球而不互相覆蓋?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。