shell.setText("SWT Too Many Balls!");
/**
* Used for ball animations.
*/
final TimingSource animationTimer = new SWTTimingSource(display);
Animator.setDefaultTimingSource(animationTimer);
/**
* Used to update the FPS display once a second.
*/
final TimingSource infoTimer = new SWTTimingSource(1, TimeUnit.SECONDS, display);
final GridLayout panelLayout = new GridLayout(2, false);
shell.setLayout(panelLayout);
GridData gridData;
final Composite topPanel = new Composite(shell, SWT.NONE);
gridData = new GridData(SWT.LEFT, SWT.FILL, false, false);
topPanel.setLayoutData(gridData);
final RowLayout topPanelLayout = new RowLayout();
topPanel.setLayout(topPanelLayout);
final Button addBall = new Button(topPanel, SWT.PUSH);
addBall.setText("Add Ball");
addBall.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
addBall();
updateBallCount();
}
});
final Button add10Balls = new Button(topPanel, SWT.PUSH);
add10Balls.setText("Add 10 Balls");
add10Balls.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
addBall();
addBall();
addBall();
addBall();
addBall();
addBall();
addBall();
addBall();
addBall();
addBall();
updateBallCount();
}
});
final Button removeBall = new Button(topPanel, SWT.PUSH);
removeBall.setText("Remove Ball");
removeBall.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
removeBall();
updateBallCount();
}
});
final Button remove10Balls = new Button(topPanel, SWT.PUSH);
remove10Balls.setText("Remove 10 Balls");
remove10Balls.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
removeBall();
removeBall();
removeBall();
removeBall();
removeBall();
removeBall();
removeBall();
removeBall();
removeBall();
removeBall();
updateBallCount();
}
});
f_infoLabel = new Label(shell, SWT.RIGHT);
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
f_infoLabel.setLayoutData(gridData);
updateBallCount();
f_panel = new Canvas(shell, SWT.DOUBLE_BUFFERED);
gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
f_panel.setLayoutData(gridData);
f_panel.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
JRenderer renderer = JRendererFactory.getDefaultRenderer(f_panel, this);
infoTimer.addTickListener(new TickListener() {
@Override
public void timingSourceTick(TimingSource source, long nanoTime) {
updateBallCount();
}
});
animationTimer.init();
infoTimer.init();
shell.setSize(800, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
renderer.getTimingSource().dispose();
renderer.shutdown();
animationTimer.dispose();
infoTimer.dispose();
display.dispose();
}