Why is my eclipse not setting my background color?

2019-09-10 18:42发布

问题:

I have designed a code for a game. The problem is that the background won't change to any of my selected colors I have picked from graphics color library.

I need someone to figure this out with the code i have provided (please don't make a whole new code). idk why java/ eclipse won't display it??? am i missing something?? The program here should display a GUI with a background color blue. instead i get white.

public class MainApp extends Canvas implements Runnable {

    private static final long serialVersionUID = 8928635543572788908L;

    private static final int WIDTH= 648, HEIGHT= WIDTH/ 12 * 9;
    private Thread thread;
    private boolean running= false;

    public MainApp()
    {
        new Window(WIDTH, HEIGHT, "App", this);
    }
    public synchronized void start()
    {
        thread= new Thread(this);
        thread.start();
        running= true;
    }
    public void run()
    {
        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();
                delta--;
            }
            if(running)

            render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000)
            {
                timer += 1000;
                System.out.print("FPS: " + frames);
                frames= 0;
            }
        }
        stop();
    }

    public synchronized void stop()
    {
        try
        {
            thread.join();
            running= false;
        }catch(Exception e){e.printStackTrace();}
    }
    public void tick()
    {

    }
    public void render()
    {
        BufferStrategy bs= this.getBufferStrategy();
        if(bs== null)
        {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g= bs.getDrawGraphics();
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.dispose();
        bs.show();
    }
    public static void main(String[] args) {
        new MainApp();
    }
}

回答1:

Your codes a little messed up, you shouldn't be making a new instance of Window from MainApp, the Window should be creating it (IMHO).

Also, you should be overriding the getPreferredSize method the the MainApp, as this is what should be controlling the viewable size of the window, this way, when you use pack on the JFrame, it will ensure that the window is larger then the preferredSize of it's contents, allowing the frame decorations to wrap around the outside of it.

BUT, the main problem you have, is adding the MainApp to the JFrame AFTER it's already been made visible

The following works for me...

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class MainApp extends Canvas implements Runnable {

    private static final long serialVersionUID = 8928635543572788908L;

    private static final int WIDTH = 648, HEIGHT = WIDTH / 12 * 9;
    private Thread thread;
    private boolean running = false;

    public MainApp() {
        new Window("App", this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(WIDTH, HEIGHT);
    }

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

    public void run() {
        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();
                delta--;
            }
            if (running) {
                render();
            }
            frames++;

            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.print("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }

    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void tick() {

    }

    public void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        new MainApp();
    }

    public static class Window {

        private Window(String title, MainApp app) {
            JFrame frame = new JFrame(title);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(app);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            app.start();
        }

    }

}


标签: java awt