Skip to content

Instantly share code, notes, and snippets.

@ashbb
Created March 7, 2012 13:33
Show Gist options
  • Select an option

  • Save ashbb/1993148 to your computer and use it in GitHub Desktop.

Select an option

Save ashbb/1993148 to your computer and use it in GitHub Desktop.
Flickering rectangle
require 'java'
require 'swt'
module Swt
include_package 'org.eclipse.swt'
include_package 'org.eclipse.swt.layout'
include_package 'org.eclipse.swt.widgets'
include_package 'org.eclipse.swt.graphics'
include_package 'org.eclipse.swt.events'
end
class Object
include Swt
end
class Rect
def initialize x, y
@x, @y = x, y
end
attr_accessor :x, :y
end
class Anim
def initialize display, cs, &blk
@display, @cs, @blk = display, cs, blk
end
def run
@blk.call
@cs.redraw
@display.timerExec 10, self
end
end
display = Swt::Display.new
shell = Swt::Shell.new display
shell.setSize 200+16, 500+38
cs = Swt::Composite.new shell, Swt::SWT::NONE
cs.setSize 600, 500
def rect_paint_control display, cs, rects
pl = Swt::PaintListener.new
class << pl; self end.
instance_eval do
define_method :paintControl do |e|
gc = e.gc
gc.setAntialias Swt::SWT::ON
gc.setBackground Swt::Color.new(display, 95, 158, 160)
rects.each do |s|
gc.fillRoundRectangle s.x, s.y, 50, 50, 0, 0
end
end
end
cs.addPaintListener pl
end
def anim display, cs, &blk
Anim.new(display, cs, &blk).tap do |a|
display.timerExec 10, a
end
end
rects = [Rect.new(50, 0)]
rect_paint_control display, cs, rects
i = 0
anim display, cs do
rects.each{|r| r.y = i % 500}
i += 1
end
shell.open
while !shell.isDisposed do
display.sleep unless display.readAndDispatch
end
display.dispose
@ashbb
Copy link
Author

ashbb commented Mar 7, 2012

I'm looking for a solution to reduce flicker in screen.
Try out this snippet with your JRuby and SWT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment