0

After I start my applet every component is drawn alright, besides my background image that is drawn with about a half second delay. I deleted my thread thinking it's maybe the cause of my problem, but it's not, so i didn't include it here.... I use Double Buffering, because I would have flickering of my components that are repainted by thread. I tried to provide as little code as possible....

public class balg extends Applet implements Runnable {


   private Image i;
   private Graphics doubleG;
   URL url;
   Image city;  //background image


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

      try{
          url = getDocumentBase();
      }catch(Exception e){

      }
      city = getImage(url , "multiplen/images/SPACE.png");
   }


   public void start(){

        Thread thread = new Thread(this);
        thread.start();

   }

   public void run(){

     // here goes the repiant();

   }

   public void stop(){


   }

   public void destroy(){


   }

   @Override
   public void update(Graphics g) {
      if(i == null){
           i = createImage(this.getSize().width, this.getSize().height);
           doubleG = i.getGraphics();
      }

      doubleG.setColor(getBackground());
      doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);

      doubleG.setColor(getForeground());
      paint(doubleG);

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

   public void paint(Graphics g){

      g.drawImage(city,(int) 800 , 0 , this); // it's drawn here

      String s = "15";  
      g.setColor(Color.BLACK);
      g.drawString(s, getWidth() - 150, 50);    
   }

 }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
quadrar
  • 5
  • 3
  • I see you are declaring a new `Graphics` object and passing into the paint method. The update method already has a `Graphics g` object passed in. Have you tried using that object instead of your declared `doubleG` variable to draw your components? – Orin Jun 29 '16 at 20:06
  • Something that might help you debug, put print statements before (and after) each `drawImage` and `start` call. What do you see? Alternatively, use the debugger and step through to make sure that your code is running in the correct order. – meepzh Jun 29 '16 at 20:56
  • I'd 1) move the code from `update` into the `init` method. 2) remove the overridden `update` method. 3) change `public void paint(Graphics g){` to `public void paint(Graphics g){ super.paint(g);` .. – Andrew Thompson Jun 30 '16 at 06:08
  • 1) Why code an applet? If it is due to the teacher specifying it, 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) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Jun 30 '16 at 06:09
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jun 30 '16 at 06:09

1 Answers1

1

It takes that much time to read the image, about 100-200 ms.

gpasch
  • 2,672
  • 3
  • 10
  • 12