Java - Canvas flashes when redesigning the screen

Asked

Viewed 197 times

0

I’m making a graphical application with the class Component, but every time I delete the canvas to draw a new frame, Canvas flashes!

The clearBackground function:

public void clearBackground() {
    Graphics g = bs.getDrawGraphics();
    g.drawImage(background, 0, 0, null);
    g.dispose();
    bs.show();
}

background is the white color, but it blinks, and I’ve done everything to solve the problem, but whenever it renders, the Canvas blinks, but sometimes it stays without blinking, and soon after comes back! What can I do to solve the problem?

  • You are drawing inside a Jpainel instead of drawing directly inside a Jframe?

  • No. I only used the add(screen) function, where screen is Canvas. What you mean is to add in the Jframe contentPane?

  • "myFrame.add(myPanel)" where myFrame is a Jframe and myPanel is a Jpanel, then you draw in Jpanel, try this.

1 answer

-1

Mine had the same error so I revised the order of height and width for example:

public static int WIDHT = 240;

public static int HEIGHT = 120; 

public static int SCALE = 3;

public Bufferedimage layer = new Bufferedimage(WIDHTSCALE,HEIGHTSCALE, Bufferedimage.TYPE_INT_RGB);

public Game() { this.setPreferredSize(new Dimension(WIDHTSCALE,HEIGHTSCALE));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.