Ich habe einen Frame mit einem Panel das für den Anfang ein kleines Rechteck Anzeigt
manchmal funktioniert es manchmal nicht. (80% nicht,20% schon) woran liegt das Source Code unten:
Game.java
Screen.java
manchmal funktioniert es manchmal nicht. (80% nicht,20% schon) woran liegt das Source Code unten:
Game.java
Code:
import javax.swing.JFrame;
import com.karegame.gui.Screen;
public class Game {
public static final intHEIGHT=500;
public static final intWIDTH=500;
public static final String TITLE="2D Game";
public static final Screen SCREEN=new Screen();
public static void main(String [] args){
JFrame frame = new JFrame();
frame.setBounds(0, 0, WIDTH, HEIGHT);
frame.setVisible(true);
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(SCREEN);
SCREEN.start();
}
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;
public class Screen extends JPanel {
public int x=0;
public int y=0;
public boolean RUNNING=false;
public long fps=0;
public long updates=0;
long nowUpdate = System.currentTimeMillis();
long nowCounter = System.currentTimeMillis();
public void start(){
init();
}
private void init() {
RUNNING=true;
startGameloop();
}
public void startGameloop() {
System.out.println("Starting Game Loop!");
this.repaint();
while(RUNNING){
/*render();
if(System.currentTimeMillis()-nowUpdate>=20){
nowUpdate = System.currentTimeMillis();
update();
}
if(System.currentTimeMillis()-nowCounter>=1000){
nowCounter = System.currentTimeMillis();
System.out.println(fps+" | "+updates);
fps=0;
updates=0;
}*/
}}
public void render() {
repaint();
addFrame();
}
public void update() {
addUpdate();
x++;
y++;
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.BLUE);
g2d.fillRect(x, y, 10, 10);
}
public void addFrame(){
fps++;
}
public void addUpdate(){
updates++;
}
}