From daaad10c43fa62902f653d9b90f729875321ecc7 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 16 Nov 2014 19:32:44 -0700 Subject: [PATCH] hammering through placement and sizing issues --- core/README.md | 1 + core/src/processing/core/PApplet.java | 18 ++++++-- core/src/processing/core/PGraphics.java | 2 +- core/src/processing/core/PSurfaceAWT.java | 50 +++++++++++++++++------ 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/core/README.md b/core/README.md index b23e6f767..70f30dcbc 100644 --- a/core/README.md +++ b/core/README.md @@ -58,6 +58,7 @@ another PSurfaceAWT variant could allow direct rendering to the canvas (no loadP inside main, will know the screen that's being used for the app #### Questions/To Do +- change size() command to check through renderer constants and give better error message when using one of the built-in renderers - bad idea, or worst idea, to have 'surface' var in PGraphics? - move getFontRenderContext(font) to PApplet? surface? elsewhere? _ do we need canDraw() anymore? diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 51a974ec6..06aa33d91 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -753,6 +753,7 @@ public class PApplet implements PConstants { // screenWidth = screen.width; // screenHeight = screen.height; + defaultSize = true; finished = false; // just for clarity // this will be cleared by draw() if it is not overridden @@ -1480,7 +1481,7 @@ public class PApplet implements PConstants { if (!renderer.equals(sketchRenderer())) { System.err.println("Because you're not running from the PDE, add this to your code:"); System.err.println("public String sketchRenderer() {"); - System.err.println(" return " + renderer + ";"); + System.err.println(" return \"" + renderer + "\";"); System.err.println("}"); throw new RuntimeException("The sketchRenderer() method is not implemented."); } @@ -1811,7 +1812,7 @@ public class PApplet implements PConstants { // // Give up, instead set the new renderer and re-attempt setup() // return; // } - this.defaultSize = false; + defaultSize = false; } else { // frameCount > 0, meaning an actual draw() // update the current frameRate @@ -9470,7 +9471,7 @@ public class PApplet implements PConstants { // and the empty draw() has set "finished" to true. // TODO make sure this won't hang if the applet has an exception. while (defaultSize && !finished) { - //System.out.println("default size"); +// System.out.println("default size"); try { Thread.sleep(5); @@ -9478,11 +9479,22 @@ public class PApplet implements PConstants { //System.out.println("interrupt"); } } + System.out.println("out of default size loop, " + width + " " + height); // convenience to avoid another 'get' from the static main() method return surface; } +// protected void createSurface() { +// surface = g.createSurface(); +// if (surface == null) { +// System.err.println("This renderer needs to be updated for Processing 3"); +// System.err.println("The createSurface() method returned null."); +// System.exit(1); +// } +// } + + /** * Return a Canvas object that can be embedded into other Java GUIs. * This is necessary because PApplet no longer subclasses Component. diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index 9fe2bca37..b2e85d0f7 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -770,7 +770,7 @@ public class PGraphics extends PImage implements PConstants { public PSurface createSurface() { // ignore - return null; + return new PSurfaceAWT(this); } diff --git a/core/src/processing/core/PSurfaceAWT.java b/core/src/processing/core/PSurfaceAWT.java index 1a7ba2a39..8a854b7cb 100644 --- a/core/src/processing/core/PSurfaceAWT.java +++ b/core/src/processing/core/PSurfaceAWT.java @@ -533,7 +533,7 @@ public class PSurfaceAWT implements PSurface { if (fullScreen) { frame.invalidate(); } else { - frame.pack(); +// frame.pack(); } // insufficient, places the 100x100 sketches offset strangely @@ -616,10 +616,7 @@ public class PSurfaceAWT implements PSurface { public void placeWindow(int[] location) { - calcFrameSize(sketchWidth, sketchHeight); - - int contentW = Math.max(sketchWidth, MIN_WINDOW_WIDTH); - int contentH = Math.max(sketchHeight, MIN_WINDOW_HEIGHT); + setFrameSize(); //sketchWidth, sketchHeight); if (location != null) { // a specific location was received from the Runner @@ -643,9 +640,7 @@ public class PSurfaceAWT implements PSurface { // ((JFrame) frame).getContentPane().setBackground(backgroundColor); // } - canvas.setBounds((contentW - sketchWidth)/2, - (contentH - sketchHeight)/2, - sketchWidth, sketchHeight); + setCanvasSize(); //sketchWidth, sketchHeight); frame.addWindowListener(new WindowAdapter() { @Override @@ -664,7 +659,20 @@ public class PSurfaceAWT implements PSurface { } - private Dimension calcFrameSize(int sketchWidth, int sketchHeight) { + private void setCanvasSize() { + int contentW = Math.max(sketchWidth, MIN_WINDOW_WIDTH); + int contentH = Math.max(sketchHeight, MIN_WINDOW_HEIGHT); + + canvas.setBounds((contentW - sketchWidth)/2, + (contentH - sketchHeight)/2, + sketchWidth, sketchHeight); + } + + + /** Resize frame for these sketch (canvas) dimensions. */ + private Dimension setFrameSize() { //int sketchWidth, int sketchHeight) { + System.out.format("setting frame size %d %d %n", sketchWidth, sketchHeight); + new Exception().printStackTrace(System.out); Insets insets = frame.getInsets(); int windowW = Math.max(sketchWidth, MIN_WINDOW_WIDTH) + insets.left + insets.right; @@ -684,7 +692,8 @@ public class PSurfaceAWT implements PSurface { public void placeWindow(int[] location, int[] editorLocation) { - Dimension window = calcFrameSize(sketchWidth, sketchHeight); + //Dimension window = setFrameSize(sketchWidth, sketchHeight); + Dimension window = setFrameSize(); //sketchWidth, sketchHeight); int contentW = Math.max(sketchWidth, MIN_WINDOW_WIDTH); int contentH = Math.max(sketchHeight, MIN_WINDOW_HEIGHT); @@ -806,6 +815,18 @@ public class PSurfaceAWT implements PSurface { // needs to resize the frame, which will resize the canvas, and so on... public void setSize(int wide, int high) { + System.out.format("frame visible %b, setSize(%d, %d) %n", frame.isVisible(), wide, high); + new Exception().printStackTrace(System.out); + + sketchWidth = wide; + sketchHeight = high; + +// canvas.setSize(wide, high); +// frame.setSize(wide, high); + setFrameSize(); //wide, high); + setCanvasSize(); + frame.setLocationRelativeTo(null); + GraphicsConfiguration gc = canvas.getGraphicsConfiguration(); // If not realized (off-screen, i.e the Color Selector Tool), gc will be null. if (gc == null) { @@ -820,8 +841,8 @@ public class PSurfaceAWT implements PSurface { graphics.image = gc.createCompatibleImage(wide * factor, high * factor); //throw new RuntimeException("implement me, see readme.md"); - sketchWidth = sketch.width = wide; - sketchHeight = sketch.height = high; + sketch.width = wide; + sketch.height = high; // sets internal variables for width/height/pixelWidth/pixelHeight graphics.setSize(wide, high); @@ -1499,4 +1520,9 @@ public class PSurfaceAWT implements PSurface { canvas.setCursor(invisibleCursor); cursorVisible = false; } + + + void debug(String format, Object ... args) { + System.out.format(format + "%n", args); + } } \ No newline at end of file