Kế thừa của Applet trong java

Hiện tại em đang làm 1 game bằng java, hiện tại em chỉ in được những nét vẽ, tạo hình
Ví dụ:
drawLine, drawRect…
không thể nào dùng drawImage đc.

đây là code của em lấy mẫu:

public class StartingClass extends JApplet implements Runnable, KeyListener {
    private Image background;
    private Graphics second;
    private URL base;

    @Override
    public void init() {
        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Game lung tung");
        try {
            base = getCodeBase();
        } catch (Exception e) {
            // TODO: handle exception
        }
        // Image Setups

        background = getImage(base, "res/background.png");
    }

    @Override
    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
        super.stop();
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void run() {
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }

        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, this);
    }
}
1 Like

Mọi người giúp đỡ :sob:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?