Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, October 24, 2009

Flicker-free rendering with double buffering in Java

If you draw graphics directly using the paint(Graphics) method in an AWT component such as Canvas, Frame or Panel your rendering will suffer greatly from flickering. The general solution to this is double buffering.

There are many ways to implement double buffering. One way is to not do it and instead use Swing components, such as JFrame and JPanel, that already does double buffering. Use paintComponent(Graphics) and override it the same way as you would with paint(Graphics).

class JPanelWithSmileyFace extends JPanel  {
 public Dimension getPreferredSize() {
  return new Dimension(200, 200);
 }

 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.setColor(Color.YELLOW);
  g.fillOval(10, 10, 180, 180);
  g.setColor(Color.BLACK);
  g.fillOval(90-30, 50, 20, 30);
  g.fillOval(90+30, 50, 20, 30);
  g.drawArc(40, 40, 200-2*40, 200-2*40, -10, -160);
 }
}
Example paintComponent.

public class SmileyFaceDemo {
 public static void main(String[] args) {
  JFrame window = new JFrame();
  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  window.getContentPane().add(new JPanelWithOval());
  window.pack();
  window.setVisible(true);
 }
}
Example main.

Thursday, March 26, 2009

Revert commit in Subversion

How to undo a commit (or uncommit) in Subversion.

The svn revert command can not be used as it only reverts local changes, ie. restores all files to the checked out revision.

Instead, here's how to do it. Use the svn merge command. It usually merges revisions from one repository into another. However, it can be used to do the opposite: unmerging.

svn merge -c X SOURCE merges commit X from SOURCE repository to working copy. If X is negative it will be regarded as an inverse merge and the commit removed instead of added.

So, the following command reverts revision 34 in the working copy.

svn merge -c -34 .

Don't forget to commit.