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, August 6, 2009

Middle clicking and scrolling

Some more Firefox tweaks in about:config.
  • Enable scroll on middle click: general.autoScroll = true
  • Disable load URL on middle click: middlemouse.contentLoadURL = false
  • Disable paste on middle click: middlemouse.paste = false

Monday, June 29, 2009

Store dict in AppEngine Datastore

This post is about Python and Google Appengine.

I want to store a dict/dictionary in a model. There is no native DictProperty. However there are workarounds.
  • Encode it somehow (for example with pickle) and store it as a blob.
  • Use an Expando model and store your key-values with setattr(yourmodel, key, value). You can get your dict later with yourmodel.dynamic_properties(). See also delattr. This seems to trigger encoding bugs for non-ascii keys.
  • Store keys and values separately with two StringListProperty. Combine them to build a dict with dict(zip(keylist, valuelist)).

Saturday, May 16, 2009

Firefox progress bar in address bar

Fission is a great addon/plugin/extension to Firefox that moves the progress/loading bar to the address bar, similar to what Safari has. The color is adjustable. Requires Firefox 3 or better.

Thursday, April 16, 2009

Disable drag and drop in Firefox

I think the drag and drop of images and text in Firefox (especially Firefox 3) is annoying.

It's possible to disable it by setting nglayout.enable_drag_images to false in about:config.

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.

Tuesday, February 10, 2009

Disabling mailto links in Firefox

Set network.protocol-handler.external.mailto to false in about:config to disable mailto links. Works in Firefox 2 and 3.

Saturday, January 24, 2009

Tearing while playing video with mplayer on Ubuntu

I had a slight tearing problem while playing video with mplayer on my Ubuntu 8.10 machine. My machine has a Intel i945 graphics card with the i915 driver. This is how I solved it.

In my case the problem was that mplayer used wrong xvideo adaptor.
$ mplayer -v file.avi
...
using Xvideo port 97 for hw scaling

So it's using port 97.

Running xvinfo displays information about the xv xorg extension. Two adaptors in my case; one bad and one good. Mplayer is unfortunately using the bad one because it always picking number #0.

Relevant parts from xvinfo:
Adaptor #0: "Intel(R) Textured Video"
port base: 97
Adaptor #1: "Intel(R) Video Overlay"
port base: 113

Port 97 is Textured Video and port 113 is Video Overlay. The -vo parameter can be used to tell mplayer which adaptor to use.
mplayer -vo xv:port=113 file.avi

And bam! That's it.