如何在Graphics方法中使用ImageObserver drawImage()

时间:2011-07-10 02:37:06

标签: java image applet scale draw

我尝试使用的方法是: drawImage(image,int,int,int,int,ImageObserver)方法 所以我可以扩展我的图像,在我看到ImageObserver应该是这样的所有例子中,但这似乎不起作用(即我看到的唯一方法是: drawImage(image,int,int,ImageObserver),不知道这是否有所不同)。

这是我的主要类applet:

import java.applet.*;
import java.awt.*;

public class Main extends Applet implements Runnable{
    private Thread th;
    private Hitter hitter;

    //double buffering
    private Graphics dbg;
    private Image dbImage;

    public void init(){
        hitter = new Hitter(getImage(getCodeBase(), "Chitter.png"));
    }

    public void start(){
        th = new Thread(this);
        th.start();
    }

    public void stop(){
        th.stop();
    }

    public void update(Graphics g){
        if(dbImage == null){
            dbImage = createImage(this.getSize().width, this.getSize().width);
            dbg = dbImage.getGraphics();
        }

        dbg.setColor(getBackground());
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
        dbg.setColor(getForeground());
        paint(dbg);

        g.drawImage(dbImage, 0, 0, this);
    }

    public void paint(Graphics g){
        hitter.drawHitter(g);
    }

    public void run() {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        while(true){
            repaint();

            try{
                Thread.sleep(15);
            }catch(InterruptedException ex){}

            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }

    public boolean mouseMove(Event e, int x, int y){
        hitter.move(x);

        return true;
    }

}

这是Hitter类:

import java.awt.*;
import java.awt.image.ImageObserver;

public class Hitter{
    private int x, y;
    private Image hitter;
    private int hitterWidth = 50, hitterHeight = 10;
    private int appletsizeX = 500, appletsizeY = 500;

    Hitter(Image i){
        hitter = i;
        start();
    }

    public void drawHitter(Graphics g){
        g.drawImage(hitter, x, y, hitterWidth, hitterHeight, this);
    }

    public void move(int a){
        x = a;
    }

    public void start(){
        x = appletsizeX/2 - hitterWidth/2;
        y = 0;
    }
}

1 个答案:

答案 0 :(得分:8)

除非您致电Graphics.drawImage(Image, int, int, int, int, ImageObserver)的班级是ImageObserver,否则使用this作为ImageObserver的参数将无效:

class MyClass {
  public void resizeImage() {
    Graphics g = getGraphicsObjectFromSomewhere();

    // The following line will not compile, as `MyClass` 
    // does not implement `ImageObserver`.
    g.drawImage(img, 0, 0, 50, 50, this);
  }
}

如果您要调整不需要ImageObserver的图片(例如已包含您要调整大小的图片的BufferedImage),那么您只需移交{{1 }}:

null

那就是说,我要为我的图像大小调整库Thumbnailator添加一点插件。

如果只需要调整图像大小,可以像下面的代码一样简单地完成:

// The image we want to resize
BufferedImage img = ImageIO.read("some-image.jpg");

// The Graphics object of the destination
// -- this will probably just be obtained from the destination image.
Graphics g = getGraphicsObjectFromSomewhere();

// Perform the resizing. Hand a `null` for the ImageObserver,
// as we don't need one.
g.drawImage(img, 0, 0, 50, 50, null);

Thumbnailator非常灵活,可以接受Thumbnails.of("path/to/image") .size(100, 100) .toFile("path/to/thumbnail"); BufferedImageFile作为输入。


看到您的编辑,我建议更改InputStream类,以便它将在构造函数中执行图像的大小调整。

由于您在Hitter的每次调用中调用drawHitter方法,因此即使在Applet.drawImage和{Graphics.drawImage时,也会多次调用使用hitterWidth的调整大小操作对于所有意图和目的,{1}}都是常量。

提前调整hitterHeight的大小,并在Image方法中绘制预先调整大小的图片效率会更高。