Java 语言程序设计 -图形与多媒体处理

同心圆效果图:

 

/**
 *程序要求:新建一个600*600像素的应用程序窗口,并在窗口中绘制5个不同颜色的同心圆,
 *所有圆心都是屏幕的中心点,相邻两个圆直接的半径相差50像素
 *效果图如下图所示(颜色随机设置),源程序保存为Ex7_1.java。
 *作者:wwj
 *日期:2012/4/25
 *功能:显示一个有5个不同颜色的同心圆
 **/

 import javax.swing.*;
 import java.awt.*;
 import java.awt.Color;
 public class Ex7_1 extends JFrame
 {
	 int red,green,blue;
	 Color color;

	 public Ex7_1()
	 {
		 super("一个有5个不同颜色的同心圆");	//显示窗口名称
		 setSize(600,600);						//设置窗口大小
		 setVisible(true);						//设置为可见
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭动作
	
	 }

	
	 public void paint(Graphics g)
	 {
		 //第一个圆
		red=(int)(Math.random()*255);
		green=(int)(Math.random()*255);
		blue=(int)(Math.random()*255);
		color=new Color(red,green,blue);
		g.setColor(color);
		g.fillOval(175,175,250,250);
		//第二个圆
		red=(int)(Math.random()*255);
		green=(int)(Math.random()*255);
		blue=(int)(Math.random()*255);
		color=new Color(red,green,blue);
		g.setColor(color);
		g.fillOval(200,200,200,200);
		//第三个圆
		red=(int)(Math.random()*255);
		green=(int)(Math.random()*255);
		blue=(int)(Math.random()*255);
		color=new Color(red,green,blue);
		g.setColor(color);
		g.fillOval(225,225,150,150);
		//第四个圆
		red=(int)(Math.random()*255);
		green=(int)(Math.random()*255);
		blue=(int)(Math.random()*255);
		color=new Color(red,green,blue);
		g.setColor(color);
		g.fillOval(250,250,100,100);
		//第五个圆
		red=(int)(Math.random()*255);
		green=(int)(Math.random()*255);
		blue=(int)(Math.random()*255);
		color=new Color(red,green,blue);
		g.setColor(color);
		g.fillOval(275,275,50,50);

	 }		 
	
	 public static void main(String[] args)
	 {
		 Ex7_1 e = new Ex7_1();		
	 }

 }


 

播放音乐和切换图片的小程序效果图:

 

 

/**
 *程序要求:编写一个Applet的小程序,准备5幅图片和三个音乐文件,绘制到Applet中,
 *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。
 *作者:wwj
 *日期:2012/4/29
 *参考:neicole
 *功能:能进行图片和歌曲的选择变换的applet小程序
 **/

 import javax.swing.*;
 import java.awt.*;
 import	java.awt.event.*;
 import java.applet.Applet;
 import java.applet.AudioClip;

 
 public class Ex7_2 extends Applet implements ActionListener,ItemListener
 {

	 //创建两个面板
	 JPanel p1=new JPanel();
	 JPanel p2=new JPanel();
	 JPanel p3=new JPanel();
	 //声音对象
	 AudioClip[] sound=new AudioClip[3];
	 int playingSong=0;
	 //切换图片的按钮
	 JButton lastPic=new JButton("上一张");
	 JButton setLarge=new JButton("放大");
	 JButton setLittle=new JButton("缩小");
	 JButton nextPic=new JButton("下一张");
	 //切换歌曲的按钮
	 JButton lastSound=new JButton("上一首");
	 JButton play=new JButton("播放");
	 JButton loop=new JButton("连续");
	 JButton stop=new JButton("停止");
	 JButton nextSound=new JButton("下一首");
	 //曲目下拉列表
	 JComboBox xx;
	 String names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"};
	
	//创建画布对象
	MyCanvasl showPhotos;

	 

	 public void init()
	 {
		 //窗口布局
		 this.setLayout(new BorderLayout());

		 //为图片控制按钮注册监听器
		 lastPic.addActionListener(this);
		 setLarge.addActionListener(this);
		 setLittle.addActionListener(this);
		 nextPic.addActionListener(this);

		 //向面板p1添加组件
		 p1.add(lastPic);
		 p1.add(setLarge);
		 p1.add(setLittle);
		 p1.add(nextPic);
		 p1.repaint();
	
		//实例化下拉列表对象
		xx = new JComboBox(names);
		xx.addItemListener(this);

	    //为控制播放音乐按钮注册监听器
		lastSound.addActionListener(this);
		play.addActionListener(this);
		loop.addActionListener(this);
		stop.addActionListener(this);
		nextSound.addActionListener(this);

		for(int i=0;i<3;i++)
		 {
			sound[i]=getAudioClip(getCodeBase(),"music/"+"曲目"
					+Integer.toString(i+1)+".wav");
		 }
		

		
	    //向面板p2添加组件
		 p2.add(xx);
		 p2.add(lastSound);
		 p2.add(play);
		 p2.add(loop);
		 p2.add(stop);
		 p2.add(nextSound);
		 p2.repaint();
		
		showPhotos = new MyCanvasl();
		p3.add(showPhotos);
		 p3.repaint();

		//把面板p1和p2分别布置到窗口的北部和南部 
		 add(p1,BorderLayout.NORTH);
		 add(p2,BorderLayout.SOUTH);
		 add(p3,BorderLayout.CENTER);

		 this.repaint();

	 }


	 //按钮的事件处理
	 public void actionPerformed(ActionEvent e)
	 {

		
		if(e.getSource() == lastPic){
			showPhotos.changePhotoShow('P');
		}
		else if(e.getSource() == nextPic){
			showPhotos.changePhotoShow('N');
		}
		else if(e.getSource() == setLarge){
			showPhotos.changePhotoSize('B');
		}
		else if(e.getSource() == setLittle){
			showPhotos.changePhotoSize('S');
		}
	
		else if(e.getSource()==lastSound){	//上一首
			sound[playingSong].stop();
			playingSong=(playingSong-1+3)%3;
			xx.setSelectedIndex(playingSong);
			sound[playingSong].play();

		}
		else if(e.getSource()==play){		//按下播放按钮
			sound[playingSong].play();
		}
		else if(e.getSource()==loop){		//按下循环按钮
			sound[playingSong].loop();
		}
		else if(e.getSource()==stop){		//按下停止按钮
			sound[playingSong].stop();
		}
		else{								//下一首
			sound[playingSong].stop();
			playingSong=(playingSong+1)%3;
			xx.setSelectedIndex(playingSong);
			sound[playingSong].play();

		}	
	 }


	 //下拉列表的事件处理
	 public void itemStateChanged(ItemEvent e)
	 {
		 
		 sound[playingSong].stop();
		 sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());
	 }

	class MyCanvasl extends Canvas
	{
		
		public Image[] img=new Image[5];

		int MaxWidth = 600;
		int MaxHeight = 500;
		int nowImageIndex = 0;
		int coordinateX = 0;
		int coordinateY = 0;
		int currentWidth = MaxWidth;
		int currentHeight = MaxHeight;

		
		MyCanvasl(){
		 setSize(MaxWidth,MaxHeight);
		 //获取当前目录下的图片
		 for(int i=0;i<5;i++){
			 img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg");
		 }
		}


		private void changePhotoIndex(int index){
			nowImageIndex = index;
			changePhotoSize('M');
		}



		public void changePhotoShow(char command){
			if('P' == command){
				changePhotoIndex((nowImageIndex + 5 - 1 ) % 5);
			}
			else if('N' == command){
				changePhotoIndex((nowImageIndex + 1) % 5);
			}
		}
		


		 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方法用来在窗口显示图片
	 public void paint(Graphics g){
	       g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);

	 }
	}
 }


 

图书详细描述: 本书将Java语言作为大学生的计算机程序设计入门语言,其特色是内容全面、深入浅出、辅助教材立体配套。不但详细介绍了Java语言本身,而且讨论了面向对象的设计思想和编程方法、UML建模语言图形用户界面的编程方法、网络和数据程序的编程方法、线程的使用、Java集合框架等实用开发技术。全书以面向对象的程序设计方法贯穿始终,基础性和实用性并重。本书不仅能使读者掌握Java语言,而且能够让读者对现实世界中较简单的问题及其解决方法用计算机语言进行描述。    本书既可供大专院校用作“Java语言程序设计”课程教材,也可供Java 爱好者作为入门的自学教材。 目录第1章 Java语言基础知识 1.1 Java语言与面向对象的程序设计  1.1.1 面向对象的程序设计思想  1.1.2 Java语言的特点  1.1.3 Java类库 1.2 Java程序概述  1.2.1 Java开发环境  1.2.2 Application举例  1.2.3 Applet举例  1.2.4 Servlet举例  1.2.5 JSP和JavaBean举例 1.3 基本数据类型与表达式  1.3.1 变量与常量  1.3.2 基本数据类型  1.3.3 表达式与运算符  1.3.4 类型转换 1.4 数组的概念 1.5 数组的创建和引用  1.5.1 数组的声明  1.5.2 数组的创建  1.5.3 数组元素的初始化  1.5.4 数组的引用  1.5.5 多维数组 1.6 本章小结 习题第2章 类与对象的基本概念 2.1 面向对象的程序设计方法概述  2.1.1 抽象  2.1.2 封装  2.1.3 继承  2.1.4 多态 2.2 类与对象  2.2.1 类的声明  2.2.2 对象的声明与引用  2.2.3 数据成员  2.2.4 方法成员  2.2.5 类的组织——包的概念  2.2.6 类的访问控制  2.2.7 类成员的访问控制 2.3 对象初始化和回收  2.3.1 构造方法  2.3.2 内存回收技术 2.4 应用举例  2.4.1 声明BankAccount类  2.4.2 声明toString()方法  2.4.3 声明存取款方法  2.4.4 使用DecimalFormat类  2.4.5 声明类方法生成特殊的实例  2.4.6 声明类变量 2.5 UML简介  2.5.1 类图  2.5.2 对象图 2.6 本章小结 习题第3章 类的方法 3.1 方法的控制流程  3.1.1 if选择结构  3.1.2 switch选择结构  3.1.3 for循环结构  3.1.4 while语句  3.1.5 do while语句  3.1.6 break语句  3.1.7 continue语句 3.2 异常处理简介  3.2.1 异常处理的意义  3.2.2 错误的分类  3.2.3 异常的处理  3.2.4 生成异常对象  3.2.5 声明自己的异常类 3.3 方法的重载 3.4 本章小结 习题第4章 类的重用第5章 接口与多态第6章 输入输出流第7章 对象群体的组织第8章 多线程第9章 图形用户界面第10章 JDBC与数据库访问第11章 Servlet程序设计第12章 JSP程序设计参考文献
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小巫技术博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值