How to use ImageObserver in the drawImage () graphical method

The method I'm trying to use is: drawImage (image, int, int, int, int, ImageObserver) method so that I can scale my image, in all the examples I saw, ImageObserver should be this, but this seems to be doesn't work (i.e. the only methods I've seen are: drawImage (image, int, int, ImageObserver), I don't know if this matters).

Here is my main class, which is an 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; } } 

Here is the Hitter class:

 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; } } 
+7
source share
1 answer

If the class in which you call Graphics.drawImage(Image, int, int, int, int, ImageObserver) is ImageObserver using this , because the argument for ImageObserver will not work:

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

If you are ImageObserver image that does not require ImageObserver (for example, BufferedImage , which already contains the image you want to resize), then you can simply pass 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); 

However, I'm going to add a small trial for the Thumbnailator image resizing library.

If all that is required is resizing the image, it can be performed as simply as the following code:

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

The Thumbnailator is flexible enough to accept BufferedImage s, File s, and InputStream as input.


Having seen your editing, I would suggest changing the Hitter class so that it performs image resizing in the constructor.

Since you call the drawHitter method for each call from Applet.drawImage , the Applet.drawImage operation using Graphics.drawImage is called many times, even if hitterWidth and hitterHeight are hitterHeight for all purposes and purposes.

Resizing the Image advance, and drawing this previously drawHitter image in the drawHitter method will be more efficient.

+8
source

All Articles