2011-07-10 17 views
7

Metoda, którą próbuję użyć, to: metoda drawImage (image, int, int, int, int, ImageObserver) , dzięki czemu mogę skalować mój obraz, na wszystkich przykłady, które widziałem w ImageObserver powinny być takie, ale to nie działa (tj. jedyne metody jakie widziałem to: drawImage (image, int, int, ImageObserver), nie wiem czy to powoduje różnica).Sposób korzystania z ImageObserver w metodzie Graphics drawImage()

Oto moje główne klasy, która jest aplet:

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

} 

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

Twoje pytanie jest zbyt nieprecyzyjne. Proszę napisać kod demonstrujący problem. – g051051

Odpowiedz

8

Jeżeli klasa, w której dzwonisz Graphics.drawImage(Image, int, int, int, int, ImageObserver) jest ImageObserver korzystając this jako argument dla ImageObserver nie zadziała:

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

Jeśli zmiana rozmiaru obrazu, który nie wymaga ImageObserver (takich jak BufferedImage który już zawiera obraz, który chcesz zmienić rozmiar), a następnie można po prostu oddać ponad 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); 

To powiedziawszy, Zamierzam wrzucić małą wtyczkę do zmiany rozmiaru mojej biblioteki Thumbnailator.

Jeśli wszystko, co jest wymagane jest, aby zmienić rozmiar obrazu, może być dokonane tak proste, jak w poniższym kodzie:

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

Thumbnailator jest na tyle elastyczny, aby zaakceptować BufferedImage s, File S i InputStream s jako wejście .


Widząc swoją edycję, chciałbym zaproponować, aby zmienić klasę Hitter, tak, że będzie wykonać zmianę rozmiaru obrazu w konstruktorze.

Ponieważ jesteś wywołanie metody drawHitter na każdym połączeniu z Applet.drawImage, operacja zmiany rozmiaru za pomocą Graphics.drawImage jest nazywany wiele razy, nawet gdy hitterWidth i hitterHeight są na wszystkich zamiarów i celów, stałe.

Zmiana rozmiaru obiektu Image z wyprzedzeniem i rysowanie tego obrazu o zmienionym rozmiarze w metodzie drawHitter będzie bardziej wydajne.