主要思路;
创新互联建站服务项目包括怀安网站建设、怀安网站制作、怀安网页制作以及怀安网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,怀安网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到怀安省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
。创建父类子类你自己看着敲进去,比较简单。
每个函数都要有一个带参数的构造函数;当数组中的参数传入时可以对子类对象初始化。
最后比较排序我是用另一个数组接收原数组元素(对象)中的getWeight() 方法的返回值用于排序,然后在找到对应的值打出来就能达到你要的效果了。
父类:
package playgame;
public abstract class Pet {
private String name;
private int age;
private double weight;
public void getInformation(String name,int age,double weight){
this.name=name;
this.age=age;
this.weight=weight;
}
public double getWeight(){
return weight;
}
public String toString(){
String str="name="+name+",age="+age+",weight"+weight;
return str;
}
public abstract void speak();
public abstract void eat();
}
package playgame;
public class Bird extends Pet{
private String name;
private double weight;
private double flyspeed;
public Bird(String name,double flyspeed,double weight){
this.name=name;
this.flyspeed=flyspeed;
this.weight=weight;
}
public double getWeight(){
return weight;
}
public void speak(){
System.out.println("我是一只小小鸟");
}
public void eat(){
System.out.println("我最爱吃虫子");
}
public String toString(){
String str="name="+name+",flyspeed="+flyspeed+",weight="+weight;
return str;
}
}
package playgame;
public class Cat extends Pet {
private String name;
private String coulour;
private double weight;
public Cat(String name,String coulour,double weight){
this.name=name;
this.coulour=coulour;
this.weight=weight;
}
public double getWeight(){
return weight;
}
public void speak() {
System.out.println("我是一只猫");
}
public void eat() {
System.out.println("我爱吃鱼");
}
public String toString(){
String str="name="+name+",coulour="+coulour+",weight="+weight;
return str;
}
}
package playgame;
public class Dog extends Pet {
private String name;
private double weight;
private String variety;
public Dog(String name,String variety,double weight){
this.name=name;
this.variety=variety;
this.weight=weight;
}
public double getWeight(){
return weight;
}
public void speak() {
System.out.println("我是一只狗");
}
public void eat() {
System.out.println("我爱吃骨头");
}
public String toString(){
String str="name="+name+",weight="+weight+",variety="+variety;
return str;
}
}
package playgame;
import java.util.Arrays;
public class Text1 {
public static void main(String[] args) {
Pet arr[]={new Dog("狗","藏獒",180000.5),new Dog("狗","藏獒",180006.5),
new Cat("猫","红色",18000.5),new Cat("猫","黑色",18000.5),
new Bird("鸟",20.5,180), new Bird("鸟",22.5,187)};
double arr1[]=new double[6];
for(int i=0;i6;i++){
arr1[i]=arr[i].getWeight();
}
Arrays.sort(arr1);
for(int i=0;i6;i++){
for(int j=0;j6;j++){
if(arr1[i]==arr[j].getWeight()){
System.out.println(arr[j].toString()+"\t");
break;
}
}
}
}
}
最后面这个是主函数的类。。请他三个子类在中间,有不明白的地方在追问。
canvas中有一个函数translate()是用来实现画布平移的,画布的原状是以左上角为原点,向左是X轴正方向,向下是Y轴正方向,如下图所示,也就是(0,0)。
int rgbR;
int rgbG;
int rgbB;
int minx = 0;
int miny = 0;
try {
File file = new File("E:\\dd.png");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();//图片宽度
int height = image.getHeight();//图片高度
for (int i = minx; i width; i++) {
for (int j = miny; j height; j++) {
int pixel = image.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgbR = (pixel 0xff0000) 16;
rgbG = (pixel 0xff00) 8;
rgbB = (pixel 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgbR + "," + rgbG + "," + rgbB + ")");
}
}
System.out.println("图片宽度为:"+width+",高度为:"+height);
} catch (IOException e) {
System.out.println("读取文件出错");
e.printStackTrace();
}
首先到sun下载最新的jmf,然后安装。
然后,说一下需求
1. 用摄像头拍照
2. 在文本框输入文件名
3. 按下拍照按钮,获取摄像头内的图像
4. 在拍下的照片上有一红框截取固定大小的照片。
5. 保存为本地图像为jpg格式,不得压缩画质
技术关键,相信也是大家最感兴趣的部分也就是如何让一个摄像头工作,并拍下一张照片了。
利用jmf,代码很简单:
//利用这三个类分别获取摄像头驱动,和获取摄像头内的图像流,获取到的图像流是一个swing的component组件类
public static player player = null;
private capturedeviceinfo di = null;
private medialocator ml = null;
//文档中提供的驱动写法,为何这么写我也不知:)
string str1 = "vfw:logitech usb video camera:0 ";
string str2 = "vfw:microsoft wdm image capture (win32):0 ";
di = capturedevicemanager.getdevice(str2);
ml = di.getlocator();
try
{
player = manager.createrealizedplayer(ml);
player.start();
component comp;
if ((comp = player.getvisualcomponent()) != null)
{
add(comp, borderlayout.north);
}
}
catch (exception e)
{
e.printstacktrace();
}
接下来就是点击拍照,获取摄像头内的当前图像。
代码也是很简单:
private jbutton capture;
private buffer buf = null;
private buffertoimage btoi = null;
private imagepanel imgpanel = null;
private image img = null;
private imagepanel imgpanel = null;
jcomponent c = (jcomponent) e.getsource();
if (c == capture)//如果按下的是拍照按钮
{
framegrabbingcontrol fgc =(framegrabbingcontrol) player.getcontrol( "javax.media.control.framegrabbingcontrol ");
buf = fgc.grabframe(); // 获取当前祯并存入buffer类
btoi = new buffertoimage((videoformat) buf.getformat());
img = btoi.createimage(buf); // show the image
imgpanel.setimage(img);
}
保存图像的就不多说了,以下为示例代码
bufferedimage bi = (bufferedimage) createimage(imgwidth, imgheight);
graphics2d g2 = bi.creategraphics();
g2.drawimage(img, null, null);
fileoutputstream out = null;
try
{
out = new fileoutputstream(s);
}
catch (java.io.filenotfoundexception io)
{
system.out.println( "file not found ");
}
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
jpegencodeparam param = encoder.getdefaultjpegencodeparam(bi);
param.setquality(1f, false);//不压缩图像
encoder.setjpegencodeparam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.ioexception io)
{
system.out.println( "ioexception ");
}
把.jar文件导入。下载了jmf后需要安装,安装后你的那个jmf目录下就会有一个lib文件夹里面有.jar文件,然后打开eclipse,右键选择你的工程-〉属性-〉java build path- library-〉add external jars 找到你的jmf目录下lib的那个文件夹然后选中那些文件导入就ok了。
然后利用工具提供的导入文件帮助,一个一个导就OK了