JFrame图形闪烁

时间:2014-10-19 23:51:23

标签: java

我正在尝试在加载时创建一个信息JFrame,加载后新框架将打开我正在创建的游戏,唯一的问题是我的加载时间一直闪烁,我知道基本的java,但我不是超级高级,这是我的代码:

package Console;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics; 
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class Main extends Canvas implements Runnable{

public boolean running = false;
public boolean LoadingScreen = false;
public Thread mainThread;
public Thread loadThread;
public boolean finishedLoading = false;

public void start_loading()
{




}

public void stop_loading()
{

    if(!LoadingScreen)
    {

        return;

    }else
    {

        LoadingScreen = false;
        try {
            loadThread.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

public synchronized void start()
{

    if(LoadingScreen)
    {

        return;

    }else
    {

        LoadingScreen = true;
        loadThread = new Thread(this);
        loadThread.start();

    }

    if(running == true)
    {

        return;

    }else
    {

        running = true;
        mainThread = new Thread(this);
        mainThread.start();

    }

}

public synchronized void stop()
{

    if(!running)
    {

        return;

    }else
    {

        running = false;
        try 
        {

            mainThread.join();

        }
        catch 
        (InterruptedException e) 
        {

            e.printStackTrace();

        }

    }

}

public static final int WIDTH = 240, HEIGHT = 260, SCALE = 2;
//public static JTextArea loadingShow = new JTextArea();

public static void main(String args[])
{

    Main Main = new Main();
    Main.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    Main.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    Main.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    JFrame screen = new JFrame("Screen");
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setSize(WIDTH * SCALE, HEIGHT * SCALE);
    screen.setResizable(false);
    //screen.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    screen.add(Main);
    //JTextArea text = new JTextArea();
    //text.setText("Starting up game...\n---------------------------------------\nCredits:\nCreated by Illurity.\n");

    //text.setEditable(false);
    //loadingShow.setEditable(false);
    //screen.add(loadingShow);
    //screen.add(text);
    screen.setVisible(true);

    Main.start();

}

public static int LoadTime = 0;


public void run() 
{

    //Game Loop
    long lastTime = System.nanoTime();
    final double amountOfTicks = 60D;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;

    while(running == true)
    {

        //Loading
        while(LoadingScreen == true)
        {

            while(LoadTime < 10000)
            {

                render();
                LoadTime += 2;

            }

            finishedLoading = true;
            System.out.println("FInished Loading");
            stop_loading();
            start();

        }
        //End loading
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if(delta >= 1)
        {

            tick();
            delta--;
            //number++;
            System.out.println("Running");

        }

        render();

    }

    stop();

}

public void tick()
{} 

public void render()
{

    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null)
    {

        createBufferStrategy(3);
        return;

    }

    Graphics g = bs.getDrawGraphics();


    int fontSize = 16;

    g.setColor(Color.WHITE);

    g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

    g.setColor(Color.BLACK);

    g.drawString("Load: ", 1, 15);
    g.drawString(Integer.toString(LoadTime / 100), 17, 15);

    g.dispose();
    bs.show();

}

}

我犯了一些明显的错误吗?

0 个答案:

没有答案