Hỏi cách làm 2 quả bóng chạy hình tròn

package ballgame;

import javax.swing.*; // giống #include
import java.awt.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class BallGame extends JFrame implements Runnable {

    int x = 50;
    int y = 100;
    int r = 10;

    public BallGame() {

        setSize(400, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Thread t = new Thread(this);
        t.start();

    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.black);
        g.fillOval(100, 150, 20, 20);// x y r r
        g.setColor(Color.red);
        g.fillOval(10, 30, 20, 20);
        g.fillOval(x, y, r, r);

    }

    public void run() {
        try {
            while (true) {
                x += 5;
                y += 5;
                repaint();
                Thread.sleep(150);

            }
        } catch (Exception e) {
        }

    }

    public static void main(String[] args) {
        BallGame game = new BallGame();
        game.setVisible(true);
    }

}

Bạn post lên để làm gì thế bạn? Cho thêm mục đích vào bài post đi bạn. Đây là câu hỏi hay là chia sẻ :smiley:

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?