Last active
April 10, 2020 21:01
-
-
Save bytecodeman/0a648bae5aace486b9943208b0106276 to your computer and use it in GitHub Desktop.
CSC-112 Ch15 Control Circl eWith Buttons, Mouse, and Keys (Shows consume method)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Antonio C. Silvestri | |
* Ch15 Control Circle with Buttons, Mouse, and Keys (Demos consume method) | |
* CSC-112 Intermediate Java | |
* 4/9/2020 | |
*/ | |
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.input.KeyCode; | |
import javafx.scene.input.MouseButton; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.input.KeyEvent; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.shape.Circle; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
public class ControlCircleWithMouseAndKey extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
AppGUI ag =new AppGUI(); | |
Scene scene = new Scene(ag, 200, 150); | |
ag.requestFocus(); | |
primaryStage.setTitle("Control Circle With Buttons, Mouse, and Keys"); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} | |
//************************************************************** | |
class AppGUI extends BorderPane { | |
private CirclePane cp = new CirclePane(); | |
public AppGUI() { | |
ControlButtonsPane cbp = new ControlButtonsPane(cp); | |
this.setCenter(cp); | |
this.setBottom(cbp); | |
} | |
public void requestFocus() { | |
this.cp.requestFocus(); | |
} | |
} | |
//************************************************************** | |
class CirclePane extends StackPane { | |
private int radius; | |
private Circle circle = new Circle(); | |
public CirclePane() { | |
this.radius = 50; | |
this.setOnKeyPressed((KeyEvent e) -> { | |
KeyCode kc = e.getCode(); | |
if (kc == KeyCode.UP || kc == KeyCode.RIGHT) { | |
this.enlarge(); | |
e.consume(); | |
} else if (kc == KeyCode.DOWN || kc == KeyCode.LEFT) { | |
this.shrink(); | |
e.consume(); | |
} | |
}); | |
this.setOnMouseClicked((MouseEvent e) -> { | |
if (e.getButton() == MouseButton.PRIMARY) { | |
this.enlarge(); | |
} else if (e.getButton() == MouseButton.SECONDARY) { | |
this.shrink(); | |
} | |
this.requestFocus(); | |
}); | |
paintCircle(); | |
} | |
private void paintCircle() { | |
this.getChildren().clear(); | |
circle = new Circle(this.radius); | |
circle.setStroke(Color.BLACK); | |
circle.setFill(Color.WHITE); | |
this.getChildren().add(circle); | |
} | |
public void enlarge() { | |
this.radius += 2; | |
paintCircle(); | |
} | |
public void shrink() { | |
this.radius -= 2; | |
if (this.radius < 0) | |
this.radius = 0; | |
paintCircle(); | |
} | |
} | |
//************************************************************** | |
class ControlButtonsPane extends HBox { | |
public ControlButtonsPane(CirclePane cp) { | |
this.setSpacing(10); | |
this.setAlignment(Pos.CENTER); | |
Button btEnlarge = new Button("Enlarge"); | |
Button btShrink = new Button("Shrink"); | |
this.getChildren().add(btEnlarge); | |
this.getChildren().add(btShrink); | |
// Create and register the handler | |
btEnlarge.setOnAction((ActionEvent e) -> { | |
cp.enlarge(); | |
cp.requestFocus(); | |
}); | |
btShrink.setOnAction(e -> { | |
cp.shrink(); | |
cp.requestFocus(); | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment