I am trying to code an application that gets user mouse input and draws a dot on each click (not that that is especially important). Where should the game loop functions be, in the paint method or in a separate while loop? Here is my code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Main extends JPanel{
static Rectangle scrnSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
//static Dimension wholeScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
//public static int taskbarHeight = wholeScreenSize.height - scrnSize.height;
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillOval(0, 0, 30, 30);
g2d.drawOval(0, 50, 30, 30);
g2d.fillRect(50, 0, 30, 30);
g2d.drawRect(50, 50, 30, 30);
g2d.draw(new Ellipse2D.Double(0, 100, 30, 30));
//do loop stuff here?
}
public static void main(String[] args) throws InterruptedException {
//init
JFrame frame = new JFrame("DrillSweet");
frame.setLayout(new GridLayout());
Main main = new Main();
frame.add(main);
frame.setSize(new Dimension(scrnSize.width, scrnSize.height));
frame.setPreferredSize(new Dimension(scrnSize.width, scrnSize.height));
frame.setMaximumSize(new Dimension(scrnSize.width, scrnSize.height));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//game loop
while(true){
//do loop stuff here?
main.repaint();
Thread.sleep(10);
}
}
}