0

I am trying to set an image as a background for the applet. whenever the paint method is called, it redraws the image again; and this causes flickering. how can I make the image be drawn only once?

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main extends Applet  {

    Image background;

    @Override
    public void init() {
        setSize(800, 600);

        try {
            System.out.println(getCodeBase());
            background = ImageIO.read(new File("1.jpg"));
        } catch (IOException ex) {
            System.out.println("Error reading the image");
        }
    }



    @Override
    public void paint(Graphics g) {

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


    }
}
Mohamad Shawkey
  • 135
  • 1
  • 11
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 13 '13 at 20:27
  • Switching to JApplet will actually likely fix this, since I believe it does double buffering for you which should stop the flickering. – Cruncher Nov 13 '13 at 20:30
  • 2
    3) Don't attempt to set the size of applets in Java code! The size of an applet is set by the HTML that loads it. 4) remember to call `super.paint(g)` as the first line when overriding functionality. 5) An applet would not be able to access a `File` on the server, and only a trusted applet can load a resource from the user's machine.. – Andrew Thompson Nov 13 '13 at 20:30

2 Answers2

0

Try to use JApplet instead of the old Applet. JApplet should already have double buffering, which decreases flickering.

Akira
  • 4,001
  • 1
  • 16
  • 24
  • I'm pretty sure that all top level containers aren't double buffered, but perhaps you could put an example together that proves me wrong ;) – MadProgrammer Nov 13 '13 at 20:52
0

The reason you are seeing this is because most top level containers are not double buffered (in fact if I recall correctly, AWT components aren't double buffered at all).

This means if you override paint on any top level container, you will see this problem.

A better solution would be to create a custom component, extending from something like JPanel, override its paintComponent method and paint the image there.

You should also make sure that you are calling super.paintXxx to ensure that the Graphics context is prepared correctly for painting.

You can the add this component to something like JApplet or JFrame as you need

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366