《Java語言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第十四章練習(xí)題答案_第1頁
《Java語言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第十四章練習(xí)題答案_第2頁
《Java語言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第十四章練習(xí)題答案_第3頁
《Java語言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第十四章練習(xí)題答案_第4頁
《Java語言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第十四章練習(xí)題答案_第5頁
已閱讀5頁,還剩45頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Java 語言程序設(shè)計(jì)(基礎(chǔ)篇) (第 10 版 梁勇 著)第十四章 練習(xí)題答案14.1import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.layout.GridPane;import javafx.stage.Stage;import javafx.scene.image.ImageView;public class Exercise14_01 extends Application Override / Override

2、the start method in the Application class public void start(Stage primaryStage) GridPane pane = new GridPane(); pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);ImageView imageView1 =ImageView imageView2 =new ImageView(new ImageView("image/uk.gif""image/ca.gif"););Im

3、ageView imageView3 =ImageView imageView4 =new ImageView(new ImageView("image/china.gif""image/us.gif"););pane.add(imageView1, 0, 0); pane.add(imageView2, 1, 0); pane.add(imageView3, 0, 1); pane.add(imageView4, 1, 1);/ Create a scene and place it in the stage Scene scene =new Scen

4、e(pane);primaryStage.setTitle( "Exercise14_01" ); / Set the stage title primaryStage.setScene(scene); / Place the scene in the stage primaryStage.show(); / Display the stage* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command li

5、ne. */public static void main(String args) launch(args);14.1 附加importjava.util.Scanner;importjavafx.application.Application;importjavafx.geometry.Point2D;importjavafx.scene.Scene;importjavafx.scene.layout.Pane;importjavafx.scene.paint.Color;importjavafx.scene.shape.Circle;importjavafx.scene.text.Tex

6、t;importjavafx.stage.Stage;public class Exercise14_01Extra extends Application Override/ Override the start method in the Application classpublic void start(Stage primaryStage) orderSystem.out.print( "Enter coordinates and radius of two circles in this x1 y1 r1 x2 y2 r2: " );Scanner input

7、=new Scanner(System.in);double x1 = input.nextDouble(); double y1 = input.nextDouble(); double r1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double r2 = input.nextDouble();Circle c1 = new Circle(x1, y1, r1); c1.setFill( new Color(1, 1, 1, 0); c1.setStroke(C

8、olor.BLACK);Circle c2 = new Circle(x2, y2, r2); c2.setFill(new Color(1, 1, 1, 0);c2.setStroke(Color.BLACK);Pane pane = new Pane(); double paneWidth = 200; double paneHeight = 200;Text text = new Text(40, paneHeight - 20, "" ); if (contains(c1, c2) | contains(c2, c1) text.setText("One

9、circle is contained in another");else if (overlaps(c1, c2) text.setText( "The circles overlap" );else text.setText( "The circles do not overlap" );pane.getChildren().addAll(c1, c2, text);/ Create a scene and place it in the stageScene scene =new Scene(pane, paneWidth, paneHe

10、ight);primaryStage.setTitle( "Exercise14_01" ); / Set the stage title primaryStage.setScene(scene); / Place the scene in the stage primaryStage.show();/ Display the stage/* Returns true if c1 contains c2 or c2 contains c1 */private static boolean contains(Circle c1, Circle c2) return new P

11、oint2D(c1.getCenterX(), c1.getCenterY().distance(c2.getCenterX(), c2.getCenterY() <= Math.abs(c1.getRadius() - c2.getRadius();/* Returns true if c1 overlaps c2 */private static boolean overlaps(Circle c1, Circle c2) return new Point2D(c1.getCenterX(), c1.getCenterY().distance(c2.getCenterX(), c2.

12、getCenterY() <= c1.getRadius() + c2.getRadius();/* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String args) launch(args);14.2import javafx.application.Application;import javafx.geometry.Pos;import

13、javafx.scene.Scene;import javafx.scene.layout.GridPane;import javafx.stage.Stage;import javafx.scene.image.ImageView;import javafx.scene.image.Image;public class Exercise14_02 extends Application Override / Override the start method in the Application class public void start(Stage primaryStage) Imag

14、e imageX =Image imageO =new Image( "image/x.gif");new Image( "image/o.gif");GridPane pane =new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);for ( int i = 0; i < 3; i+) for ( int j = 0; j < 3; j+) int status = ( int )(Math.random() The main method

15、 is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String args) launch(args); 3);if (status = 0) pane.add(new ImageView(imageX), j, i);else if (status = 1) pane.add( new ImageView(imageO), j, i);/ Create a scene and place

16、 it in the stageScene scene = new Scene(pane);primaryStage.setTitle( "Exercise14_02" ); / Set the stage title primaryStage.setScene(scene); / Place the scene in the stage primaryStage.show(); / Display the stage14.2 附加import javafx.application.Application;importjavafx.scene.Scene;importjav

17、afx.scene.layout.Pane;importjavafx.scene.paint.Color;importjavafx.scene.shape.Circle;importjavafx.scene.shape.Line;importjavafx.scene.text.Text;importjavafx.stage.Stage;publicclass Exercise14_02Extraextends Application Override / Override the start method in the Application classpublic void start(St

18、age primaryStage) Pane pane = new Pane();double paneWidth = 200;double paneHeight = 200;double RADIUS = 10;Circle circles =new Circle4;for ( int i = 0; i < 4; i+) circlesi =new Circle(Math.random() * (paneWidth - 20) + 10,Math.random() * (paneHeight - 20) + 10, RADIUS);circlesi.setFill(Color.WHIT

19、E); circlesi.setStroke(Color.BLACK);for ( int i = 0; i < 4; i+) for ( int j = i; j < 4; j+) pane.getChildren().add(new Line(circlesi.getCenterX(), circlesi.getCenterY(), circlesj.getCenterX(), circlesj.getCenterY();for ( int i = 0; i < 4; i+) pane.getChildren().addAll(circlesi, new Text(cir

20、clesi.getCenterX() - 6,circlesi.getCenterY() + 4, "" + (i + 1);/ Create a scene and place it in the stageScene scene = new Scene(pane, paneWidth, paneHeight); primaryStage.setTitle( "Exercise14_02" ); / Set the stage title primaryStage.setScene(scene);/ Place the scene in the sta

21、geprimaryStage.show();/ Display the stage* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String args) launch(args);14.3import java.util.ArrayList;import javafx.application.Application;import javafx.geo

22、metry.Pos;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.stage.Stage;import javafx.scene.image.ImageView;public class Exercise14_03 extends Application Override / Override the start method in the Application class public void start(Stage primaryStage) / There are two ways fo

23、r shuffling. One is to use the hint in the book./ The other way is to use the static shuffle method in the java.util.Collections class.ArrayList<Integer> list =new ArrayList<>();for ( int i = 1; i <= 52; i+) list.add(i);java.util.Collections.shuffle(list);HBox pane =new HBox(5);pane.s

24、etAlignment(Pos.CENTER);pane.getChildren().add(newImageView("image/card/"+ list.get(0) +".png" );pane.getChildren().add(newImageView("image/card/"+ list.get(1) +".png" );pane.getChildren().add(newImageView("image/card/"+ list.get(2) +".png"

25、 );/ Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle( "Exercise14_03" ); / Set the stage title primaryStage.setScene(scene); / Place the scene in the stage primaryStage.show();/ Display the stage/* The main method is only needed for the IDE with

26、limited* JavaFX support. Not needed for running from the command line.*/public static void main(String args) launch(args);14.3 附加import javafx.application.Application;import javafx.collections.ObservableList;import javafx.scene.Scene;import javafx.scene.layout.Pane;import javafx.scene.paint.Color;im

27、port javafx.scene.shape.Polygon;import javafx.stage.Stage;public class Exercise14_03Extra extends Application Override / Override the start method in the Application classpublic void start(Stage primaryStage) Pane pane = new Pane();double paneWidth = 200;double paneHeight = 200;pane.setStyle( "

28、-fx-background-color: blue" ); pane.getChildren().addAll(getStar(paneWidth / 2, paneHeight / 2, 50);/ Create a scene and place it in the stageScene scene =new Scene(pane, paneWidth, paneHeight);primaryStage.setTitle( "Exercise14_03" ); / Set the stage title primaryStage.setScene(scene

29、); / Place the scene in the stage primaryStage.show();/ Display the stageradius) private Polygon getStar( double centerX, double centerY, double / Create a Polygon objectPolygon polygon =new Polygon();polygon.setFill(Color.WHITE);polygon.setStroke(Color.BLACK);doublea = 2 * Math.PI / 20;doubleb = 2

30、* Math.PI / 10;doubleh1 = radius * Math.sin(a);doubler2 = h1 / Math.sin(a + b);/ The radius of the inner pointsObservableList<Double> list = polygon.getPoints();/ Add points to the polygon double angle = 2 * Math.PI / 20;double angle1 = - 2 * Math.PI / 20 + 2 * Math.PI / 5; for ( int i = 0; i

31、< 5; i+) / Add an outer point list.add(centerX + radius * Math.cos(angle); list.add(centerY - radius * Math.sin(angle); angle += 2 * Math.PI / 5;/ Add an inner point list.add(centerX + r2* Math.cos(angle1); list.add(centerY - r2 * Math.sin(angle1); angle1 += 2 * Math.PI / 5;return polygon;/* The

32、main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String args) launch(args);14.4import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.layout.HBox;import

33、 javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_04 extends Application Override / Override the start method in the Application classpublic

34、void start(Stage primaryStage) HBox pane =new HBox();pane.setAlignment(Pos.CENTER);Font font = Font.font("Times New Roman" , FontWeight.BOLD,FontPosture.ITALIC, 22);for ( int i = 0; i < 5; i+) Text txt =new Text( "Java" );txt.setRotate(90);txt.setFont(font);txt.setFill(new Col

35、or(Math.random(), Math.random(), Math.random(),Math.random();pane.getChildren().add(txt);/ Create a scene and place it in the stageScene scene =new Scene(pane, 200, 100);primaryStage.setTitle( "Exercise14_04" ); / Set the stage title primaryStage.setScene(scene); / Place the scene in the s

36、tage primaryStage.show();/ Display the stage* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String args) launch(args);14.4 附加import javafx.application.Application;import javafx.collections.ObservableLi

37、st;import javafx.scene.Scene;import javafx.scene.layout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Polygon;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class Exercise14_04Extra extends Application Override / Override the start method in the Application cla

38、ss public void start(Stage primaryStage) Pane pane = new Pane();double paneWidth = 570;double paneHeight = 310;Rectangle rectangle =new Rectangle(10, 10, 240, 160);rectangle.setFill(Color.BLUE); pane.getChildren().addAll(rectangle);int centerX = 30;int centerY = 30;int radius = 10; pane.getChildren(

39、).addAll(getStar(centerX, centerY, radius);for ( int j = 0; j < 5; j+) for ( int i = 0; i < 6; i+) pane.getChildren().addAll(getStar(centerX, centerY, radius); centerX += 40;centerX = 30;centerY += 30;centerX = 50;centerY = 45;for ( int j = 0; j < 4; j+) for ( int i = 0; i < 5; i+) pane.

40、getChildren().addAll(getStar(centerX, centerY, radius); centerX += 40;centerX = 50;centerY += 30;int y = 0;for ( int i = 0; i < 4; i+) Rectangle r =new Rectangle(250, 10 + y, 550 - 240, 160 / 7);r.setFill(Color.RED);y += 2 * 160 / 7;pane.getChildren().addAll(r);for ( int i = 0; i < 3; i+) Rect

41、angle r =new Rectangle(10, 10 + y, 550, 160 / 7);r.setFill(Color.RED);y += 2 * 160 / 7;pane.getChildren().addAll(r);/ Create a scene and place it in the stageScene scene =new Scene(pane, paneWidth, paneHeight);primaryStage.setTitle( "Exercise14_04" ); / Set the stage title primaryStage.set

42、Scene(scene); / Place the scene in the stage primaryStage.show(); / Display the stageradius) private Polygon getStar( double centerX, double centerY, double / Create a Polygon objectPolygon polygon =new Polygon();polygon.setFill(Color.WHITE);polygon.setStroke(Color.BLACK);doublea = 2 * Math.PI / 20;

43、doubleb = 2 * Math.PI / 10;doubleh1 = radius * Math.sin(a);doubler2 = h1 / Math.sin(a + b);/ The radius of the inner pointsObservableList<Double> list = polygon.getPoints();/ Add points to the polygondouble angle = 2 * Math.PI / 20;double angle1 = - 2 * Math.PI / 20 + 2 * Math.PI / 5; for ( in

44、t i = 0; i < 5; i+) / Add an outer point list.add(centerX + radius * Math.cos(angle); list.add(centerY - radius * Math.sin(angle); angle += 2 * Math.PI / 5;/ Add an inner point list.add(centerX + r2* Math.cos(angle1); list.add(centerY - r2 * Math.sin(angle1); angle1 += 2 * Math.PI / 5;return poly

45、gon;/* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String args) launch(args);14.5import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.Pane;import javafx.scene.te

46、xt.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_05 extends Application Override/ Override the start method in the Application classpublic void start(Stage primaryStage) Pane pane = new Pa

47、ne();Font font = Font.font( "Times New Roman" , FontWeight.BOLD, FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA " ;double radius = 80;I!);for ( int i = 0; i < s.length(); i+) double alpha = 2 * Math.PI * (s.length() - i) / s.length(); Text txt =new Text(radius * Math.cos

48、(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + txt.setFont(font);txt.setRotate(360 * i / s.length() + 90); pane.getChildren().add(txt);/ Create a scene and place it in the stageScene scene =new Scene(pane, 240, 240);primaryStage.setTitle( "Exercise14_05" ); / Set the stage tit

49、le primaryStage.setScene(scene); / Place the scene in the stage primaryStage.show();/ Display the stage/* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String args) launch(args);14.5 附加import javafx.app

50、lication.Application;import javafx.collections.ObservableList;import javafx.scene.Scene;import javafx.scene.layout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Polygon;import javafx.stage.Stage;public class Exercise14_05Extra extends Application Override / Override the start method

51、 in the Application classpublic void start(Stage primaryStage) Pane pane = new Pane();double paneWidth = 200;double paneHeight = 200;pane.getChildren().addAll(getStar(paneWidth / 2, paneHeight / 2, 80);/ Create a scene and place it in the stageScene scene =new Scene(pane, paneWidth, paneHeight);prim

52、aryStage.setTitle( "Exercise14_05" ); / Set the stage title primaryStage.setScene(scene); / Place the scene in the stage primaryStage.show();/ Display the stageradius) private Polygon getStar( double centerX, double centerY, double / Create a Polygon objectPolygon polygon = new Polygon();p

53、olygon.setFill(new Color(1, 1, 1, 0);polygon.setStroke(Color.BLACK);ObservableList<Double> list = polygon.getPoints();/ Add points to the polygondouble angle = 2 * Math.PI / 20;for ( int i = 0; i < 5; i+) list.add(centerX + radius * Math.cos(angle);list.add(centerY - radius * Math.sin(angle

54、);angle += 2 * 2 * Math.PI / 5;return polygon;/* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String args) launch(args);14.6import javafx.application.Application;import javafx.scene.Scene;import javafx

55、.scene.layout.Pane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javafx.scene.shape.Rectangle; public class Exercise14_06 extends Application Override / Override the start method in the Application classpublic void start(Stage primaryStage) double WIDTH = 200;double HEIGHT = 200;P

56、ane pane =new Pane();for ( int i = 0; i < 8; i+) boolean isWhite = i % 2 = 0;for ( int j = 0; j < 8; j+) Rectangle rectangle =new Rectangle(i * WIDTH / 8,j * HEIGHT / 8, WIDTH / 8, HEIGHT / 8);rectangle.setStroke(Color.BLACK);if (isWhite) rectangle.setFill(Color.WHITE);else rectangle.setFill(Color.BLACK);isWhite = !isWhite;pane.getChildren().add(rectangle);/ Create a scene and place it in the stageScene s

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論