当前位置: 技术问答>java相关
简单问题!getImage 后怎么知道 image 的大小!
来源: 互联网 发布时间:2015-02-08
本文导语: 在取得图片参数后, image = getImage(getCodeBase(),"hi.jpg"); 怎么取得 hi.jpg 这个图片的宽与高! | follow the examples: import java.awt.*; import java.applet.*; public class Test extends Applet { Imag...
在取得图片参数后,
image = getImage(getCodeBase(),"hi.jpg");
怎么取得 hi.jpg 这个图片的宽与高!
image = getImage(getCodeBase(),"hi.jpg");
怎么取得 hi.jpg 这个图片的宽与高!
|
follow the examples:
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);
}
}