title: Java, how to draw constantly changing graphics
body: <p>Have not done this before, so obviously I suck at it. Here 64 pixels around current mouse position get drawn little bigger on a form. Problem is, that it's 'kind of' to slow, and I have no idea where to start fixing.</p>
  
  <p>Besides that, I made a thread, that constantly calls update graphics when it's finished and a little fps like text, to show really how fast things are drawn.</p>
  
  <p>Image example: (Image is from letter 'a' in Eclipse)</p>
  
  <p><img src="https://i.stack.imgur.com/i0OQQ.png" alt="alt text"></p>
  
  <p>Code example :</p>
  
  <pre><code>@SuppressWarnings("serial")
  public static class AwtZoom extends Frame {
      private BufferedImage image;
      private long timeRef = new Date().getTime();
      Robot robot = null;
  
      public AwtZoom() {
          super("Image zoom");
          setLocation(new Point(640, 0));
          setSize(400, 400);
          setVisible(true);
          final Ticker t = new Ticker();
  
          this.image = (BufferedImage) (this.createImage(320, 330));
          addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent we) {
                  t.done();
                  dispose();
              }
          });
          try {
              robot = new Robot();
          } catch (AWTException e) {
              e.printStackTrace();
          }
          t.start();
      }
  
      private class Ticker extends Thread {
          public boolean update = true;
          public void done() {
              update = false;
          }
          public void run() {
              try {
  
                  while (update == true) {
                      update(getGraphics());
                      // try {
                      // Thread.sleep(200);
                      // } catch (InterruptedException e) {
                      // e.printStackTrace();
                      // return;
                      // }
                  }
              } catch (Exception e) {
  
                  update=false;
              }
          }
      }
  
      public void update(Graphics g) {
          paint(g);
      }
  
      boolean isdone = true;
      public void paint(Graphics g) {
          if (isdone) {
              isdone=false;
              int step = 40;
              Point p = MouseInfo.getPointerInfo().getLocation();
  
              Graphics2D gc = this.image.createGraphics();
  
              try {
  
                  for (int x = 0; x &lt; 8; x++) {
                      for (int y = 0; y &lt; 8; y++) {
                          gc.setColor(robot.getPixelColor(p.x - 4 + x, p.y
                                  - 4 + y));
                          gc.fillOval(x * step, y * step, step - 3, step - 3);
                          gc.setColor(Color.GRAY);
                          gc.drawOval(x * step, y * step, step - 3, step - 3);
                      }
                  }
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
              gc.dispose();
              isdone = true;
              iter++;
          }
          g.drawImage(image, 40, 45, this);
          g.setColor(Color.black);
          StringBuilder sb = new StringBuilder();
          sb.append(iter)
                  .append(" frames in ")
                  .append((double) (new Date().getTime() - this.timeRef) / 1000)
                  .append("s.");
          g.drawString(sb.toString(), 50, 375);
      }
  
      int iter = 0;
  }
  </code></pre>
  
  <p>Changes made: <br>
  * added "gc.dispose();"<br>
  * added "isdone", so redraw could not be called faster, then it should.<br>
  * added <a href="http://pastebin.com/huD9s6bq" rel="noreferrer">this link</a> to thrashgod source rewrite <br>
  * added <a href="http://pastebin.com/V8mDfUiV" rel="noreferrer">this link</a> to thrashgod source rewrite 2</p>