Draw image on AWT Canvas

前端 未结 3 1061
醉梦人生
醉梦人生 2021-01-24 23:24

I\'m trying to draw an image on an AWT Canvas.

The only line I\'ve got this far is:

Canvas canvas = new Canvas();

I\'m not sure which m

3条回答
  •  滥情空心
    2021-01-25 00:02

    You have several ways to do this. If you don't want to create a subclass of Canvas, add this line (where img is a subclass java.awt.Image):

    canvas.getGraphics().drawImage(img, 0,0, null);
    

    This will draw your Image to position 0,0 on the Canvas. If you create a subclass of canvas, overwrite the paint-Method:

    public void paint (Graphics g) {
        g.drawImage(img, 0,0, null);
    }
    

提交回复
热议问题