Ustawienie obrazu na cały ekran.

0

Hejka, chce zrobić grę i chcę, żeby obraz był na cały ekran bez widocznej ramy, ale coś mi to nie idzie...
Klasa odpowiedzialna za ramę wygląda następująco:

package com.game.engine;

import javax.swing.*;
import java.awt.*;

public class Frame {

    private static final JFrame frame = new JFrame("Medieval Wizard");

    private static GraphicsDevice graphicsDevice;

    private GraphicsEnvironment graphicsEnvironment;

    public void setFrame() {
        graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Engine(graphicsDevice.getDisplayMode().getWidth(), graphicsDevice.getDisplayMode().getHeight()));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        setFullScreen();
    }

    private void setFullScreen() {
        graphicsDevice.setFullScreenWindow(frame);
    }

    public static GraphicsDevice getGraphicsDevice() {
        return graphicsDevice;
    }
}

Ktoś wie co robię nie tak?
Rezultat mam taki:
screenshot-20220624165843.png

0

https://stackoverflow.com/questions/11570356/jframe-in-full-screen-java

No i pisanie gier w Java to trochę karkołomne podejście.

0
piotrpo napisał(a):

https://stackoverflow.com/questions/11570356/jframe-in-full-screen-java

No i pisanie gier w Java to trochę karkołomne podejście.

Dzięki, o dziwo na pc z monitorem o wymiarach 1280/1024 wszystko działa, a na laptopie jak nie chciało tak nie chce... :/ screenshot-20220626105615.png

0

No ogólnie to ja widzę tutaj taką kwestię, że:

  • masz okno - dobierasz je prawidłowo
  • obiekt typu Engine, który nie jest skalowany

Trudno cokolwiek powiedzieć o tym obiekcie, bo nie wiem co w nim jest.

0
wartek01 napisał(a):

Trudno cokolwiek powiedzieć o tym obiekcie, bo nie wiem co w nim jest.

Engine wygląda tak:

package com.game.engine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class Engine extends JPanel implements Runnable, KeyListener {

    private Thread thread;
    private boolean running;

    private BufferedImage image;
    private Graphics g;

    private GUIStateManager GUIStateManager;

    public Engine(int width, int height) {
        super();
        setPreferredSize(new Dimension(width, height));
        setFocusable(true);
        requestFocus();
    }
    public void addNotify() {
        super.addNotify();
        if (thread == null) {
            thread = new Thread(this);
            addKeyListener(this);
            thread.start();
        }
    }

    @Override
    public void run() {
        init();

        int fps = 0;
        double timer = System.currentTimeMillis();
        long lastTime = System.nanoTime();
        double delta = 0;
        int FPS = 60;
        double ns = 1000000000.0 / FPS;
        while (running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while (delta >= 1) {
                update();
                draw();
                drawToScreen();
                fps++;
                delta--;
            }
            if(System.currentTimeMillis() - timer >= 1000){
                //System.out.println("FPS: " + fps + " per second");
                fps = 0;
                timer += 1000;
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        GUIStateManager.keyPressed(e);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        GUIStateManager.keyReleased(e);
    }

    private void update() {
        GUIStateManager.update();
    }


    private void draw() {
        GUIStateManager.draw(g);
    }

    private void init() {
        image = new BufferedImage(Frame.getGraphicsDevice().getDisplayMode().getWidth(),
                Frame.getGraphicsDevice().getDisplayMode().getHeight(),
                BufferedImage.TYPE_INT_RGB);
        g = image.getGraphics();
        running = true;
        GUIStateManager = new GUIStateManager();
    }

    private void drawToScreen() {
        setBackground(Color.BLUE);
        Graphics g2 = getGraphics();
        g2.drawImage(image, 0, 0,
                Frame.getGraphicsDevice().getDisplayMode().getWidth(),
                Frame.getGraphicsDevice().getDisplayMode().getHeight(),
                null);
        g2.dispose();
    }

    private void drawLoadingScreen() {
        setLayout(null);
    }
}
0

Problem leży w linijce:

g2.drawImage(image, 0, 0, Frame.getGraphicsDevice().getDisplayMode().getWidth(), Frame.getGraphicsDevice().getDisplayMode().getHeight(), null)

Z dokumentacji:

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle. The image is drawn inside the specified rectangle of this graphics context's coordinate space, and is scaled if necessary. Transparent pixels do not affect whatever pixels are already there.

This method returns immediately in all cases, even if the entire image has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete, then drawImage returns false. As more of the image becomes available, the process that loads the image notifies the image observer by calling its imageUpdate method.A scaled version of an image will not necessarily be available immediately just because an unscaled version of the image has been constructed for this output device. Each size of the image may be cached separately and generated from the original data in a separate image production sequence.

IMO według mnie scaled to fit inside należy rozumieć jako "będzie skalowana tak, żeby się zmieścić w", tj. jeśli będzie za duża to się zmniejszy - natomiast gdy kontener na obrazek będzie większy to niekoniecznie musi być wyskalowane.

Jeśli miałbym zmieniać kod to:

  1. Na starcie dorzuciłbym this.repaint() po wywołaniu drawToScreen()
  2. Możesz też spróbować dorzucić odpowiednie RenderingHint do obiektu Graphics - być może uda się skłonić renderer do działania.
  3. Spróbowałbym skalować obiekt ręcznie (używając Image.getScaledInstance(int, int, int))

1 użytkowników online, w tym zalogowanych: 0, gości: 1