2011-07-10 60 views
7

我試圖使用的方法是: drawImage(image,int,int,int,int,ImageObserver)方法 這樣我就可以縮放我的圖像我看過ImageObserver的例子應該是這樣的,但這似乎不起作用(即我見過的唯一方法是: drawImage(image,int,int,ImageObserver),不知道這是否會產生區別)。如何在圖形方法中使用ImageObserver drawImage()

這裏是我的主類是小程序:

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; 
    } 

} 

這裏是揮臂類:

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; 
    } 
} 
+0

你的問題太含糊。請張貼演示問題的代碼。 – g051051

回答

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已經包含要調整圖像)的圖像,那麼你可以只交出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); 

也就是說,我將爲我的圖像調整大小庫Thumbnailator插入一個小插件。

如果所要求的所有是調整圖像的大小,它可以被實現爲簡單的,如以下代碼:

Thumbnails.of("path/to/image") 
    .size(100, 100) 
    .toFile("path/to/thumbnail"); 

Thumbnailator是足夠柔性的,以接受BufferedImage S,File s和InputStream S作爲輸入。


看到您的編輯,我建議改變Hitter類,因此,它會在構造函數中進行圖像的大小調整。

由於您呼叫從Applet.drawImage每次調用drawHitter方法,使用Graphics.drawImage調整操作被稱爲很多次,即使hitterWidthhitterHeight是,所有意圖和目的,常量。

提前調整Image的大小,並在drawHitter方法中繪製預先調整大小的圖像效率更高。