Graphics flashing in JApplet

后端 未结 2 620
执念已碎
执念已碎 2020-12-22 12:05

I know that this has been asked many times before, but every time I visit the question thread and excecute the posed solution, it still doesn\'t work for me.

So befo

2条回答
  •  半阙折子戏
    2020-12-22 12:40

    You need to create a separate image to draw all things on:

    im=createImage(getWidth(), getHeight());
    

    in init.

    Then in your paint:

    Graphics g2=im.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0,0,600,600);
    

    and continue with all the g commands replaced with g2:

    g2.setColor(Color.BLACK);
    g2.fillPolygon(xpoints, ypoints, 5);
    

    then you draw the image on g:

    g.drawImage(im, 0, 0, this);
    

    and you have an update method:

    public void update(Graphics g) { paint(g); }
    

提交回复
热议问题