简体   繁体   中英

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

I have to use a permanently running while loop to draw my line, otherwise the line renders for a millisecond and goes away. It may look fine but I may add hundreds upon hundreds of methods that would be running in one loop, that may cause some performance issues.

Debug (The main class)

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 (the class to create the window)

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;
    }
}

Renderer (the class to draw things)

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);
    }
}

You need to call your draw() method inside paint() as for Canvas in JAVA, paint() method paints this canvas. No need to call it explicitly in your Debug class.

Completely remove the while loop and what is inside it. In your Renderer3D class, modify the canvas creation part like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM