Phương pháp tôi đang cố gắng sử dụng là: phương thức drawImage (hình ảnh, int, int, int, int, ImageObserver) để tôi có thể chia tỷ lệ hình ảnh của mình các ví dụ tôi đã nhìn thấy ImageObserver nên được điều này, nhưng điều này dường như không làm việc (tức là phương pháp duy nhất tôi đã thấy là: drawImage (hình ảnh, int, int, ImageObserver), không biết nếu điều này làm cho một Sự khác biệt).Cách sử dụng ImageObserver trong phương thức đồ họa drawImage()
Đây là lớp học chính của tôi đó là 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;
}
}
Đây là lớp 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;
}
}
Câu hỏi của bạn quá mơ hồ. Vui lòng đăng mã chứng minh sự cố. – g051051