实验题目:
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);
}
}
}