0

so currently I am in the process of making a game, and I want to make it easy so that all screen sizes can use it. But I don't know how to do this, currently I just have it set so the default resolution is 1600 x 900. If you guys know how I can make this adjustable for all sizes I would appreciate the help :)

I have look through all my code but I don't know what I have to do to make this work, if you need more information on my problem just ask.

Many thanks,

Ash

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){ }
        }
    }
}
Ash Davies
  • 21
  • 2
  • 1
    Please remember that we are volunteers, and you are asking for free help from volunteers. In that light it is up to you to make it as easy as possible for folks to help you, including posting all pertinent code here with your question as code-formatted text, not as a link. If you have too much code to post here, then you have too much code to ask a volunteer to go through. Please read [mcve], as well as the [help] section, especially the subsection on how to ask a good question. – Hovercraft Full Of Eels Jun 01 '16 at 22:42

1 Answers1

0

You can get the screen size with the Toolkit.getScreenSize() method.

See How can I get screen resolution in java?

Community
  • 1
  • 1
GreenGiant
  • 4,930
  • 1
  • 46
  • 76