正方形划分程序而非绘图

时间:2014-10-27 23:03:25

标签: java swing for-loop

我回到Java,我正在尝试制作一个有正方形的程序,这个正方形会变成较小的正方形,而那些正方形变成4个较小的正方形,依此类推,但当我尝试运行程序它没有做任何事情。

这是我的代码:

package main;

import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;

public class mainPanel extends JPanel {

    @SuppressWarnings("rawtypes")

    ArrayList rects = new ArrayList();

    int rectangles = 1;
    int x = 1;
    int y = 1;
    int size = 1000;

    long lastTime = System.nanoTime();
    int frames = 0;
    int fps = 0;

    public mainPanel() {
        setDoubleBuffered(true);
        rects.add(new cube(x, y, size));

        Thread t = new Thread() {
            public void run() {
                while (1 != 2) {
                    repaint();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        };

        t.start();

    }

    @SuppressWarnings("unchecked")
    public void paint(Graphics g) {

        super.paint(g);

        Graphics g2 = g;

        for (int i = 0; i < rectangles; i++) {
            g.fillRect(x, y, size, size);

            size /= 4;
            x += size / 2 + 1;
            y += size / 2 + 1;
            rectangles += 4;
        }

        frames++;
        if (System.nanoTime() - lastTime >= 1000000000) {
            fps = frames;
            frames = 0;
            lastTime = System.nanoTime();
        }

        g.drawString("FPS: " + fps, 5, 15);
        g.dispose();
    }

}

当我运行程序时,即使我将睡眠功能减慢到1000毫秒,根本没有绘制正方形,随着时间的推移,我的fps会慢下来。

0 个答案:

没有答案