headius (owner)

Revisions

gist: 207182 Download_button fork
public
Public Clone URL: git://gist.github.com/207182.git
Embed All Files: show embed
Java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  /**
* Creates a new PGraphics object and sets it to the specified size.
*
* Note that you cannot change the renderer once outside of setup().
* In most cases, you can call size() to give it a new size,
* but you need to always ask for the same renderer, otherwise
* you're gonna run into trouble.
*
* The size() method should *only* be called from inside the setup() or
* draw() methods, so that it is properly run on the main animation thread.
* To change the size of a PApplet externally, use setSize(), which will
* update the component size, and queue a resize of the renderer as well.
*/
  public void size(final int iwidth, final int iheight,
                   String irenderer, String ipath) {
    // Run this from the EDT, just cuz it's AWT stuff (or maybe later Swing)
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        // Set the preferred size so that the layout managers can handle it
        setPreferredSize(new Dimension(iwidth, iheight));
        setSize(iwidth, iheight);
      }
    });
 
    // ensure that this is an absolute path
    if (ipath != null) ipath = savePath(ipath);
 
    String currentRenderer = g.getClass().getName();
    if (currentRenderer.equals(irenderer)) {
      // Avoid infinite loop of throwing exception to reset renderer
      resizeRenderer(iwidth, iheight);
      //redraw(); // will only be called insize draw()
 
    } else { // renderer is being changed
      // otherwise ok to fall through and create renderer below
      // the renderer is changing, so need to create a new object
      g = makeGraphics(iwidth, iheight, irenderer, ipath, true);
      width = iwidth;
      height = iheight;
 
      // fire resize event to make sure the applet is the proper size
// setSize(iwidth, iheight);
      // this is the function that will run if the user does their own
      // size() command inside setup, so set defaultSize to false.
      defaultSize = false;
 
      // throw an exception so that setup() is called again
      // but with a properly sized render
      // this is for opengl, which needs a valid, properly sized
      // display before calling anything inside setup().
      throw new RendererChangeException();
    }
  }