package ToolKit;
import javafx.animation.PathTransition;
import javafx.collections.ObservableList;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.util.Duration;
import java.util.ArrayList;
/**
*
* Chapter 15 Exercise 15
* Created by luizsa on 9/22/14.
*/
public class TBeanMachinePane extends Pane {
// Bean machine measurements
double w = 800; // Pane default width
double h = 800; // Pane default height
double gap; // distance gap between slot lines
double radius; // peg's radius
double dropX; // Drop ball starting x point
double dropY; // Drop ball starting y point //
int numOfSlots; // numbers of slots
// Line's sequence drawn matters
Line baseLine;
Line leftSLine; // left side line (bottom left)
Line rightSLine; // right side line (bottom right)
Line[] cSlotLines; // center slot lines
Line midLeftSLine; // middle left line
Line midRightSLine; // middle right line
Line topLeftSLine; // top left line
Line topRightSLine; // top right line
// Triangle pegs
Circle[] pegs; // PEEGGSSSSS
// Dropped balls
ArrayList<Circle> balls;
ArrayList<Circle> fineshedBalls;
ArrayList<Polyline> ballPaths;
// Animation
ArrayList<PathTransition> mPathTransitions = new ArrayList<>();
private TBeanMachinePane() {
baseLine = new Line();
baseLine.translateYProperty().bind(translateXProperty());
leftSLine = new Line();
rightSLine = new Line();
midLeftSLine = new Line();
midRightSLine = new Line();
topLeftSLine = new Line();
topRightSLine = new Line();
balls = new ArrayList<>();
fineshedBalls = new ArrayList<>();
ballPaths = new ArrayList<>();
}
public TBeanMachinePane(int slots, double width, double height) {
this();
initSlotLinesAndPegs(slots); // All layout nodes are initialized after this point
numOfSlots = slots;
setMinWidth(w = width);
setMinHeight(h = height);
setMaxSize(width, height);
drawLayout();
addLayoutShapes();
}
public void dropBall() {
Circle ball = new Circle(dropX, dropY, radius);
Polyline polyline = generatePath();
ObservableList<Double> list = polyline.getPoints();
double x = list.get(list.size() - 2);
double y = list.get(list.size() - 1);
fineshedBalls.add(new Circle(x, y, radius));
ballPaths.add(polyline);
balls.add(ball);
PathTransition path = new PathTransition(Duration.seconds(8), polyline, ball);
mPathTransitions.add(path);
path.setDelay(Duration.seconds(mPathTransitions.size()));
getChildren().addAll(ball);
//getChildren().addAll(polyline);
path.play();
}
private Polyline generatePath() {
// dropped ball's beginning x and y point
final int X = 0;
final int Y = 1;
double[] p = new double[]{dropX, dropY };
// the polyline will track the ball's path
Polyline polyline = new Polyline();
ObservableList<Double> list = polyline.getPoints();
// Adding start point to the polyline
list.addAll(p[X], p[Y]);
// While the ball hasn't reached the end
while (!isAtEnd(p[Y]) ) {
// while the ball hasn't hit a boundary: keep falling...
if (!hasBouncedOnPeg(p[X], p[Y]) && !hasBouncedOnBall(p[X], p[Y])) {
p[Y] = moveDown(p[Y]);
list.addAll(p[X], p[Y]);
}
// If the ball didn't bounce on another ball
else if (hasBouncedOnPeg(p[X], p[Y]) || hasBouncedOnBall(p[X], p[Y])){
// If both sides are clear than make a random turn
if (isClearLeft(p[X], p[Y]) && isClearRight(p[X], p[Y])) {
p = ((int) (Math.random() * 2) == 1) ? rightArc(list, p[X], p[Y]) : leftArc(list, p[X], p[Y]);
}
// else if left side is clear make a left turn
else if (isClearLeft(p[X], p[Y])) {
p = leftArc(list, p[X], p[Y]);
}
// else if right side is clear make a right turn
else if (isClearRight(p[X], p[Y])) {
p = rightArc(list, p[X], p[Y]);
} else {
// if statement reaches here its a dead end
break;
}
}
}
return polyline;
}
private boolean hasBouncedOnPeg(double x, double y) {
for (Circle c : pegs) {
if (c.contains(x, y + radius * 1.59999999)) {
//getChildren().add(new Circle(x, y + radius * 2, 1));
return true;
}
}
return false;
}
private double[] rightArc(ObservableList<Double> list, double startX, double startY) {
double angle = 90;
double x = 0;
double y = 0;
double arcRadius = radius * 2;
for (int i = 0; i < 90; i++) {
x = startX + arcRadius * Math.cos(Math.toRadians(angle));
y = (startY + arcRadius) - arcRadius * Math.sin(Math.toRadians(angle));
list.addAll(x,y);
angle--;
}
return new double[]{x,y};
}
private double[] leftArc(ObservableList<Double> list, double startX, double startY) {
double angle = 90;
double x = 0;
double y = 0;
double arcRadius = radius * 2;
for (int i = 0; i < 90; i++) {
x = startX + arcRadius * Math.cos(Math.toRadians(angle));
y = (startY + arcRadius) - arcRadius * Math.sin(Math.toRadians(angle));
list.addAll(x,y);
angle++;
}
return new double[]{x,y};
}
private boolean isClearLeft(double x, double y) {
double limitX = x - radius * 2;
if (limitX < baseLine.getStartX()) return false;
for (Circle peg : pegs) {
if (peg.contains(limitX, y) || !isInsideBeanMachine(limitX, y)) {
return false;
}
}
for (Circle c : balls) {
if (c.contains(limitX, y) || !isInsideBeanMachine(limitX, y)) {
return false;
}
}
return true;
}
private boolean isClearRight(double x, double y) {
double limitX = x + radius * 2;
if (limitX > baseLine.getEndX()) return false;
for (Circle peg : pegs) {
if (peg.contains(limitX, y) || !isInsideBeanMachine(limitX, y)) {
return false;
}
}
for (Circle c : balls) {
if (c.contains(limitX, y) || !isInsideBeanMachine(limitX, y)) {
return false;
}
}
return true;
}
private boolean isInsideBeanMachine(double x, double y) {
if (y < pegs[pegs.length - 1].getCenterY()) return true;
MyPoint p1 = new MyPoint(leftSLine.getEndX(), leftSLine.getEndY());
MyPoint p2 = new MyPoint(rightSLine.getEndX(), rightSLine.getEndY());
MyPoint p3 = new MyPoint(
pegs[pegs.length - 1].getCenterX(),
pegs[pegs.length - 1].getCenterY());
return (new Triangle2D(p1, p2, p3).contains(x, y));
}
private boolean isAtEnd(double y) {
return y >= baseLine.getEndY() - radius * 2;
}
private boolean hasBouncedOnBall(double x, double y) {
for (Circle c : fineshedBalls) {
if (c.contains(x, y + radius * 2)) {
return true;
}
}
return false;
}
private double moveDown(double y){
return y + radius;
}
private void drawLayout() {
// sequence matters...
drawBase();
drawLeftAndRightSide();
没有合适的资源?快使用搜索试试~ 我知道了~
Introduction to Java Programming(10th Edition)课后练习答案
共666个文件
java:663个
md:1个
gitignore:1个
5星 · 超过95%的资源 需积分: 34 115 下载量 174 浏览量
2017-09-30
08:51:50
上传
评论 11
收藏 620KB ZIP 举报
温馨提示
由 Luiz Arantes Sa编写,并不是随书配套的答案,可以作为参考
资源推荐
资源详情
资源评论
收起资源包目录
Introduction to Java Programming(10th Edition)课后练习答案 (666个子文件)
Exercise_26.class 1KB
.gitignore 110B
TBeanMachinePane.java 12KB
Exercise_31.java 11KB
Exercise_05.java 10KB
Exercise_07.java 10KB
PostfixNotation.java 10KB
Exercise_33.java 9KB
Exercise_30.java 9KB
Exercise_32.java 8KB
Exercise_09.java 8KB
Exercise_26.java 8KB
ClockPane.java 7KB
Kit.java 7KB
MyRectangle2D.java 7KB
Exercise_10.java 7KB
Exercise_20.java 7KB
Exercise_09.java 7KB
MyLinkedList.java 7KB
Exercise_09.java 6KB
Exercise_17.java 6KB
Exercise_08.java 6KB
Exercise_13.java 6KB
Exercise_11.java 6KB
Exercise_37.java 6KB
Exercise_36.java 6KB
Exercise_35.java 6KB
JavaStringRemover.java 6KB
Exercise_25.java 6KB
Triangle2D.java 6KB
Exercise_27.java 6KB
primeArrayClass.java 6KB
Exercise_24.java 6KB
Exercise_19.java 6KB
Exercise_33.java 6KB
Exercise_29.java 6KB
SortLargeFile.java 6KB
Exercise_15.java 6KB
MultipleBounceBall.java 6KB
Exercise_03.java 5KB
BigRational.java 5KB
Exercise_23.java 5KB
Exercise_09.java 5KB
Exercise_27.java 5KB
MyString1.java 5KB
Exercise_19.java 5KB
Exercise_23.java 5KB
Exercise_28.java 5KB
Exercise_34.java 5KB
Exercise_33.java 5KB
Exercise_22.java 5KB
Exercise_18.java 5KB
Exercise_34.java 5KB
Exercise_29.java 5KB
Exercise_24.java 5KB
Exercise_17.java 5KB
Exercise_02.java 4KB
BeanMachinePane.java 4KB
Exercise_25.java 4KB
Exercise_19.java 4KB
MyArrayList.java 4KB
Exercise_14.java 4KB
Exercise_25.java 4KB
Exercise_34.java 4KB
Rational.java 4KB
Exercise_35.java 4KB
Exercise_13.java 4KB
Exercise_21.java 4KB
Exercise_31.java 4KB
Exercise_02.java 4KB
Exercise_29.java 4KB
Exercise_39.java 4KB
Exercise_37.java 4KB
Exercise_21.java 4KB
Exercise_14.java 4KB
Exercise_17.java 4KB
Exercise_19.java 4KB
Exercise_28.java 4KB
Exercise_05.java 4KB
Exercise_19.java 3KB
Exercise_13.java 3KB
Exercise_16.java 3KB
Exercise_22.java 3KB
Exercise_01.java 3KB
Exercise_07.java 3KB
Exercise_20.java 3KB
Exercise_24.java 3KB
Exercise_08.java 3KB
Exercise_03.java 3KB
Exercise_18.java 3KB
Exercise_11.java 3KB
Exercise_04.java 3KB
Exercise_36.java 3KB
MyPoint.java 3KB
Exercise_14.java 3KB
PaneCollection.java 3KB
Exercise_01.java 3KB
Exercise_18.java 3KB
Exercise_25.java 3KB
Exercise_17.java 3KB
共 666 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- 涛涌四海2018-08-11配套Introduction to Java Programming Comprehen version 10 Edition
weixin_40450505
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功