Java 2D Bug?

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

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();
    }
}
Screen.java
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++;
    }
}
 
versuch mal deine while so umzugestalten:
Java:
while(true) {

    if(RUNNING) {

        // HIER PASSIERT WAS
    }
}
Ich vermute nämlich, dass deine while-schleife manchmal garnicht erst startet, weil RUNNING nicht schnell genug auf true gesetzt wird. So ein ähnliches Problem hatte ich auch schonmal.
 
Hab das geändert...
Code:
private void init() {
        RUNNING=true;
        while(RUNNING!=true){}
        startGameloop();
    }
Funktioniert immer noch nicht...
(Kann der Programm Code eigentlich weiterlaufen bevor die Variable geändert wurde? Ist ja eig. kein neuer Thread...)
Ach ja der Konsolen Output (Ticks/FPS) werden aber angezeigt...
 
Dein Code den du gerade gepostet hast ist Schwachsinn. Wenn du abfragen willst ob etwas nicht true ist musst du bloß !HierDeinBoolean schreiben. HierDeinBoolean != true wird von den meisten IDEs allerdings nicht als Fehler angekannt, funktioniert aber trotzdem nicht.
Hier ist eine Hauptklasse die auf jeden Fall funktioniert:
Java:
public class Game extends JFrame {

    private static final Game game = new Game();

    private final Canvas canvas = new Canvas();

    private boolean running;
    private int         fps;

    public Game() {

        super("Mein Spiel");
        this.init();
    }

    public static void main(String[] args) {

        game.setSize(800, 600);
        game.setLocationRelativeTo(null);
        game.setResizable(false);
        game.setDefaultCloseOperation(EXIT_ON_CLOSE);
        game.setContentPane(game.canvas);
        game.setVisible(true);

        new Thread(new Runnable() {

            @Override
            public void run() {

                try {

                    int fpsLimit = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getRefreshRate();
                    int delta = 15;
                    int frames = 0;
                    long startTimer = System.currentTimeMillis();
                    long updateTimer = 0;

                    while(true) {

                        if(running) {

                            update();
                        }

                        // Der Teil sorgt für VSync
                        Thread.sleep(delta);
                        frames++;
                        updateTimer = System.currentTimeMillis();

                        if(updateTimer - startTimer >= 1000) {

                            fps = frames;
                            frames = 0;
                            startTimer = System.currentTimeMillis();

                            if(fps > fpsLimit) {

                                delta++;

                            } else if(fps < fpsLimit && delta > 0) {

                                delta--;
                            }
                        }
                    }

                } catch(InterruptedException exception) {

                    exception.printStackTrace();
                    System.exit(1);
                }
            }

        }).start();
    }

    public void setRunning(boolean running) {

        this.running = running;
    }

    public void init() {

        // HIER WIRD ALLES INITIALISIERT
    }

    public void render(Graphics graphics) {

        // HIER WIRD GERENDERT
    }

    public void upate() {

        // HIER WIRD ALLES BERECHNET
    }

    public static final Game getGame() {

        return Game.game;
    }

    public final Canvas getCanvas() {

        return this.canvas;
    }

    public boolean isRunning() {

        return this.running;
    }

    public int getFPS() {

        return this.fps;
    }

    public class Canvas extends Container {

        private final Font defaultFont = new Font(Font.MONOSPACED, 0, 16);

        public Canvas() {

            super();
            this.setLayout(null);
            this.setFocusable(true);
        }

        @Ovrride
        public void paint(Graphics graphics) {

            super.paint(graphics);

            graphics.setColor(Color.BLACK);
            graphics.fillRect(0, 0, getWidth(), getHeight());
            graphics.setColor(Color.WHITE);
            graphics.setFont(this.defaultFont);
        }
    }
}
 
Dein Code den du gerade gepostet hast ist Schwachsinn. Wenn du abfragen willst ob etwas nicht true ist musst du bloß !HierDeinBoolean schreiben. HierDeinBoolean != true wird von den meisten IDEs allerdings nicht als Fehler angekannt, funktioniert aber trotzdem nicht.
wieso sollte das nicht funktionieren?

außerdem ist dein code nicht lauffähig, da sind einige syntaxfehler drin. es gibt probleme mit static-zugriffen auf running und fps, außerdem gibt es keine methode update() und keine getRefreshRate()
 
wieso sollte das nicht funktionieren?

außerdem ist dein code nicht lauffähig, da sind einige syntaxfehler drin. es gibt probleme mit static-zugriffen auf running und fps, außerdem gibt es keine methode update() und keine getRefreshRate()
ich hätte bei den zugriffen auf die variablen das statische game Objekt verwenden sollen. die Methode getRefreshRate() existiert in der Klasse DisplayMode. und update() existiert auch, allerdings hatte ich auch dort vergessen das statische game Objekt zu verwenden.
 

Zurück
Oben