From b0a5f882d641f813f0407df52a0cdecc8e9e732a Mon Sep 17 00:00:00 2001 From: Nico Nico Nii Date: Sun, 22 May 2022 21:55:33 -0500 Subject: [PATCH] Screen panel follows resize Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> --- scripts/jar.sh | 4 +-- scripts/md5 | 10 +++--- src/sig/RabiClone.java | 9 ++--- src/sig/engine/Panel.java | 69 +++++++++++++++++++++++++++++---------- utils/md5 | 10 +++--- 5 files changed, 65 insertions(+), 37 deletions(-) diff --git a/scripts/jar.sh b/scripts/jar.sh index 441b43b..ed70855 100755 --- a/scripts/jar.sh +++ b/scripts/jar.sh @@ -10,7 +10,7 @@ ORIGINAL_LOC=$(pwd) cd ${OUT_DIR} jar cfm ${PROJECT_NAME}.jar ${ORIGINAL_LOC}/manifest sig printf "\n\n\nRunning Program...\n\n" -java -jar ${PROJECT_NAME}.jar +java -jar ${PROJECT_NAME}.jar "$@" mv ${PROJECT_NAME}.jar ${ORIGINAL_LOC} cd .. -./scripts/clean.sh \ No newline at end of file +./scripts/clean.sh diff --git a/scripts/md5 b/scripts/md5 index c986421..dd142ed 100644 --- a/scripts/md5 +++ b/scripts/md5 @@ -1,5 +1,5 @@ -build.sh:de32fdf9f3ce9eb5d4ea9902ee48dbe0 *- -clean.sh:8aec15e447257f1c3a4e978f1b56a78e *- -commit.sh:21af1fa6f09d01679c9e11408967264a *- -jar.sh:2ac636f584c43a1124affb9ea6bdc7bf *- -lean.sh:3be7b8b182ccd96e48989b4e57311193 *- +build.sh:a833e7598ad65672a9c01306d244b49f - +clean.sh:96ce35f2d2dcb555421e00a6afda23ca - +commit.sh:21af1fa6f09d01679c9e11408967264a - +jar.sh:62ce62bbb97d3f3856bcc9b0ad34083c - +lean.sh:3be7b8b182ccd96e48989b4e57311193 - diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index 1077d47..56d5718 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -12,20 +12,15 @@ public class RabiClone { public static void main(String[] args) { JFrame f = new JFrame(PROGRAM_NAME); Panel p = new Panel(f); + p.init(); f.add(p); + f.addComponentListener(p); f.setSize(1280,720); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); p.render(); - BufferedImage nana; - try { - nana = ImageIO.read(new File("..","3x.png")); - p.Draw_Nana(p.Get_Nana(nana)); - } catch (IOException e) { - e.printStackTrace(); - } } } diff --git a/src/sig/engine/Panel.java b/src/sig/engine/Panel.java index ac76f75..520f71a 100644 --- a/src/sig/engine/Panel.java +++ b/src/sig/engine/Panel.java @@ -11,6 +11,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.awt.image.BufferedImage; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; import javax.imageio.ImageIO; import javax.swing.JFrame; @@ -18,11 +20,9 @@ import javax.swing.JPanel; import sig.RabiClone; -public class Panel extends JPanel implements Runnable { +public class Panel extends JPanel implements Runnable,ComponentListener { JFrame window; public int pixel[]; - public int width=1280; - public int height=720; final int CIRCLE_PRECISION=32; final int OUTLINE_COL=Color.BRIGHT_WHITE.getColor(); private Thread thread; @@ -37,11 +37,20 @@ public class Panel extends JPanel implements Runnable { long lastSecond=0; int lastFrameCount=0; Sprite nana_sprite; + boolean resizing=false; public Panel(JFrame f) { super(true); this.window=f; thread = new Thread(this, "MyPanel Thread"); + + BufferedImage nana; + try { + nana = ImageIO.read(new File("..","3x.png")); + nana_sprite = Get_Nana(nana); + } catch (IOException e) { + e.printStackTrace(); + } } /** @@ -59,22 +68,15 @@ public class Panel extends JPanel implements Runnable { * Call it after been visible and after resizes. */ public void init(){ - BufferedImage nana; - try { - nana = ImageIO.read(new File("..","3x.png")); - nana_sprite = Get_Nana(nana); - } catch (IOException e) { - e.printStackTrace(); - } cm = getCompatibleColorModel(); - int screenSize = width * height; + int screenSize = getWidth() * getHeight(); if(pixel == null || pixel.length < screenSize){ pixel = new int[screenSize]; } if(thread.isInterrupted() || !thread.isAlive()){ thread.start(); } - mImageProducer = new MemoryImageSource(width, height, cm, pixel,0, width); + mImageProducer = new MemoryImageSource(getWidth(), getHeight(), cm, pixel,0, getWidth()); mImageProducer.setAnimated(true); mImageProducer.setFullBufferUpdates(true); imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer); @@ -114,12 +116,19 @@ public class Panel extends JPanel implements Runnable { public /* abstract */ void render(){ + if (resizing) { + pixel = new int[getWidth()*getHeight()]; + resizing=false; + mImageProducer = new MemoryImageSource(getWidth(), getHeight(), cm, pixel,0, getWidth()); + imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer); + System.out.println("Window resized."); + } int[] p = pixel; // this avoid crash when resizing //a=h/w - for (int y=0;y=0) { Draw(p,index,col.getColor()); } @@ -306,7 +315,7 @@ public class Panel extends JPanel implements Runnable { int[] p = pixel; for(int x=0;x