This is the basic structure I usually end up with whenever I'm writing a simple application. More recently I've learned about singletons and have started to incorporate them into my design, but other that that I haven't changed much about it in recent times.
I've been wondering if there is anything in here that I could improve on or that is considered bad practice.
Main
import javax.swing.*;
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
e.printStackTrace();
}
Controller.getInstance();
}
}
Controller
import javax.swing.*;
public class Controller {
private static Controller instance;
private static final int FPS = 30;
public static final int WIDTH = 600;
public static final int HEIGHT = 800;
private final JFrame frame;
private Timer timer;
public static Controller getInstance() {
return instance != null ? instance : (instance = new Controller());
}
private Controller() {
//Do setup
frame = new AppFrame();
timer = new Timer(1000/FPS, e -> update());
timer.start();
}
private void update() {
//Update model
frame.repaint();
}
}
AppFrame
import javax.swing.*;
public class AppFrame extends JFrame {
public AppFrame() {
super("My Example App");
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(new AppPanel());
//Do more setup if applicable
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
AppPanel
import javax.swing.*;
import java.awt.*;
public class AppPanel extends JPanel {
private final Dimension DIMENSION = new Dimension(Controller.WIDTH, Controller.HEIGHT);
@Override
public Dimension getPreferredSize() {
return DIMENSION;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//Draw things on g2
}
}