我想在java中製作遊戲並遇到問題。當我的程序重新繪製我的JFrame時,它需要CPU的很多能量,我想知道是否有一些我忽略的明顯錯誤或者減少CPU使用率的方法。重新繪製JPanel吃掉CPU
Frame.java
public class Frame extends JFrame {
JFrame frame;
public static Panel panel;
public void makeFrame() {
frame = new JFrame("Game");
panel = new Panel();
panel.init();
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.add(panel);
}
}
GameLoop.java
public class GameLoop {
//render gets called 30 times per second
private void render() {
Frame.panel.repaint();
}
}
Panel.java
public class Panel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
mvmt.drawCha(g2d); //mvmt is an instance of Movement
}
}
Movement.java
public class Movement {
public void drawCha(Graphics2D g2d) {
g2d.drawImage(charImg, x , y, x + 40 , y + 40, 0, 0, 17, 28, null);
}
}
在此先感謝您的幫助!
出於興趣你正在使用哪種CPU,屏幕分辨率是多少? – sorifiend
@sorifiend這是一個英特爾i5第7代,屏幕是1920x1080 –
重新繪製真的需要30/s?如果任何狀態改變,爲什麼不只是重新噴漆? –