OnJava8-Examples/swt/ColorBoxes.java

84 lines
2.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// swt/ColorBoxes.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// SWT translation of Swing ColorBoxes.java.
import swt.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import java.util.concurrent.*;
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
class CBox extends Canvas implements Runnable {
class CBoxPaintListener implements PaintListener {
public void paintControl(PaintEvent e) {
Color color = new Color(e.display, cColor);
e.gc.setBackground(color);
Point size = getSize();
e.gc.fillRectangle(0, 0, size.x, size.y);
color.dispose();
}
}
private static Random rand = new Random();
private static RGB newColor() {
return new RGB(rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255));
}
private int pause;
private RGB cColor = newColor();
public CBox(Composite parent, int pause) {
super(parent, SWT.NONE);
this.pause = pause;
addPaintListener(new CBoxPaintListener());
}
@Override
public void run() {
try {
while(!Thread.interrupted()) {
cColor = newColor();
getDisplay().asyncExec( () -> {
try { redraw(); } catch(SWTException e) {}
// SWTException is OK when the parent
// is terminated from under us.
});
TimeUnit.MILLISECONDS.sleep(pause);
}
} catch(InterruptedException e) {
// Acceptable way to exit
} catch(SWTException e) {
// Acceptable way to exit: our parent
// was terminated from under us.
}
}
}
public class ColorBoxes implements SWTApplication {
private int grid = 12;
private int pause = 50;
@Override
public void createContents(Composite parent) {
GridLayout gridLayout = new GridLayout(grid, true);
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
parent.setLayout(gridLayout);
ExecutorService exec = new DaemonThreadPoolExecutor();
for(int i = 0; i < (grid * grid); i++) {
final CBox cb = new CBox(parent, pause);
cb.setLayoutData(new GridData(GridData.FILL_BOTH));
exec.execute(cb);
}
}
public static void main(String[] args) {
ColorBoxes boxes = new ColorBoxes();
if(args.length > 0)
boxes.grid = new Integer(args[0]);
if(args.length > 1)
boxes.pause = new Integer(args[1]);
SWTConsole.run(boxes, 500, 400);
}
2015-09-07 11:44:36 -06:00
}