實驗題目:
1. 繪製同心圓
2. 編寫Applet小程序,實現簡單的圖片瀏覽,音樂播放控制的功能.
正文:
1. 繪製同心圓
// Ex7_1.java
/**
* 題目要求:
* 新建一個600*600像素的應用程序窗口,並在窗口中繪製5個不同顏色的同心圓,所有圓心都是屏幕的中心點,
* 相鄰兩個圓直接的半徑相差50像素(顏色隨機設置)。
* 源程序保存爲Ex7_1.java。
**/
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
public class Ex7_1 extends JFrame
{
public Ex7_1(){
super("繪製同心圓");
setSize(600, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.fillOval(175, 175, 250, 250);
g.setColor(Color.yellow);
g.fillOval(200, 200, 200, 200);
g.setColor(Color.green);
g.fillOval(225, 225, 150, 150);
g.setColor(Color.orange);
g.fillOval(250, 250, 100, 100);
g.setColor(Color.red);
g.fillOval(275, 275, 50, 50);
}
public static void main(String [] args){
Ex7_1 test = new Ex7_1();
}
}
2. 編寫Applet小程序,實現簡單的圖片瀏覽,音樂播放控制的功能.
Ex7_2.html
<html>
<Applet code = Ex7_2.class width = 800 height = 600>
</Applet>
</html>
Ex7_2.java
// Ex7_2.java
/**
* 題目要求:
* 編寫一個Applet的小程序,準備5幅圖片和三個音樂文件,繪製到Applet中,
* 並增加幾個按鈕,控制圖片的切換、放大、縮小和音樂文件的播放。
**/
/**
* 程序編寫設計思路:
* 1.先初始化各個圖片和音樂對象,
* 2.然後給全部按鈕設置監聽器,
* 3.之後給窗口進行南,中,北三個版塊的佈局。
* PS: 這些都分別封裝到不同的方法中,功能通過方法的調用來實現。
**/
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.applet.AudioClip;
public class Ex7_2 extends Applet implements ItemListener, ActionListener
{
// 創建面板對象
JPanel north_photo_choice = new JPanel();
JPanel centre_photo_show = new JPanel();
JPanel south_music_control = new JPanel();
// 創建圖片按鈕對象
JButton shangyizhang = new JButton("上一張");
JButton fangda = new JButton("放大");
JButton suoxiao = new JButton("縮小");
JButton xiayizhang = new JButton("下一張");
// 創建音樂按鈕對象
String names[] = {"第一首歌", "第二首歌", "第三首歌"};
JComboBox musicChoice = new JComboBox(names);
JButton shangyishou = new JButton("上一首");
JButton bofang = new JButton("播放");
JButton lianxu = new JButton("連續");
JButton stop = new JButton("停止");
JButton xiayishou = new JButton("下一首");
// 創建圖片和音樂的對象
MyCanvas showPhoto;
int showingPhoto = 0;
AudioClip [] sound = new AudioClip[3];
int playingSound = 0;
/**
* 方法名稱:init()
* 方法功能:Ex7_2 的構造函數
* 1.先初始化各個圖片和音樂對象,
* 2.然後給全部按鈕設置監聽器,
* 3.之後給窗口進行南,中,北三個版塊的佈局。
* PS: 這些都分別封裝到不同的方法中,功能通過方法的調用來實現。
**/
public void init(){
constructionImageAndSound();
setAllListener();
setNorthLayout();
setCentreLayout();
setSouthLayout();
setWholeLayout();
}
/**
* 方法名稱:constructionImageAndSound()
* 方法功能:初始化showPhoto對象及sound[3]組的各個對象
**/
private void constructionImageAndSound(){
showPhoto = new MyCanvas();
for(int i = 0; i < 3; ++i){
sound[i] = getAudioClip(getCodeBase(), "music/music" + Integer.toString(i+1) + ".wav");
}
}
/**
* 方法名稱:setAllListener()
* 方法功能:給各個按鈕安裝監聽器,該類的方法中繼承了接口,通過本類的接口監聽。
**/
private void setAllListener(){
// 增加與圖片事件有關按鈕的監聽
shangyizhang.addActionListener(this);
fangda.addActionListener(this);
suoxiao.addActionListener(this);
xiayizhang.addActionListener(this);
// 增加與音樂事件有關按鈕的監聽
musicChoice.addItemListener(this);
shangyishou.addActionListener(this);
bofang.addActionListener(this);
lianxu.addActionListener(this);
stop.addActionListener(this);
xiayishou.addActionListener(this);
}
/**
* 方法名稱:setNorthLayout()
* 方法功能:給北方的組件,即圖片相關設置的組件佈局
**/
private void setNorthLayout(){
north_photo_choice.add(shangyizhang);
north_photo_choice.add(fangda);
north_photo_choice.add(suoxiao);
north_photo_choice.add(xiayizhang);
}
/**
* 方法名稱:setCentreLayout()
* 方法功能:給中間的圖片區域佈局。
**/
private void setCentreLayout(){
centre_photo_show.add(showPhoto);
}
/**
* 方法名稱:setSouthLayout()
* 方法功能:給南邊的音樂相關按鈕佈局。
**/
private void setSouthLayout(){
south_music_control.add(musicChoice);
south_music_control.add(shangyishou);
south_music_control.add(bofang);
south_music_control.add(lianxu);
south_music_control.add(stop);
south_music_control.add(xiayishou);
}
/**
* 方法名稱:setWholeLayout()
* 方法功能:爲整個窗體佈局。
**/
private void setWholeLayout(){
this.setLayout(new BorderLayout());
this.add("North", north_photo_choice);
north_photo_choice.repaint();
this.add("Center", centre_photo_show);
centre_photo_show.repaint();
this.add("South", south_music_control);
north_photo_choice.repaint();
this.repaint();
}
/**
* 方法名稱:itemStateChanged(ItemEvent e)
* 方法功能:監聽音樂選擇組件的實現。
**/
public void itemStateChanged(ItemEvent e){
sound[playingSound].stop(); // 先暫停音樂
playingSound = musicChoice.getSelectedIndex(); // 然後改變所要播放音樂的下標
}
/**
* 方法名稱:actionPerformed(ActionEvent e)
* 方法功能:全部組件的功能實現,即完成音樂,圖片按鈕功能。
**/
public void actionPerformed(ActionEvent e){
// 音樂的事件處理
if (e.getSource() == shangyishou) { // 改變音樂爲上一首
sound[playingSound].stop(); // 先暫停當前音樂
playingSound = (playingSound - 1 + 3) % 3; // 然後計算上一音樂的數組下標
musicChoice.setSelectedIndex(playingSound); // 同時設定多選框中的下標
sound[playingSound].play(); // 最後播放選擇後的音樂
}
else if(e.getSource() == xiayishou){ // 改變音樂爲上一首
sound[playingSound].stop();
playingSound = (playingSound + 1) % 3;
musicChoice.setSelectedIndex(playingSound);
}
else if(e.getSource() == bofang){ // 播放音樂
sound[playingSound].play();
}
else if(e.getSource() == stop){ // 停止播放音樂
sound[playingSound].stop();
}
else if(e.getSource() == lianxu){ // 連續播放音樂
sound[playingSound].loop();
}
// 圖片事件的處理
else if(e.getSource() == shangyizhang){
showPhoto.changePhotoShow('P');
}
else if(e.getSource() == xiayizhang){
showPhoto.changePhotoShow('N');
}
else if(e.getSource() == fangda){
showPhoto.changePhotoSize('B');
}
else if(e.getSource() == suoxiao){
showPhoto.changePhotoSize('S');
}
}
/**
* 類名: MyCanvas
* 類功能:繪製600 * 500大小以內的圖片.
**/
class MyCanvas extends Canvas
{
Image [] photo = new Image[5];
int MaxWidth = 600;
int MaxHeight = 500;
int nowPhotoIndex = 0;
int coordinateX = 0;
int coordinateY = 0;
int currentWidth = MaxWidth;
int currentHeight = MaxHeight;
/**
* 方法名稱:MyCanvas() [構造函數]
* 方法功能:設定繪製畫板大小及初始化各圖片
**/
MyCanvas(){
setSize(MaxWidth, MaxHeight);
for (int i = 0; i < 5; ++i){
photo[i] = getImage(getCodeBase(), "image/" + Integer.toString(i+1) + ".jpg");
}
}
/**
* 方法名稱: changePhotoIndex(int index)
* 方法功能:通過傳入下標直接改變圖片的下標並且使對應下標的新圖片顯示。
* 前置條件:已初始化changePhotoSize(char)函數
**/
private void changePhotoIndex(int index){
nowPhotoIndex = index;
changePhotoSize('M');
//repaint();
}
/**
* 方法名稱: changePhotoShow(char command)
* 方法功能:使圖片顯示上一張或者下一張
* 前置條件:已初始化changePhotoIndex(int index)函數
**/
public void changePhotoShow(char command){
if('P' == command){
changePhotoIndex((nowPhotoIndex + 5 - 1 ) % 5);
}
else if('N' == command){
changePhotoIndex((nowPhotoIndex + 1) % 5);
}
}
/**
* 方法名稱:changePhotoSize(char command)
* 方法功能:改變圖片大小(最大化,縮小100* 100像素,放大100* 100像素)及圖片顯示的左上角座標。
**/
public void changePhotoSize(char command){
if ('M' == command){
currentWidth = MaxWidth;
currentHeight = MaxHeight;
}
else if ('B' == command){
if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){
currentWidth += 100;
currentHeight += 100;
}
}
else if('S' == command){
if((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){
currentWidth = currentWidth - 100;
currentHeight = currentHeight - 100;
}
}
coordinateX = (MaxWidth - currentWidth) / 2;
coordinateY = (MaxHeight - currentHeight) / 2;
repaint();
}
/**
* 方法名稱:paint(Graphics g)
* 方法功能:按照指定的圖片大小,座標,繪製指定的圖片。
**/
public void paint(Graphics g){
g.drawImage(photo[nowPhotoIndex], coordinateX, coordinateY, currentWidth, currentHeight, this);
}
}
}