added new drawing method for easily changing the screen size

This commit is contained in:
2025-12-08 06:10:28 +01:00
parent 40111da691
commit f4b14214bd

View File

@@ -10,6 +10,7 @@ import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.logging.Level; import java.util.logging.Level;
@@ -19,12 +20,18 @@ public class GamePanel extends JPanel implements Runnable {
// SCREEN SETTINGS // SCREEN SETTINGS
final int originalTileSize = 16; //16x16 tile final int originalTileSize = 16; //16x16 tile
final int scale = 3; final int scale = 3;
public final int maxScreenCol = 16;
public final int tileSize = originalTileSize * scale; //48x48 tile public final int tileSize = originalTileSize * scale; //48x48 tile
public final int maxScreenCol = 20;
public final int maxScreenRow = 12; public final int maxScreenRow = 12;
public final int screenWidth = tileSize * maxScreenCol; // 768 pixels public final int screenWidth = tileSize * maxScreenCol; // 960 pixels
public final int screenHeight = tileSize * maxScreenRow; // 576 pixels public final int screenHeight = tileSize * maxScreenRow; // 576 pixels
// FULLSCREEN SETTINGS
int fScreenWidth = screenWidth;
int fScreenHeight = screenHeight;
BufferedImage tempScreen;
Graphics2D fg2;
// WORLD SETTINGS // WORLD SETTINGS
public final int maxWorldCol = 50; public final int maxWorldCol = 50;
public final int maxWorldRow = 50; public final int maxWorldRow = 50;
@@ -88,7 +95,9 @@ public class GamePanel extends JPanel implements Runnable {
if(delta >= 1) { if(delta >= 1) {
update(); update();
repaint(); //repaint(); //since the new drawing method
drawTempScreen();
drawScreen();
delta--; delta--;
drawCount++; drawCount++;
} }
@@ -136,23 +145,20 @@ public class GamePanel extends JPanel implements Runnable {
break; break;
} }
} }
public void paintComponent(Graphics graphics) { public void drawTempScreen() {
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
// DEBUG // DEBUG
long drawStart; long drawStart;
drawStart = System.nanoTime(); drawStart = System.nanoTime();
// TITLE SCREEN // TITLE SCREEN
if(gameState == GameState.TITLE) { if(gameState == GameState.TITLE) {
ui.draw(graphics2d); ui.draw(fg2);
return; return;
} }
// GAME // GAME
tileM.draw(graphics2d); tileM.draw(fg2);
for(Entity entity : iTile) if(entity != null) entity.draw(graphics2d); for(Entity entity : iTile) if(entity != null) entity.draw(fg2);
// ENTITY RENDER SYSTEM // ENTITY RENDER SYSTEM
entityList.add(player); entityList.add(player);
@@ -162,10 +168,10 @@ public class GamePanel extends JPanel implements Runnable {
for(Entity entity : projectileList) if(entity != null) entityList.add(entity); for(Entity entity : projectileList) if(entity != null) entityList.add(entity);
for(Entity entity : particleList) if(entity != null) entityList.add(entity); for(Entity entity : particleList) if(entity != null) entityList.add(entity);
entityList.sort(Comparator.comparingInt(o -> o.worldY)); entityList.sort(Comparator.comparingInt(o -> o.worldY));
for(Entity entity : entityList) entity.draw(graphics2d); for(Entity entity : entityList) entity.draw(fg2);
entityList.clear(); entityList.clear();
ui.draw(graphics2d); ui.draw(fg2);
long drawEnd = System.nanoTime(); long drawEnd = System.nanoTime();
long passed = drawEnd - drawStart; long passed = drawEnd - drawStart;
@@ -173,17 +179,19 @@ public class GamePanel extends JPanel implements Runnable {
// DEBUG // DEBUG
if(keyH.debug) { if(keyH.debug) {
int start = 350; int start = 350;
graphics2d.setColor(Color.white); fg2.setColor(Color.white);
graphics2d.drawString("Draw Time: " + passed, 10, start); fg2.drawString("Draw Time: " + passed, 10, start);
graphics2d.drawString("FPS: " + fpsMeasure, 10, start+tileSize); fg2.drawString("FPS: " + fpsMeasure, 10, start+tileSize);
graphics2d.drawString("Invincible: " + player.invincibleCount, 10, start+tileSize*2); fg2.drawString("Invincible: " + player.invincibleCount, 10, start+tileSize*2);
graphics2d.drawString("X, Y: " + player.worldX+", "+player.worldY, 10, start+tileSize*3); fg2.drawString("X, Y: " + player.worldX+", "+player.worldY, 10, start+tileSize*3);
graphics2d.drawString("Col, Row: " + (player.worldX+player.solidArea.x)/tileSize+", "+(player.worldY+player.solidArea.y)/tileSize, 10, start+tileSize*4); fg2.drawString("Col, Row: " + (player.worldX+player.solidArea.x)/tileSize+", "+(player.worldY+player.solidArea.y)/tileSize, 10, start+tileSize*4);
Boot.logger.log(Level.FINE, "Draw Time: " + passed); Boot.logger.log(Level.FINE, "Draw Time: " + passed);
} }
}
// END public void drawScreen() {
graphics.dispose(); Graphics fg = getGraphics();
fg.drawImage(tempScreen, 0, 0, fScreenWidth, fScreenHeight, null);
fg.dispose();
} }
// MUSIC // MUSIC
@@ -211,6 +219,9 @@ public class GamePanel extends JPanel implements Runnable {
assetSetter.setMonster(); assetSetter.setMonster();
assetSetter.setITiles(); assetSetter.setITiles();
gameState = GameState.TITLE; gameState = GameState.TITLE;
tempScreen = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
fg2 = (Graphics2D) tempScreen.getGraphics();
} }
} }