📄 graphicsexample.java
字号:
}/** * Creates an image based on a gradient pattern made up of two colors. * * @param device - The Device * @param color1 - The first color used to create the image * @param color2 - The second color used to create the image * * */static Image createImage(Device device, Color color1, Color color2, int width, int height) { Image image = new Image(device, width, height); GC gc = new GC(image); Rectangle rect = image.getBounds(); Pattern pattern = new Pattern(device, rect.x, rect.y, rect.width - 1, rect.height - 1, color1, color2); gc.setBackgroundPattern(pattern); gc.fillRectangle(rect); gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); gc.dispose(); pattern.dispose(); return image;}/** * Creates an image based on the color provided and returns it. * * @param device - The Device * @param color - The color used to create the image * * */static Image createImage(Device device, Color color) { Image image = new Image(device, 16, 16); GC gc = new GC(image); gc.setBackground(color); Rectangle rect = image.getBounds(); gc.fillRectangle(rect); if (color.equals(device.getSystemColor(SWT.COLOR_BLACK))) { gc.setForeground(device.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); Arrays.sort(tabs, new Comparator() { public int compare(Object tab0, Object tab1) { return ((GraphicsTab)tab0).getText().compareTo(((GraphicsTab)tab1).getText()); } }); HashSet set = new HashSet(); for (int i = 0; i < tabs.length; i++) { GraphicsTab tab = tabs[i]; set.add(tab.getCategory()); } String[] categories = new String[set.size()]; set.toArray(categories); Arrays.sort(categories); for (int i = 0; i < categories.length; i++) { String text = categories[i]; TreeItem item = new TreeItem(tabList, SWT.NONE); item.setText(text); } tabs_in_order = new ArrayList(); 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); tabs_in_order.add(tab); } } } tabList.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { TreeItem item = (TreeItem)event.item; if (item != null) { GraphicsTab gt = (GraphicsTab)item.getData(); if (gt == tab) return; setTab((GraphicsTab)item.getData()); } } });}/** * Creates the multi-line text widget that will contain the tab description. * */void createTabDesc(Composite parent) { tabDesc = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.WRAP | SWT.BORDER); tabDesc.setEditable(false); tabDesc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));}/** * Initializes the GraphicsTab instances that will be contained in GraphicsExample. * */GraphicsTab[] createTabs() { return new GraphicsTab[] { new LineTab(this), new StarPolyTab(this), tab = new IntroTab(this), new BlackHoleTab(this), new AlphaTab(this), new BallTab(this), new CountDownTab(this), new CurvesSWTTab(this), new CurvesTab(this), new CustomFontTab(this), new FontBounceTab(this), new GradientTab(this), new ImageTransformTab(this), new ShapesTab(this), new MazeTab(this), new RGBTab(this), new SpiralTab(this), new CardsTab(this), new LineCapTab(this), new InterpolationTab(this), new PathClippingTab(this), new PathClippingAnimTab(this), new LineStyleTab(this), new LineJoinTab(this), new RegionClippingTab(this), new CustomAlphaTab(this), new TextAntialiasTab(this), new GraphicAntialiasTab(this), new ImageFlipTab(this), new PathTab(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 (resources != null) { for (int i = 0; i < resources.size(); i++) { if (resources.get(i) instanceof Resource) { ((Resource)resources.get(i)).dispose(); } } } resources = null; if (backMenu != null) { backMenu.dispose(); backMenu = 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;}/** * 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 (Device device, Class clazz, String string) { InputStream stream = clazz.getResourceAsStream (string); if (stream == null) return null; Image image = null; try { image = new Image (device, stream); } catch (SWTException ex) { } finally { try { stream.close (); } catch (IOException ex) {} } return image;}Image loadImage(Device device, String name) { Image image = loadImage(device, GraphicsExample.class, name); if (image != null) resources.add(image); return image;}public Shell open(final Display display) { Shell shell = new Shell(display); shell.setText(getResourceString("AdvancedGraphics")); //$NON-NLS-1$ final GraphicsExample example = new GraphicsExample(shell); shell.addListener(SWT.Close, new Listener() { public void handleEvent(Event event) { example.dispose(); } }); shell.open(); return shell;}/** * Redraws the current tab. */public void redraw() { canvas.redraw();}/** * Sets wheter the canvas is double buffered or not. */public void setDoubleBuffered(boolean doubleBuffered) { dbItem.setSelection(doubleBuffered); recreateCanvas();}/** * Grabs input focus. */public void setFocus() { tabList.setFocus();}/** * Sets the current tab. */public void setTab(GraphicsTab tab) { Control[] children = tabControlPanel.getChildren(); for (int i = 0; i < children.length; i++) { Control control = children[i]; control.dispose(); } if (this.tab != null) this.tab.dispose(); this.tab = tab; if (tab != null) { setDoubleBuffered(tab.getDoubleBuffered()); tab.createControlPanel(tabControlPanel); tabDesc.setText(tab.getDescription()); } else { tabDesc.setText(""); } FormData data = (FormData)tabControlPanel.getLayoutData(); children = tabControlPanel.getChildren(); if (children.length != 0) { data.top = null; } else { data.top = new FormAttachment(100, -MARGIN); } parent.layout(true, true); 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();}/** * Starts the animation if the animate flag is set. */void startAnimationTimer() { final Display display = parent.getDisplay(); display.timerExec(TIMER, new Runnable() { public void run() { if (canvas.isDisposed()) return; int timeout = TIMER; GraphicsTab tab = getTab(); if (tab instanceof AnimatedGraphicsTab) { AnimatedGraphicsTab animTab = (AnimatedGraphicsTab) tab; if (animate && animTab.getAnimation()) { Rectangle rect = canvas.getClientArea(); animTab.next(rect.width, rect.height); canvas.redraw(); canvas.update(); } timeout = animTab.getAnimationTime(); } display.timerExec(timeout, this); } });}public static void main(String[] args) { Display display = new Display(); Shell shell = new GraphicsExample().open(display); while (shell != null && !shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -