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

Mat*_*oni 7 java applet image 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

coo*_*ird 8

除非您调用的类Graphics.drawImage(Image, int, int, int, int, ImageObserver)是a 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您正在调整不需要的图像ImageObserver(例如BufferedImage已经包含要调整大小的图像的图像),那么您只需移交null:

// 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);
Run Code Online (Sandbox Code Playgroud)

也就是说,我将为我的图像大小调整库Thumbnailator投入一点插件.

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

Thumbnails.of("path/to/image")
  .size(100, 100)
  .toFile("path/to/thumbnail");
Run Code Online (Sandbox Code Playgroud)

Thumbnailator足够灵活,可以接受BufferedImages,Files和InputStreams作为输入.


看到你的编辑,我建议改变Hitter类,以便它将在构造函数中执行图像的大小调整.

由于你drawHitter在每次调用时都调用了方法,所以调用Applet.drawImageresize操作Graphics.drawImage被多次调用,即使是hitterWidthhitterHeight为所有意图和目的的常量.

Image提前调整大小,并在drawHitter方法中绘制预先调整大小的图像将更有效.