Add mouseDragged handler and add code for freehand drawing and erasing

This commit is contained in:
jan 2017-01-08 17:27:15 -08:00
parent 99388a2786
commit 9658e99e39
5 changed files with 105 additions and 4 deletions

View File

@ -17,8 +17,7 @@ I (Jan) haven't tested the below commands on windows, but they should
* Multiple windows (so that moving the control window around doesn't move what's * Multiple windows (so that moving the control window around doesn't move what's
on the projector. on the projector.
* Drawing using primitive shapes (lines, ellipses) and arbitrary colors. * Drawing using primitive shapes (lines, ellipses) and arbitrary colors.
* Does NOT currently support freeform drawing... should be pretty easy to add though. * Freehand drawing and erasing
## Compiling ## Compiling

47
src/DragGetter.java Normal file
View File

@ -0,0 +1,47 @@
/*
Interface for handling mouse drags
*/
public interface DragGetter {
boolean drag(int x1, int y1, int x2, int y2);
}
//public interface Shape {
// public int numVertices();
//}
//
//
//class Circle2 implements Shape {
// Circle2() {
// blah;
// }
//
// int numVertices() {
// return 0;
// }
//
// void expand() {
// blah2;
// }
//}
//
//class Square implements Shape {
// int numVertices() {
// return 4;
// }
//}
//
//
//Shape g = new Circle2();
////g.expand(); // bad
//println(g.numVertices()); //good
//g = new Square();
//println(g.numVertices());
//
//void printNumberOfVertices(Shape s) {
// println(s.numVertices());
//}
//
//printNumberOfVertices(new Circle2());
//printNumberOfVertices(new Square());

View File

@ -22,6 +22,7 @@ public class EuglenaApplet extends PApplet {
Menu menu; Menu menu;
Calibrator calibrator; Calibrator calibrator;
ClickGetter clickGetter; ClickGetter clickGetter;
DragGetter dragGetter;
//Pen //Pen
int penColor = color(200); int penColor = color(200);
@ -92,6 +93,22 @@ public class EuglenaApplet extends PApplet {
} }
} }
@Override
public void mouseDragged() {
// Ignore mouse drags from menu area
if (pmouseX > width - menu.width) {
return;
}
// If we have a clickGetter, use that to handle the mouse click
if (dragGetter != null) {
boolean done = dragGetter.drag(pmouseX, pmouseY, mouseX, mouseY);
if (done) {
dragGetter = null;
}
}
}
@Override @Override
public void keyPressed() { public void keyPressed() {
/* /*

27
src/Freehand.java Normal file
View File

@ -0,0 +1,27 @@
class Freehand {
/*
Class for freehand drawing
*/
int color, brushSize;
Freehand(int color, int penWidth) {
this.color = color;
this.brushSize = penWidth;
}
DragGetter makeDragGetter(ProjectorApplet p) {
/*
Create a DragGetter which can accept incoming mouse drags
and use them to draw lines
*/
return (int x1, int y1, int x2, int y2) -> {
Line line = new Line(x1, y1, x2, y2, this.color, this.brushSize);
p.commandQueue.add(line.makeProjectorCommand());
// Keep using this DragGetter, don't stop
return false;
};
}
}

View File

@ -200,6 +200,7 @@ class Menu {
//println(theEvent); //println(theEvent);
if (theEvent.isFrom(shapesList)) { if (theEvent.isFrom(shapesList)) {
parent.dragGetter = null;
switch((int) shapesList.getValue()) { switch((int) shapesList.getValue()) {
case 0: case 0:
parent.clickGetter = new Ellipse(parent.penColor, parent.penWidth) parent.clickGetter = new Ellipse(parent.penColor, parent.penWidth)
@ -215,6 +216,7 @@ class Menu {
break; break;
} }
} else if (theEvent.isFrom(animateList)){ } else if (theEvent.isFrom(animateList)){
parent.dragGetter = null;
switch((int) animateList.getValue()) { switch((int) animateList.getValue()) {
case 0: case 0:
parent.clickGetter = new Shrink(parent.operationSpeed) parent.clickGetter = new Shrink(parent.operationSpeed)
@ -275,11 +277,20 @@ class Menu {
parent.projectorApplet.calibrator.toggle(); parent.projectorApplet.calibrator.toggle();
break; break;
case "Freehand":
parent.clickGetter = null;
parent.dragGetter = new Freehand(parent.penColor, parent.penWidth)
.makeDragGetter(parent.projectorApplet);
break;
case "Eraser": case "Eraser":
parent.penColor = parent.color(0, 0, 0); parent.clickGetter = null;
parent.dragGetter = new Freehand(parent.projectorApplet.bgColor, parent.penWidth)
.makeDragGetter(parent.projectorApplet);
break; break;
case "Line": case "Line":
parent.dragGetter = null;
parent.clickGetter = new Line(parent.penColor, parent.penWidth) parent.clickGetter = new Line(parent.penColor, parent.penWidth)
.makeClickGetter(parent.projectorApplet); .makeClickGetter(parent.projectorApplet);
break; break;