Java程序設計教程(第七版) John Lewis & William Loftus 電子工業出版社
?
PP2.16編寫一個applet,畫出北斗七星,并在夜空中添加一些其他的星星。
?
注意:
1. 在不同的IDE環境下,有部分代碼可能需要變更。Java代碼中的package和class名稱自行設置,本文中采用Test。
2. 本程序應用到Applet,使用IDE時需要注意配置問題,具體請參考:https://blog.csdn.net/Trista_1999/article/details/103204112,如果還有其他配置問題,可自行搜索。
IDE工具:IntelliJ IDEA
?
代碼塊:
import javax.swing.JApplet;
import java.awt.*;
import java.util.Random;public class Test extends JApplet {public void paint
(Graphics page
) {setBackground
(Color.black
);page
.setColor(Color.white
);int[] xP
= {100, 104, 106, 108, 112, 109, 110, 106, 102, 103};int[] yP
= {100, 100, 97, 100, 100, 102, 108, 103, 108, 102};page
.drawPolygon(xP
, yP
, 10);page
.fillPolygon(xP
, yP
, 10);int[][] xBD
= new int[7][10];int[][] yBD
= new int[7][10];int[] xAdd
= {1000, 1050, 1350, 1400, 650, 400, 100};int[] yAdd
= {350, 550, 550, 300, 250, 210, 290};for(int i
=0; i
<xAdd
.length
; i
++){for(int j
=0; j
<xP
.length
; j
++){xBD
[i
][j
] = xP
[j
] + xAdd
[i
];yBD
[i
][j
] = yP
[j
] + yAdd
[i
];}page
.drawPolygon(xBD
[i
], yBD
[i
], 10);page
.fillPolygon(xBD
[i
], yBD
[i
], 10);}page
.drawLine(1106, 455, 1156, 655);page
.drawLine(1156, 655, 1455, 655);page
.drawLine(1455, 655, 1505, 405);page
.drawLine(1505, 405, 1106, 455);page
.drawLine(1106, 455, 755, 355);page
.drawLine(755, 355, 505, 315);page
.drawLine(505, 315, 205, 395);int[] xSmall
= new int[10];int[] ySmall
= new int[10];for(int i
=0; i
<xP
.length
; i
++){xSmall
[i
] = xP
[i
]/2 - 100;ySmall
[i
] = yP
[i
]/2;}page
.drawPolygon(xSmall
, ySmall
, 10);page
.fillPolygon(xSmall
, ySmall
, 10);Random rand
= new Random();int randX
, randY
;int[] randSmallX
= new int[10];int[] randSmallY
= new int[10];for(int i
=0; i
<50; i
++){randX
= rand
.nextInt(2000);randY
= rand
.nextInt(2000);for(int j
=0; j
<xP
.length
; j
++){randSmallX
[j
] = xSmall
[j
] + randX
;randSmallY
[j
] = ySmall
[j
] + randY
;}page
.drawPolygon(randSmallX
, randSmallY
, 10);page
.fillPolygon(randSmallX
, randSmallY
, 10);}}
}
實現效果如下:
總結
以上是生活随笔為你收集整理的Java //PP2.16编写一个applet,画出北斗七星,并在夜空中添加一些其他的星星的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。