-1

I have been trying to convert one of my games into an Applet but since it is my first time using an Applet I ran into a problem. Basically whenever I open the game the screen start blinking. I'm pretty sure that it is due to not having a BufferStrategy but whenever I try to create one like "BufferStrategy bg = this.getBufferStrategy()" it doesn't work. Can someone help?

package main;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JApplet;

import Handling.Handler;
import Other.HUD;
import Other.KeyInput;


public class Game  extends JApplet implements Runnable{

    private static final long serialVersionUID = 9021342660060318393L;

    public static final int WIDTH = 640;
    public static final int HEIGHT = WIDTH / 12 * 9;

    private Thread thread;
    private boolean running = false;

    private Handler handler;
    private HUD hud;
    private Spawner spawner;
    private Menu menu;

    public enum STATE{
        Menu(),
        Help(),
        Settings(),
        DeathScreen(),
        Game();
    };

    public enum DIFFICULTY{
        Easy(),
        Mediuм(),
        Hard();
    };

    public STATE GameState = STATE.Menu;
    public DIFFICULTY difficulty = DIFFICULTY.Easy;







    private void tick(){
        handler.tick();
        if(GameState == STATE.Game){
            hud.tick();
            spawner.tick();
        }else if(GameState == STATE.Menu){
            menu.tick();
        }
    }

    public void paint(Graphics g){

        //DRAW HERE
        g.setColor(Color.black);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        handler.paint(g);

        if(GameState == STATE.Game){
            hud.paint(g);
        }else if(GameState == STATE.Menu || GameState == STATE.Help || GameState == STATE.Settings || GameState == STATE.DeathScreen){
            menu.paint(g);
        }
        //STOP DRAWING

        g.dispose();
    }








    public void init(){
        setSize(WIDTH,HEIGHT);

        handler = new Handler();
        menu = new Menu(this, handler);
        this.requestFocusInWindow(true);
        this.setFocusable(true);
        this.addKeyListener(new KeyInput(handler));
        this.addMouseListener(menu);
        hud = new HUD();
        spawner = new Spawner(handler, hud, this);
    }

    public void start(){
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public void destroy(){}

    public void stop(){
        try {
            System.exit(1);
            thread.join();
            running = false;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public void run() {
        this.requestFocus();
        long LastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while(running){
            long now = System.nanoTime();
            delta += (now - LastTime) / ns;
            LastTime = now;
            while(delta >= 1){
                tick();
                repaint();
                delta--;
            }
            frames++;

            if(System.currentTimeMillis() - timer > 1000){
                timer += 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
            try {
                thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        stop();
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hasapin
  • 1
  • 2
  • Have you tried manual double buffering or adding, for instance, a JPanel on which to draw (which should be double buffered by default)? – Gelunox Nov 10 '16 at 09:50
  • 1) `g.dispose();` Never dispose a `Graphics` object that your code did not create. 2) Change `public void paint(Graphics g){ //DRAW HERE` to `public void paint(Graphics g){ super.paint(g); //DRAW HERE` in order to respect the paint chain. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 4) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Nov 10 '16 at 15:36
  • 5) `public void stop(){ try { System.exit(1);` A) The Java security system should prevent calls to `System.exit(int)` An applet will be stopped when it is told to by the JVM - it simpl means the applet itself should dicontinue processing, the applet itself should not try to stop the JVM. B) But note that any integer other than `0` is reserved for abnormal exit, whereas the `stop()` method is called by other processes to indicate a normal, and otherwise completely expected stop to (**current**) applet activities. – Andrew Thompson Nov 10 '16 at 15:44
  • 6) `public void init(){ setSize(WIDTH,HEIGHT);` You're level of knowledge of robust applet development is ..minimal. In this case, an applet's size is set in the HTML, and it should not try to 2nd guess, or change, that size - it **must** work with whatever size it has been assigned. – Andrew Thompson Nov 10 '16 at 15:53

1 Answers1

-1

Instead of calling repaint(), try to call

paint(getGraphics()). 

Another thing, add this method to your applet:

public void update(java.awt.Graphics g) {}
  • Both these pieces of advice are poor programming at best, and utterly screw things at worst. Programming by voodoo is highly inadvisable. – Andrew Thompson Nov 10 '16 at 15:40
  • The second tip is a very old tip, used in Java code 19 years ago. Not sure if its outdated, but since Java still supports 1.1, it may be valid. – Guilherme Campos Hazan Nov 10 '16 at 18:19
  • *"Not sure if its outdated"* It is. Don't use advice intended for AWT. Even then it was pretty poor advice. Where did you see it (link)? – Andrew Thompson Nov 10 '16 at 18:58