java - Screen Size auto adjust -
so in process of making game, , want make easy screen sizes can use it. don't know how this, have set default resolution 1600 x 900. if guys know how can make adjustable sizes appreciate :)
i have through code don't know have make work, if need more information on problem 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 = 0; < 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 = 0; < 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){ } } } }
you can screen size toolkit.getscreensize()
method.