📄 graphicsexample.java
字号:
tabBackground = (Object[])item.getData();
backItem.setImage((Image)tabBackground[1]);
}
}
dbItem = new ToolItem(toolBar, SWT.CHECK);
dbItem.setText(getResourceString("DoubleBuffer")); //$NON-NLS-1$
dbItem.setImage(loadImage(display, "db.gif")); //$NON-NLS-1$
ToolItem separator = new ToolItem(toolBar, SWT.SEPARATOR);
Composite comp = new Composite(toolBar, SWT.NONE);
GridData data;
GridLayout layout = new GridLayout(1, false);
layout.verticalSpacing = 0;
layout.marginWidth = layout.marginHeight = 3;
comp.setLayout(layout);
timerSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP);
data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
timerSpinner.setLayoutData(data);
Label label = new Label(comp, SWT.NONE);
label.setText(getResourceString("Animation")); //$NON-NLS-1$
data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
label.setLayoutData(data);
timerSpinner.setMaximum(1000);
timerSpinner.setSelection(TIMER);
timerSpinner.setSelection(TIMER);
separator.setControl(comp);
separator.setWidth(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
}
Image createImage(Display display, Color color) {
Image image = new Image(display, 16, 16);
GC gc = new GC(image);
gc.setBackground(color);
Rectangle rect = image.getBounds();
gc.fillRectangle(rect);
if (color.equals(display.getSystemColor(SWT.COLOR_BLACK))) {
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
}
gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
gc.dispose();
return image;
}
void createTabList(Composite parent) {
tabList = new Tree(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
HashSet set = new HashSet();
for (int i = 0; i < tabs.length; i++) {
GraphicsTab tab = tabs[i];
set.add(tab.getCategory());
}
for (Iterator iter = set.iterator(); iter.hasNext();) {
String text = (String) iter.next();
TreeItem item = new TreeItem(tabList, SWT.NONE);
item.setText(text);
}
TreeItem[] items = tabList.getItems();
for (int i = 0; i < items.length; i++) {
TreeItem item = items[i];
for (int j = 0; j < tabs.length; j++) {
GraphicsTab tab = tabs[j];
if (item.getText().equals(tab.getCategory())) {
TreeItem item1 = new TreeItem(item, SWT.NONE);
item1.setText(tab.getText());
item1.setData(tab);
}
}
}
tabList.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
TreeItem item = (TreeItem)event.item;
if (item != null) {
setTab((GraphicsTab)item.getData());
}
}
});
}
GraphicsTab[] createTabs() {
return new GraphicsTab[] {
new LineTab(this),
new StarPolyTab(this),
tab = new IntroTab(this),
new BlackHoleTab(this),
};
}
/**
* Disposes all resources created by the receiver.
*/
public void dispose() {
if (tabs != null) {
for (int i = 0; i < tabs.length; i++) {
GraphicsTab tab = tabs[i];
tab.dispose();
}
}
tabs = null;
if (images != null) {
for (int i = 0; i < images.size(); i++) {
((Image)images.elementAt(i)).dispose();
}
}
images = null;
if (customColor != null) customColor.dispose();
customColor = null;
if (customImage != null) customImage.dispose();
customImage = null;
}
TreeItem findItemByData(TreeItem[] items, Object data) {
for (int i = 0; i < items.length; i++) {
TreeItem item = items[i];
if (item.getData() == data) return item;
item = findItemByData(item.getItems(), data);
if (item != null) return item;
}
return null;
}
/**
* Gets the current tab.
*/
public GraphicsTab getTab() {
return tab;
}
Listener getRedrawListener() {
return redrawListener;
}
/**
* Gets a string from the resource bundle.
* We don't want to crash because of a missing String.
* Returns the key if not found.
*/
static String getResourceString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return key;
} catch (NullPointerException e) {
return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
}
}
static Image loadImage (Display display, Class clazz, String string) {
InputStream stream = clazz.getResourceAsStream (string);
if (stream == null) return null;
Image image = null;
try {
image = new Image (display, stream);
} catch (SWTException ex) {
} finally {
try {
stream.close ();
} catch (IOException ex) {}
}
return image;
}
Image loadImage(Display display, String name) {
Image image = loadImage(display, GraphicsExample.class, name);
if (image != null) images.addElement(image);
return image;
}
void paintBackground(GC gc, Rectangle rect) {
gc.setBackground((Color)tabBackground[0]);
gc.fillRectangle(rect);
}
/**
* Redraws the current tab.
*/
public void redraw() {
canvas.redraw();
}
/**
* Grabs input focus.
*/
public void setFocus() {
tabList.setFocus();
}
/**
* Sets the current tab.
*/
public void setTab(GraphicsTab tab) {
this.tab = tab;
Control[] children = controlPanel.getChildren();
for (int i = 0; i < children.length; i++) {
Control control = children[i];
control.dispose();
}
if (tab != null) {
tab.createControlPanel(controlPanel);
animate = tab.isAnimated();
}
playItem.setEnabled(!animate);
pauseItem.setEnabled(animate);
GridData data = (GridData)controlPanel.getLayoutData();
children = controlPanel.getChildren();
data.exclude = children.length == 0;
controlPanel.setVisible(!data.exclude);
if (data.exclude) {
tabPanel.layout();
} else {
tabPanel.layout(children);
}
if (tab != null) {
TreeItem[] selection = tabList.getSelection();
if (selection.length == 0 || selection[0].getData() != tab) {
TreeItem item = findItemByData(tabList.getItems(), tab);
if (item != null) tabList.setSelection(new TreeItem[]{item});
}
}
canvas.redraw();
}
void startAnimationTimer() {
final Display display = Display.getCurrent();
display.timerExec(timerSpinner.getSelection(), new Runnable() {
public void run() {
if (canvas.isDisposed()) return;
if (animate) {
GraphicsTab tab = getTab();
if (tab != null && tab.isAnimated()) {
Rectangle rect = canvas.getClientArea();
tab.next(rect.width, rect.height);
canvas.redraw();
canvas.update();
}
}
display.timerExec(timerSpinner.getSelection(), this);
}
});
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText(getResourceString("SWTGraphics")); //$NON-NLS-1$
final GraphicsExample example = new GraphicsExample(shell);
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
example.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -