当前位置: 技术问答>java相关
谁能给我个得到一幅图象的长度和宽度的方法,最好是代码.高分!!
来源: 互联网 发布时间:2015-07-31
本文导语: 我不知道java.awt.Image.getWidth(ImageObserver observer)这个方法到底是怎么回事??参数也不是个Image对象,怎么是ImageObserver接口变量??? 有没有个简单的方法得到一副图象的长和宽啊?? | 转贴 import java....
我不知道java.awt.Image.getWidth(ImageObserver observer)这个方法到底是怎么回事??参数也不是个Image对象,怎么是ImageObserver接口变量???
有没有个简单的方法得到一副图象的长和宽啊??
有没有个简单的方法得到一副图象的长和宽啊??
|
转贴
import java.awt.*;
import java.applet.*;
public class Test extends Applet {
ImageCanvas ip;
public void init() {
Image i = getImage(getDocumentBase(), "on.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(i, 0);
try { tracker.waitForAll(); } catch (InterruptedException e) { ; }
ip = new ImageCanvas(i);
setLayout(new BorderLayout(10,10));
add("Center", ip);
}
}
class ImageCanvas extends Canvas {
Image image;
public ImageCanvas(Image i) {
super();
image = i;
}
public void paint(Graphics g) {
g.drawImage(image, image.getWidth(this) >> 1,
image.getHeight(this) >> 1, this);
}
}
// ImageObserver example
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
public class Test extends Applet implements ImageObserver {
Image img;
int width, height;
boolean image_ready = false;
public void init() {
image_ready = false;
img = getImage(getDocumentBase(),"on.gif");
img.getWidth(this);
img.getHeight(this);
prepareImage(img, this);
}
public boolean imageUpdate(Image img,int status, int x,
int y, int width, int height){
if ((status & HEIGHT) != 1) {
this.height = height;
}
if ((status & WIDTH) != 1) {
this.width = width;
}
if ((status & ALLBITS) != 1) {
image_ready = true;
repaint();
return true;
}
return false;
}
public void paint(Graphics g) {
if (image_ready)
g.drawImage(img, width >> 1, height >> 1,this);
}
}
import java.awt.*;
import java.applet.*;
public class Test extends Applet {
ImageCanvas ip;
public void init() {
Image i = getImage(getDocumentBase(), "on.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(i, 0);
try { tracker.waitForAll(); } catch (InterruptedException e) { ; }
ip = new ImageCanvas(i);
setLayout(new BorderLayout(10,10));
add("Center", ip);
}
}
class ImageCanvas extends Canvas {
Image image;
public ImageCanvas(Image i) {
super();
image = i;
}
public void paint(Graphics g) {
g.drawImage(image, image.getWidth(this) >> 1,
image.getHeight(this) >> 1, this);
}
}
// ImageObserver example
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
public class Test extends Applet implements ImageObserver {
Image img;
int width, height;
boolean image_ready = false;
public void init() {
image_ready = false;
img = getImage(getDocumentBase(),"on.gif");
img.getWidth(this);
img.getHeight(this);
prepareImage(img, this);
}
public boolean imageUpdate(Image img,int status, int x,
int y, int width, int height){
if ((status & HEIGHT) != 1) {
this.height = height;
}
if ((status & WIDTH) != 1) {
this.width = width;
}
if ((status & ALLBITS) != 1) {
image_ready = true;
repaint();
return true;
}
return false;
}
public void paint(Graphics g) {
if (image_ready)
g.drawImage(img, width >> 1, height >> 1,this);
}
}
|
呵呵,哪里需要那么复杂
ImageIcon image=new ImageIcon("c:\1.gif");
int h=image.getIconHeight();//得到图片的高
int w=image.getIconWidth();//得到图片的宽
ImageIcon image=new ImageIcon("c:\1.gif");
int h=image.getIconHeight();//得到图片的高
int w=image.getIconWidth();//得到图片的宽