屏幕尺寸自动调整

时间:2016-06-01 22:39:11

标签: java

所以目前我正在制作游戏,我想让它变得简单,以便所有屏幕尺寸都可以使用它。但是我不知道如何做到这一点,目前我只是将它设置为默认分辨率为1600 x 900.如果你们知道我如何能够调整所有尺寸,我会很感激帮助:)

我查看了我的所有代码,但如果您需要有关我的问题的更多信息,我就不知道我需要做些什么才能完成这项工作。

非常感谢,

package net.captainash123.minitale;

import java.applet.*;
import java.awt.*;

import javax.swing.*;

import java.util.*;

public class Main extends Applet implements Runnable {
    private static final long serialVersionUID = 1L;

    public static byte pixelSize = 5;

    public static int moveFromBorder = 0;
    public static double sX = moveFromBorder, sY = moveFromBorder;
    public static double dir = 0;

    public static int width = 1600;
    public static int height = 900;

    public int timer = 0;
    public byte movingTimer = 0;

    public static Dimension realSize;
    public static Dimension size = new Dimension(1600, 900);
    public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);

    public static Point mse = new Point(0, 0);

    public static String name = "Minitale: Beta - 1.1.3";
    public static String deathText = "You Died";

    public static boolean isRunning = false;
    public static boolean isMoving = false;
    public static boolean isJumping = false;
    public static boolean isMouseLeft = false;
    public static boolean isMouseRight = false;

    private Image screen;

    public int FPS;
    public int totalTime;

    public static Level level;
    public static Character character;
    public static Inventory inventory; 
    public static CraftingTable craftingTable;
    public static Sky sky;
    public static Checker checker;
    public static ArrayList<Mob> mob = new ArrayList<Mob>();

    public Main() {
        setPreferredSize(size);

        addKeyListener(new Listening());
        addMouseListener(new Listening());
        addMouseMotionListener(new Listening());
        addMouseWheelListener(new Listening());

        }


    public void start() {
        requestFocus();

        //Defining objects etc.
        new Tile(); //Loading Images.
        level = new Level();
        character = new Character(Tile.tileSize, Tile.tileSize * 2);
        inventory = new Inventory();
        craftingTable = new CraftingTable();
        Furnace furnace = new Furnace();
        sky = new Sky();
        checker = new Checker();
        //mob.add(new Zombie(50, 10, Tile.tileSize, Tile.tileSize * 2, Tile.zombie));
        //mob.add(new P2(60, 10, Tile.tileSize, Tile.tileSize * 2, Tile.P2));
        //mob.add(new Turtle(40, 10, Tile.tileSize  * 2, Tile.tileSize, Tile.turtle));
        mob.add(new Chicken(40, 10, Tile.tileSize, Tile.tileSize, Tile.chicken));
        mob.add(new Pig(60, 10, Tile.tileSize, Tile.tileSize, Tile.pig));

        //Starting game loop.
        isRunning = true;
        new Thread(this).start();
    }

    public void stop() {
        isRunning = false;
    }

    private static JFrame frame;
    public static void main(String args[]) {
        Main component = new Main();

        frame = new JFrame();
        frame.add(component);
        frame.setIconImage(new ImageIcon("assets/textures/gui/gameIcon.png").getImage());
        frame.pack();

        realSize = new Dimension(frame.getWidth(), frame.getHeight());

        frame.setTitle(name);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        component.start();
    }

    public void tick() {
        if(frame.getWidth() != realSize.width || frame.getHeight() != realSize.height) {
            frame.pack();
        }
        character.tick();
        level.tick((int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
        sky.tick();
        checker.tick();
        craftingTable.checkRecipes();
        Furnace.checkRecipes();

        for(int i = 0; i < mob.toArray().length; i++) {
            mob.get(i).tick();
        }
    }

    public void render() {
        Graphics g = screen.getGraphics();

        //Drawing things. //102, 178, 255 blue sky.
        sky.render(g);

        level.render(g, (int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
        character.render(g);
        for(int i = 0; i < mob.toArray().length; i++) {
            mob.get(i).render(g);
        }
        inventory.render(g);
        craftingTable.render(g);
        Furnace.render(g);

        if(character.isDead) {
            g.setColor(Color.BLACK); //255, 0 ,0, 50
            g.fillRect(0, 0, width, height);
            g.setColor(Color.WHITE);
            g.setFont(new Font("Calibri", Font.BOLD, 24));
            g.drawString(deathText, 116, 150);
        }

        if(timer < 270) {
            timer++;
            movingTimer++;
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, width, height);
            if(movingTimer > 10) {
            }
            if(movingTimer > 20){
                movingTimer = 0;
            }
            g.setColor(new Color(108, 108, 108));
            g.setFont(new Font("Calibri", Font.BOLD, 24));
            g.drawString("Minitale", width / 16 + 16, height / 8 + 30);
            g.drawImage(Tile.Splash, width / 32 + 34, height / 32 - 40, null);
        }

        g = getGraphics();

        g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
        g.dispose();

    }

    public void run() {
        screen = createVolatileImage(pixel.width, pixel.height);

        int frames = 0;
        double nonProcessedSeconds = 0;
        long previousTime = System.nanoTime();
        double secondsPerTick = 1 / 60.0;
        int tickCount = 0;
        boolean hasTicked = false;
        int totalTime = 0;

        while(isRunning) {
            long currentTime = System.nanoTime();
            long passedTime = currentTime - previousTime;
            previousTime = currentTime;
            nonProcessedSeconds += passedTime / 1000000000.0;

            while(nonProcessedSeconds > secondsPerTick) {
                tick();
                nonProcessedSeconds -= secondsPerTick;
                hasTicked = true;
                tickCount++;
                    if(tickCount % 60 == 0) {
                        previousTime += 1000;
                        FPS = frames;
                        frames = 0;
                    }
            }
            if(hasTicked) {
                frames++;
            }
            frames++;
            requestFocus();
            tick();
            render();

            try {
                Thread.sleep(5);
            } catch(Exception e){ }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用Toolkit.getScreenSize()方法获取屏幕尺寸。

请参阅How can I get screen resolution in java?