The issue I am having is very simple-I am using Java AWT (and Swing) to make a game, but the initial Graphics.drawString(String str, int x, int y)
call takes way too long. I am not setting a custom font or anything. I am not setting any rendering hints. I am using Mac Sierra 10.12.4 in a MacBook Air-I didn't get to test my program in Windows yet. If that matters at all.
In case you don't want to spend time reading the minimal example, I have a class called GamePanel that extends JPanel (and implements runnable) that I add to a JFrame in the main method. I initialize the game thread in the constructor and start the game loop in the run()
method. The game loop then repeats itself using Swing Timers. In the game loop, I call paintComponent(Graphics g)
through redraw()
-the text is drawn in the paintComponent
method. This isn't really necessary information since the problem is reproducible without a game loop, just with the game thread and the paintComponent
method, but I put this here just incase.
Any help is appreciated.
Game class (JFrame)
public class Game {
public static JFrame frame;
public static JPanel gamePanel;
public static void main(String[] args) {
frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
gamePanel = new GamePanel();
frame.add(gamePanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
GamePanel class (Panel)
public class GamePanel() extends JPanel implements Runnable {
public static Thread thread;
public GamePanel() {
super();
setPreferredSize(new Dimension(640, 480));
setFocusable(true);
requestFocus();
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void run() {
while (! Game.frame.isVisible()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
EventQueue.invokeLater(new Runnable() {
public void run() {gameLoop();}
});
}
public void GameLoop() {
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
gameLoop();
}
public void paintComponent(Graphics g) {
super.paintComponent();
g.drawText("Hello World", 10, 10);
}
}
I know the game loop in the minimal example's speed would be ever-changing (and thats exactly the opposite of what a good game loop should do). But what does that matter in a minimal example?