2011-07-10 10 views
7

私が使用しようとしているメソッドは、 drawImage(image、int、int、int、int、int、ImageObserver)メソッド です。私はImageObserverを見てきた例がこれでなければなりませんが、これはうまくいかないようです(つまり、私が見た唯一の方法は、 drawImage(image、int、int、ImageObserver)。差)。ここでGraphicsメソッドdrawImage()でImageObserverを使用する方法

はアプレットである私のメインクラスです。ここ

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)を呼び出しされているクラスとしてthisを使用して、ImageObserverでない限り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); 
    } 
} 

あなたが(例えば、すでにあなたがサイズを変更したい画像が含まれているBufferedImageとして)ImageObserverを必要としない画像をリサイズしている場合は、あなただけの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メソッドで事前にサイズ変更されたイメージを描画する方が効率的です。

関連する問題