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