簡體   English   中英

為什么不使用無限運行的while循環就不能畫線

[英]Why can't I draw my line without the use of an infinitely running while loop

我必須使用永久運行的 while 循環來繪制我的線條,否則線條會呈現一毫秒並消失。 它可能看起來不錯,但我可能會添加數百個將在一個循環中運行的方法,這可能會導致一些性能問題。

調試(主類)

public class Debug {
    public static void main(String[] args) {
        boolean running = true;
        Window test = new Window(800, 600, 100, 10, true, "This is the title");
        Renderer3D renderer = new Renderer3D();
        
        // my permanantly running while loop
        while (running) {
            renderer.draw();
        }

        test.addRenderer();
    }
}

Window(class創建窗口)

public class Window 
    static int width;
    static int height;
    static int x;
    static int y;
    static boolean v;
    static String title;
    
    public Window() {}
    
    public Window(int WIDTH, int HEIGHT, int X, int Y, boolean V, String TITLE) {
        // TODO Auto-generated constructor stub
        width = WIDTH;
        height = HEIGHT;
        x = X;
        y = Y;
        v = V;
        title = TITLE;
    }

    public static JFrame window = new JFrame();

    public void setWindowVisible(boolean v) {
        window.setVisible(v);
    }

    public void setWindowSize(int Width, int Height) {
        window.setSize(Width, Height);
    }

    public void setWindowLocation(int X, int Y) {
        window.setLocation(X, Y);
    }
    
    public void setWindowTitle(String TITLE) {
        window.setTitle(TITLE);
    }

    public static void displayProperties() {
        window.setSize(width, height);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocation(x, y);
        window.setTitle(title);
        window.setVisible(v);
        window.setLocationRelativeTo(null);
    }
    
    public void addRenderer() {
        window.pack();
        displayProperties();
    }
    
    public JFrame returnWindow() {
        return window;
    }
}

渲染器(class 來繪制東西)

public class Renderer3D extends JFrame {
    public boolean executed;
    public boolean running = true;
    public Thread thread;

    public Canvas canvas = new Canvas() {
        public void paint(Graphics g) { 
        }
    };

    @SuppressWarnings("deprecation")
    public Renderer3D() {
        canvas.setBackground(Color.black);
        Window.window.add(canvas);
        Window.displayProperties();
        Window.window.show();
    }
    
    public void draw() {
        if (canvas == null) {
            canvas.createBufferStrategy(3);
        }
        
        Graphics g = canvas.getGraphics();
        g.setColor(Color.BLUE);
        g.drawLine(100, 100, 300, 300);
    }
}

您需要在paint()中調用draw()方法,就像Canvas中的 Canvas 一樣, paint()方法會繪制此 canvas。 無需在Debug class 中顯式調用它。

完全刪除 while 循環及其內部的內容。 在您的Renderer3D class 中,像這樣修改 canvas 創建部分:

public Canvas canvas = new Canvas() {
    public void paint(Graphics g) {
        draw();
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM