working on embedded Canvas

This commit is contained in:
Ben Fry
2014-11-16 15:27:30 -07:00
parent 0b5e825d67
commit 58cc7e6e2f
6 changed files with 94 additions and 33 deletions
+32 -20
View File
@@ -77,20 +77,18 @@ public class ColorChooser { //extends JFrame implements DocumentListener {
box.setBorder(new EmptyBorder(12, 12, 12, 12));
range = new ColorRange();
range.init();
Box rangeBox = new Box(BoxLayout.Y_AXIS);
rangeBox.setAlignmentY(0);
rangeBox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
rangeBox.add(range);
rangeBox.add(range.getCanvas());
box.add(rangeBox);
box.add(Box.createHorizontalStrut(10));
slider = new ColorSlider();
slider.init();
Box sliderBox = new Box(BoxLayout.Y_AXIS);
sliderBox.setAlignmentY(0);
sliderBox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
sliderBox.add(slider);
sliderBox.add(slider.getCanvas());
box.add(sliderBox);
box.add(Box.createHorizontalStrut(10));
@@ -108,6 +106,10 @@ public class ColorChooser { //extends JFrame implements DocumentListener {
window.pack();
window.setResizable(false);
// initialize once the components exist
range.init();
slider.init();
// Dimension size = getSize();
// Dimension screen = Toolkit.getScreenSize();
@@ -492,8 +494,15 @@ public class ColorChooser { //extends JFrame implements DocumentListener {
int lastX, lastY;
public int sketchWidth() {
return WIDE;
}
public int sketchHeight() {
return HIGH;
}
public void setup() {
size(WIDE, HIGH); //, P3D);
noLoop();
colorMode(HSB, 360, 256, 256);
@@ -504,22 +513,18 @@ public class ColorChooser { //extends JFrame implements DocumentListener {
}
public void draw() {
// if ((g == null) || (g.pixels == null)) return;
if ((width != WIDE) || (height < HIGH)) {
//System.out.println("bad size " + width + " " + height);
return;
}
int index = 0;
for (int j = 0; j < 256; j++) {
for (int i = 0; i < 256; i++) {
pixels[index++] = color(hue, i, 255 - j);
if (width == WIDE && height == HIGH) {
int index = 0;
for (int j = 0; j < 256; j++) {
for (int i = 0; i < 256; i++) {
pixels[index++] = color(hue, i, 255 - j);
}
}
}
updatePixels();
stroke((brightness > 50) ? 0 : 255);
rect(lastX, lastY, 9, 9);
updatePixels();
stroke((brightness > 50) ? 0 : 255);
rect(lastX, lastY, 9, 9);
}
}
public void mousePressed() {
@@ -571,8 +576,15 @@ public class ColorChooser { //extends JFrame implements DocumentListener {
static final int WIDE = 20;
static final int HIGH = 256;
public int sketchWidth() {
return WIDE;
}
public int sketchHeight() {
return HIGH;
}
public void setup() {
size(WIDE, HIGH); //, P3D);
colorMode(HSB, 255, 100, 100);
noLoop();
loadPixels();
+20
View File
@@ -25,6 +25,7 @@
package processing.core;
// used for setting bg colors and whatnot
import java.awt.Canvas;
import java.awt.Color;
// use for the link() command (and maybe open()?)
import java.awt.Desktop;
@@ -9385,6 +9386,8 @@ public class PApplet implements PConstants {
// enable it. Conversely, if the command line does not, don't disable it.
// Query the applet to see if it wants to be full screen all the time.
fullScreen |= applet.sketchFullScreen();
// If spanning screens, that means we're also full screen.
fullScreen |= applet.sketchSpanScreens();
// pass everything after the class name in as args to the sketch itself
// (fixed for 2.0a5, this was just subsetting by 1, which didn't skip opts)
applet.args = PApplet.subset(args, argIndex + 1);
@@ -9480,6 +9483,23 @@ public class PApplet implements PConstants {
}
/**
* Return a Canvas object that can be embedded into other Java GUIs.
* This is necessary because PApplet no longer subclasses Component.
*
* <pre>
* PApplet sketch = new EmbedSketch();
* Canvas canvas = sketch.getCanvas();
* // add the canvas object to your project and validate() it
* sketch.init() // start the animation thread
*/
public Canvas getCanvas() {
g = makePrimaryGraphics();
surface = g.createSurface();
return surface.initCanvas(this);
}
/** Convenience method, should only be called by PSurface subclasses. */
static public void hideMenuBar() {
if (PApplet.platform == PConstants.MACOSX) {
+3
View File
@@ -22,6 +22,7 @@
package processing.core;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
@@ -45,6 +46,8 @@ public interface PSurface {
// renderer that doesn't draw to the screen
public void initOffscreen();
public Canvas initCanvas(PApplet sketch);
public Frame initFrame(PApplet sketch, Color backgroundColor,
int deviceIndex, boolean fullScreen, boolean spanDisplays);
+35 -9
View File
@@ -39,6 +39,9 @@ import processing.event.MouseEvent;
public class PSurfaceAWT implements PSurface {
GraphicsDevice displayDevice;
// used for canvas to determine whether resizable or not
boolean resizable; // default is false
// Internally, we know it's always a JFrame (not just a Frame)
JFrame frame;
@@ -85,15 +88,7 @@ public class PSurfaceAWT implements PSurface {
void createCanvas() {
canvas = new SmoothCanvas();
//canvas.setIgnoreRepaint(true); // ??
}
Canvas getCanvas() {
return canvas;
}
void initCanvas() {
// send tab keys through to the PApplet
canvas.setFocusTraversalKeysEnabled(false);
@@ -145,6 +140,24 @@ public class PSurfaceAWT implements PSurface {
private Dimension newSize = new Dimension(0, 0);
@Override
public Dimension getPreferredSize() {
return new Dimension(sketchWidth, sketchHeight);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return resizable ? super.getMaximumSize() : getPreferredSize();
}
@Override
public void validate() {
super.validate();
@@ -394,6 +407,15 @@ public class PSurfaceAWT implements PSurface {
}
*/
public Canvas initCanvas(PApplet sketch) {
this.sketch = sketch;
sketchWidth = sketch.sketchWidth();
sketchHeight = sketch.sketchHeight();
return canvas;
}
public Frame initFrame(PApplet sketch, Color backgroundColor,
int deviceIndex, boolean fullScreen, boolean spanDisplays) {
@@ -532,7 +554,11 @@ public class PSurfaceAWT implements PSurface {
/** Set true if we want to resize things (default is not resizable) */
public void setResizable(boolean resizable) {
frame.setResizable(resizable);
this.resizable = resizable; // really only used for canvas
if (frame != null) {
frame.setResizable(resizable);
}
}
@@ -309,7 +309,7 @@ public class PGraphics2D2X extends PGraphicsOpenGL {
} else if (type == PShape.GEOMETRY) {
shape = new PShapeOpenGL(pg, PShape.GEOMETRY);
}
shape.is3D(false);
shape.set3D(false);
return shape;
}
@@ -380,7 +380,7 @@ public class PGraphics2D2X extends PGraphicsOpenGL {
shape.setParams(p);
}
shape.is3D(false);
shape.set3D(false);
return shape;
}
@@ -192,7 +192,7 @@ public class PGraphics3D2X extends PGraphicsOpenGL {
} else if (type == PShape.GEOMETRY) {
shape = new PShapeOpenGL(pg, PShape.GEOMETRY);
}
shape.is3D(true);
shape.set3D(true);
return shape;
}
@@ -273,7 +273,7 @@ public class PGraphics3D2X extends PGraphicsOpenGL {
shape.setParams(p);
}
shape.is3D(true);
shape.set3D(true);
return shape;
}
}