Sketch with a GView control
This control encapsulates a 2D or 3D canvas that is embedded in the main sketch window. The view control responds to standard Processing mouse events almost as if it was a separate sketch.
This video shows a Processing sketch with 2 view controls embedded in the main sketch display. It will give you a flavour of what cn be done with this control but to find out more you need to read these guides.
The sketch shown in this video is also included as an example in the library download / installation.
How do I do that?
Although this sketch will be included in the library examples, these guides describes in detail how it is implemented making it easier for you to use this control in your own sketches.
In this sketch there are two GView controls, on the left is a 2D view using the JAVA2D renderer and on the right a 3D view using the P3D renderer. Note...
- JAVA2D or P2D can be used for a a 2D view..
- Using P2D and/or P3D views requires the main sketch to use either P2D or P3D.
- If the main sketch uses the default JAVA2D renderer, then only JAVA2D views can be created..
In this sketch the main display uses P2D and has a number of balls floating accross its surface.
Creating the two views is simple enough, we start with a couple of global variables for the two GView controls.
GView view2D, view3D;
Then in setup we create the actual controls.
void setup() {
surface.setTitle("G4P view control (pre-release");
cursor(CROSS);
// Setup 2D view and viewer
view2D = new GView(this, 30, 24, 260, 200, JAVA2D);
// Setup 3D view and viewer
view3D = new GView(this, 340, 24, 200, 200, P3D);
}
We are using the default GCtrlMode.CORNER mode (see Creating GUI
controls) so the first parameter value (this
) is the main sketch window, the next four
numbers are the x/y position of top left corner, the width and the height of the view. The final parameter is
the renderer to use.
If you were to run the sketch now you would see 2 black rectangles where the views are. So far so good the next guide shows how to make them respond to mouse events and draw something.