   [java] Is this a nice way to draw graphics?
  I had a lot of trouble to make my graphics to draw and not flicker at the same time. Eventually, I ended up with the following code.Do you guys think this is proper code or is it ugly or overcomplicated?package pong;import java.awt.Color;import java.awt.Graphics;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferStrategy;import javax.swing.JFrame;import javax.swing.Timer;public class Field extends JFrame {    Timer timer;    Ball ball = new Ball(200, 200);    public Field() {   setTitle("Pong");   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   setSize(400, 400);   setVisible(true);   getContentPane().setBackground(Color.WHITE); // I want doublebuffering   this.createBufferStrategy(2);   // A timed mainloop:   new Timer(100, new ActionListener() {  public void actionPerformed(ActionEvent e) { ball.update(); repaint();  }   }).start();    }    @Override    public void paint(Graphics g) {   super.paint(g);   BufferStrategy bf = this.getBufferStrategy();   try {  g = getBufferStrategy().getDrawGraphics();  // erase the old image  g.clearRect(0, 0, getWidth(), getHeight());  // draw the object (just draw some filled elipse on g)  ball.draw(g);   } finally {  // It is best to dispose() a Graphics object when done with it.  g.dispose();   } //I don't really understand this   // Shows the contents of the backbuffer on the screen.   bf.show();   //Tell the System to do the Drawing now, otherwise it can take a few extra ms until   //Drawing is done which looks very jerky   Toolkit.getDefaultToolkit().sync(); // I don't understand this either    }    public void xpaint(Graphics g) {   super.paint(g);   ball.draw(g);    }}
  The general idea is good, since you've seperated updates & rendnering.Couple of things I did notice; you'd probably benefit from using "Active Rendering" rather than relying on swings "paint" mechanism.Passive vs. Active RenderingJava Games: Active RenderingAlso I wouldn't recommend using swing Timers for your gameloop. The "actionPerformed" method is invoked by the AWT Event Dispatch thread, which is really meant for small tasks in swing GUI's. (Basically it's a simple way to keep the GUI synchronized).It would be best to create your own thread for your gameloop. There are different ways to implement it, so check out Concurrency Trail.Also the try..finally block doesn't seem to have any catch statements??Last couple of things, the "dispose()" method of Graphics is to release some of the resource it uses. This allows the garbage collector to recover the memory earlier, and with less effort."Toolkit.getDefaultToolkit().sync()", I belive is to do with V-Sync, but it's platform specific.   