📄 graphicsexample.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.examples.graphics;import java.io.*;import java.util.*;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.widgets.*;/** * This class is the main class of the graphics application. Various "tabs" are * created and made visible by this class. */public class GraphicsExample { Composite parent; GraphicsTab[] tabs; // tabs to be found in the application GraphicsTab tab; // the current tab GraphicsBackground background; // used to store information about the background ToolBar toolBar; // toolbar that contains backItem and dbItem Tree tabList; // tree structure of tabs Text tabDesc; // multi-line text widget that displays a tab description Sash hSash, vSash; Canvas canvas; Composite tabControlPanel; ToolItem backItem, dbItem; // background, double buffer items Menu backMenu; // background menu item ArrayList resources; // stores resources that will be disposed ArrayList tabs_in_order; // stores GraphicsTabs in the order that they appear in the tree boolean animate = true; // whether animation should happen static boolean advanceGraphics, advanceGraphicsInit; static final int MARGIN = 5; static final int SASH_SPACING = 1; static final int TIMER = 30; static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("examples_graphics"); //$NON-NLS-1$/* * Default constructor is needed so that example launcher can create an instance. */public GraphicsExample() { super();}public GraphicsExample(final Composite parent) { this.parent = parent; resources = new ArrayList(); createControls(parent); setTab(tab); startAnimationTimer();}boolean checkAdvancedGraphics() { if (advanceGraphicsInit) return advanceGraphics; advanceGraphicsInit = true; Display display = parent.getDisplay(); try { Path path = new Path(display); path.dispose(); } catch (SWTException e) { Shell shell = display.getActiveShell(), newShell = null; if (shell == null) shell = newShell = new Shell(display); MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK); dialog.setText(RESOURCE_BUNDLE.getString("Warning")); //$NON-NLS-1$ dialog.setMessage(RESOURCE_BUNDLE.getString("LibNotFound")); //$NON-NLS-1$ dialog.open(); if (newShell != null) newShell.dispose(); return false; } return advanceGraphics = true;}void createControls(final Composite parent) { tabs = createTabs(); createToolBar(parent); createTabList(parent); hSash = new Sash(parent, SWT.HORIZONTAL); createTabDesc(parent); vSash = new Sash(parent, SWT.VERTICAL); createCanvas(parent); createControlPanel(parent); FormData data; FormLayout layout = new FormLayout(); parent.setLayout(layout); data = new FormData(); data.left = new FormAttachment(0, MARGIN); data.top = new FormAttachment(0, MARGIN); data.right = new FormAttachment(100, -MARGIN); toolBar.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, MARGIN); data.top = new FormAttachment(toolBar, MARGIN); data.right = new FormAttachment(vSash, -SASH_SPACING); data.bottom = new FormAttachment(hSash, -SASH_SPACING); tabList.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, MARGIN); int offset = parent.getBounds().height - tabDesc.computeSize(SWT.DEFAULT, tabDesc.getLineHeight() * 10).y; data.top = new FormAttachment(null, offset); data.right = new FormAttachment(vSash, -SASH_SPACING); hSash.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, MARGIN); data.top = new FormAttachment(hSash, SASH_SPACING); data.right = new FormAttachment(vSash, -SASH_SPACING); data.bottom = new FormAttachment(100, -MARGIN); tabDesc.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(null, tabList.computeSize(SWT.DEFAULT, SWT.DEFAULT).x + 50); data.top = new FormAttachment(toolBar, MARGIN); data.bottom = new FormAttachment(100, -MARGIN); vSash.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(vSash, SASH_SPACING); data.top = new FormAttachment(toolBar, MARGIN); data.right = new FormAttachment(100, -MARGIN); data.bottom = new FormAttachment(tabControlPanel); canvas.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(vSash, SASH_SPACING); data.right = new FormAttachment(100, -MARGIN); data.bottom = new FormAttachment(100, -MARGIN); tabControlPanel.setLayoutData(data); vSash.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { Rectangle rect = hSash.getParent().getClientArea(); event.x = Math.min (Math.max (event.x, 60), rect.width - 60); System.out.println(event.x); if (event.detail != SWT.DRAG) { FormData data = (FormData)vSash.getLayoutData(); data.left.offset = event.x; parent.layout(true); animate = true; } else { animate = false; } } }); hSash.addListener (SWT.Selection, new Listener () { public void handleEvent (Event event) { Rectangle rect = vSash.getParent().getClientArea(); event.y = Math.min (Math.max (event.y, tabList.getLocation().y + 60), rect.height - 60); if (event.detail != SWT.DRAG) { FormData data = (FormData)hSash.getLayoutData(); data.top.offset = event.y; parent.layout(true); } } });}void createCanvas(Composite parent) { int style = SWT.NO_BACKGROUND; if (dbItem.getSelection()) style |= SWT.DOUBLE_BUFFERED; canvas = new Canvas(parent, style); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event event) { GC gc = event.gc; Rectangle rect = canvas.getClientArea(); Device device = gc.getDevice(); Pattern pattern = null; if (background.getBgColor1() != null) { if (background.getBgColor2() != null) { // gradient pattern = new Pattern(device, 0, 0, rect.width, rect.height, background.getBgColor1(), background.getBgColor2()); gc.setBackgroundPattern(pattern); } else { // solid color gc.setBackground(background.getBgColor1()); } } else if (background.getBgImage() != null) { // image pattern = new Pattern(device, background.getBgImage()); gc.setBackgroundPattern(pattern); } gc.fillRectangle(rect); GraphicsTab tab = getTab(); if (tab != null) tab.paint(gc, rect.width, rect.height); if (pattern != null) pattern.dispose(); } });}void recreateCanvas() { if (dbItem.getSelection() == ((canvas.getStyle() & SWT.DOUBLE_BUFFERED) != 0)) return; Object data = canvas.getLayoutData(); if (canvas != null) canvas.dispose(); createCanvas(parent); canvas.setLayoutData(data); parent.layout(true, true);}/** * Creates the control panel * @param parent */void createControlPanel(Composite parent) { Group group; tabControlPanel = group = new Group(parent, SWT.NONE); group.setText(getResourceString("Settings")); //$NON-NLS-1$ tabControlPanel.setLayout(new RowLayout());}void createToolBar(final Composite parent) { final Display display = parent.getDisplay(); toolBar = new ToolBar(parent, SWT.FLAT); ToolItem back = new ToolItem(toolBar, SWT.PUSH); back.setText(getResourceString("Back")); //$NON-NLS-1$ back.setImage(loadImage(display, "back.gif")); //$NON-NLS-1$ back.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { int index = tabs_in_order.indexOf(tab) - 1; if (index < 0) index = tabs_in_order.size() - 1; setTab((GraphicsTab)tabs_in_order.get(index)); } }); ToolItem next = new ToolItem(toolBar, SWT.PUSH); next.setText(getResourceString("Next")); //$NON-NLS-1$ next.setImage(loadImage(display, "next.gif")); //$NON-NLS-1$ next.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { int index = (tabs_in_order.indexOf(tab) + 1)%tabs_in_order.size(); setTab((GraphicsTab)tabs_in_order.get(index)); } }); ColorMenu colorMenu = new ColorMenu(); // setup items to be contained in the background menu colorMenu.setColorItems(true); colorMenu.setPatternItems(checkAdvancedGraphics()); colorMenu.setGradientItems(checkAdvancedGraphics()); // create the background menu backMenu = colorMenu.createMenu(parent, new ColorListener() { public void setColor(GraphicsBackground gb) { background = gb; backItem.setImage(gb.getThumbNail()); if (canvas != null) canvas.redraw(); } }); // initialize the background to the first item in the menu background = (GraphicsBackground)backMenu.getItem(0).getData(); // background tool item backItem = new ToolItem(toolBar, SWT.PUSH); backItem.setText(getResourceString("Background")); //$NON-NLS-1$ backItem.setImage(background.getThumbNail()); backItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { if (event.widget == backItem) { final ToolItem toolItem = (ToolItem) event.widget; final ToolBar toolBar = toolItem.getParent(); Rectangle toolItemBounds = toolItem.getBounds(); Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y)); backMenu.setLocation(point.x, point.y + toolItemBounds.height); backMenu.setVisible(true); } } }); // double buffer tool item dbItem = new ToolItem(toolBar, SWT.CHECK); dbItem.setText(getResourceString("DoubleBuffer")); //$NON-NLS-1$ dbItem.setImage(loadImage(display, "db.gif")); //$NON-NLS-1$ dbItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { setDoubleBuffered(dbItem.getSelection()); } });}/** * Creates and returns a thumbnail image. * * @param device * a device * @param name * filename of the image */static Image createThumbnail(Device device, String name) { Image image = new Image(device, name); Rectangle src = image.getBounds(); Image result = null; if (src.width != 16 || src.height != 16) { result = new Image(device, 16, 16); GC gc = new GC(result); Rectangle dest = result.getBounds(); gc.drawImage(image, src.x, src.y, src.width, src.height, dest.x, dest.y, dest.width, dest.height); gc.dispose(); } if (result != null) { image.dispose(); return result; } return image;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -