List of usage examples for org.eclipse.swt.widgets Canvas Canvas
public Canvas(Composite parent, int style)
From source file:Palettes.java
public Palettes() { shell.setLayout(new FillLayout()); PaletteData paletteData = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF); ImageData imageData = new ImageData(100, 100, 24, paletteData); for (int i = 0; i < 100; i++) { // each column. for (int j = 0; j < 100; j++) { // each row. if (j < 30 || (i > 35 && i < 65)) imageData.setPixel(i, j, 0xFF); else/*from ww w. j a v a 2 s .c om*/ imageData.setPixel(i, j, 0xFFFF00); } } // int pixelValue = imageData.getPixel(50, 50); // int redComponent = pixelValue & imageData.palette.redMask; // int greenComponent = pixelValue & imageData.palette.greenMask; // int blueComponent = pixelValue & imageData.palette.blueMask; PaletteData paletteData2 = new PaletteData(new RGB[] { new RGB(0, 0, 255), // blue new RGB(255, 255, 0) // yellow }); ImageData imageData2 = new ImageData(100, 100, 1, paletteData2); for (int i = 0; i < 100; i++) { // each column. for (int j = 0; j < 100; j++) { // each row. if (j < 30 || (i > 35 && i < 65)) imageData2.setPixel(i, j, 0); else imageData2.setPixel(i, j, 1); } } System.out.println(imageData.palette.getRGB(imageData.getPixel(50, 50))); final Image image2 = new Image(display, imageData2); final Image image = new Image(display, imageData); final Canvas canvas = new Canvas(shell, SWT.NULL); canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawImage(image2, 0, 0); } }); shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:FocusTraversal.java
private void init() { shell.setLayout(new RowLayout()); Composite composite1 = new Composite(shell, SWT.BORDER); composite1.setLayout(new RowLayout()); composite1.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); Button button1 = new Button(composite1, SWT.PUSH); button1.setText("Button1"); List list = new List(composite1, SWT.MULTI | SWT.BORDER); list.setItems(new String[] { "Item-1", "Item-2", "Item-3" }); Button radioButton1 = new Button(composite1, SWT.RADIO); radioButton1.setText("radio-1"); Button radioButton2 = new Button(composite1, SWT.RADIO); radioButton2.setText("radio-2"); Composite composite2 = new Composite(shell, SWT.BORDER); composite2.setLayout(new RowLayout()); composite2.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); Button button2 = new Button(composite2, SWT.PUSH); button2.setText("Button2"); final Canvas canvas = new Canvas(composite2, SWT.NULL); canvas.setSize(50, 50);/*from ww w . ja v a2s .c om*/ canvas.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); Combo combo = new Combo(composite2, SWT.DROP_DOWN); combo.add("combo"); combo.select(0); canvas.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { GC gc = new GC(canvas); // Erase background first. Rectangle rect = canvas.getClientArea(); gc.fillRectangle(rect.x, rect.y, rect.width, rect.height); Font font = new Font(display, "Arial", 32, SWT.BOLD); gc.setFont(font); gc.drawString("" + e.character, 15, 10); gc.dispose(); font.dispose(); } public void keyReleased(KeyEvent e) { } }); canvas.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) e.doit = true; } }); composite1.setTabList(new Control[] { button1, list }); composite2.setTabList(new Control[] { button2, canvas, combo }); shell.setTabList(new Control[] { composite2, composite1 }); }
From source file:org.eclipse.swt.snippets.Snippet365.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet365 - Transparent Background"); RowLayout layout = new RowLayout(SWT.VERTICAL); layout.spacing = 20;//from w w w . j a va2 s . com layout.marginWidth = 10; layout.marginHeight = 10; shell.setLayout(layout); // Standard color background for Shell // shell.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // Gradient background for Shell shell.addListener(SWT.Resize, event -> { Rectangle rect = shell.getClientArea(); Image newImage = new Image(display, Math.max(1, rect.width), 1); GC gc = new GC(newImage); gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false); gc.dispose(); shell.setBackgroundImage(newImage); if (oldImage != null) oldImage.dispose(); oldImage = newImage; }); // Transparent buttonCheckBox = new Button(shell, SWT.CHECK | SWT.None); buttonCheckBox.setText("SET TRANSPARENT"); buttonCheckBox.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); buttonCheckBox.setSelection(false); buttonCheckBox.addSelectionListener(widgetSelectedAdapter(e -> { boolean transparent = ((Button) e.getSource()).getSelection(); if (transparent) { // ContainerGroup containerGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); canvas.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); composite.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); tabFolder.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); for (TabItem item : tabFolder.getItems()) { item.getControl().setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); } // Native nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); toolBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); coolBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); label.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); link.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); scale.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); radio.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); check.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); group.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); sash.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); slider.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); list.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); // Custom customGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); cLabel.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); cTab.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT)); gradientCTab.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT)); sashForm.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT)); for (Control control : sashForm.getChildren()) { control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT)); } // Default push.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); combo.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); progressBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); dateTime.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); ccombo.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); text.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); styledText.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); expandBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); table.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); tree.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); // ItemGroup itemGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT)); } else { // Native nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); toolBar.setBackground(null); coolBar.setBackground(null); label.setBackground(null); link.setBackground(null); scale.setBackground(null); RGB rgb = display.getSystemColor(SWT.COLOR_CYAN).getRGB(); radio.setBackground(new Color(display, new RGBA(rgb.red, rgb.blue, rgb.green, 255))); check.setBackgroundImage(getBackgroundImage(display)); group.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); sash.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN)); slider.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); list.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); text.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // ContainerGroup containerGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); canvas.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); composite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); tabFolder.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); for (TabItem item : tabFolder.getItems()) { item.getControl().setBackground(display.getSystemColor(SWT.COLOR_CYAN)); } // Custom customGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); cLabel.setBackground((Color) null); styledText.setBackground((Color) null); sashForm.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); for (Control control : sashForm.getChildren()) { control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); } cTab.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gradientCTab.setBackground(new Color[] { display.getSystemColor(SWT.COLOR_RED), display.getSystemColor(SWT.COLOR_WHITE) }, new int[] { 90 }, true); // Default defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); push.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); combo.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); ccombo.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); dateTime.setBackground(null); progressBar.setBackground(null); expandBar.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); table.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); tree.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // ItemGroup itemGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); } })); // ContainerGroup containerGroup = new Composite(shell, SWT.NONE); containerGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); containerGroup.setToolTipText("CONTAINER"); layout = new RowLayout(); layout.spacing = 20; containerGroup.setLayout(layout); // Native nativeGroup = new Composite(shell, SWT.NONE); nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); nativeGroup.setToolTipText("NATIVE"); layout = new RowLayout(); layout.spacing = 20; nativeGroup.setLayout(layout); // Custom customGroup = new Composite(shell, SWT.NONE); customGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); customGroup.setToolTipText("CUSTOM"); layout = new RowLayout(); layout.spacing = 20; customGroup.setLayout(layout); // AsDesigned defaultBackgroundGroup = new Composite(shell, SWT.NONE); defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); defaultBackgroundGroup.setToolTipText("Default Background"); layout = new RowLayout(); layout.spacing = 20; defaultBackgroundGroup.setLayout(layout); // ItemGroup itemGroup = new Composite(shell, SWT.NONE); itemGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); itemGroup.setToolTipText("ITEM"); layout = new RowLayout(); layout.spacing = 20; itemGroup.setLayout(layout); // Label label = new Label(nativeGroup, SWT.NONE); label.setText("Label"); // Radio button radio = new Button(nativeGroup, SWT.RADIO); radio.setText("Radio Button"); radio.setSelection(true); radio.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // Checkbox button with image check = new Button(nativeGroup, SWT.CHECK); check.setText("CheckBox Image"); check.setSelection(true); check.setBackgroundImage(getBackgroundImage(display)); // Push Button push = new Button(defaultBackgroundGroup, SWT.PUSH); push.setText("Push Button"); // Toolbar toolBar = new ToolBar(nativeGroup, SWT.FLAT); toolBar.pack(); ToolItem item = new ToolItem(toolBar, SWT.PUSH); item.setText("ToolBar_Item"); // Coolbar coolBar = new CoolBar(nativeGroup, SWT.BORDER); for (int i = 0; i < 2; i++) { CoolItem item2 = new CoolItem(coolBar, SWT.NONE); Button button = new Button(coolBar, SWT.PUSH); button.setText("Button " + i); Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT); item2.setPreferredSize(item2.computeSize(size.x, size.y)); item2.setControl(button); } // Scale scale = new Scale(nativeGroup, SWT.None); scale.setMaximum(100); scale.setSelection(20); // Link link = new Link(nativeGroup, SWT.NONE); link.setText("<a>Sample link</a>"); // List list = new List(nativeGroup, SWT.BORDER); list.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); list.add("List_one"); list.add("List_two"); list.add("List_three"); list.add("List_four"); // Canvas canvas = new Canvas(containerGroup, SWT.NONE); canvas.setToolTipText("Canvas"); canvas.addPaintListener(e -> { GC gc = e.gc; gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); gc.drawRectangle(e.x + 1, e.y + 1, e.width - 2, e.height - 2); gc.drawArc(2, 2, e.width - 4, e.height - 4, 0, 360); }); // Composite composite = new Composite(containerGroup, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); composite.setToolTipText("Composite"); // TabFolder tabFolder = new TabFolder(containerGroup, SWT.BORDER); tabFolder.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); for (int i = 0; i < 2; i++) { TabItem tabItem = new TabItem(tabFolder, SWT.NONE); tabItem.setText("TabItem " + i); Label label = new Label(tabFolder, SWT.PUSH); label.setText("Page " + i); tabItem.setControl(label); tabItem.getControl().setBackground(display.getSystemColor(SWT.COLOR_CYAN)); } tabFolder.pack(); // Group group = new Group(containerGroup, SWT.NONE); group.setText("Group"); // Sash sash = new Sash(containerGroup, SWT.HORIZONTAL | SWT.BORDER); sash.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN)); sash.setLayoutData(new RowData(100, 100)); sash.setToolTipText("Sash"); // SashForm sashForm = new SashForm(containerGroup, SWT.HORIZONTAL | SWT.BORDER); Label leftLabel = new Label(sashForm, SWT.NONE); leftLabel.setText("SashForm\nLeft\n...\n...\n...\n...\n..."); Label rightLabel = new Label(sashForm, SWT.NONE); rightLabel.setText("SashForm\nRight\n...\n...\n...\n...\n..."); // DateTime dateTime = new DateTime(defaultBackgroundGroup, SWT.NONE); dateTime.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // Text text = new Text(nativeGroup, SWT.BORDER); text.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); text.setText("text"); // ProgressBar progressBar = new ProgressBar(defaultBackgroundGroup, SWT.NONE); progressBar.setMaximum(100); progressBar.setSelection(80); // Combo combo = new Combo(defaultBackgroundGroup, SWT.BORDER); combo.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); combo.add("combo"); combo.setText("combo"); // Slider slider = new Slider(nativeGroup, SWT.HORIZONTAL | SWT.BORDER); slider.setSelection(20); slider.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // CCombo ccombo = new CCombo(defaultBackgroundGroup, SWT.BORDER); ccombo.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); ccombo.add("ccombo"); ccombo.setText("ccombo"); // CLable cLabel = new CLabel(customGroup, SWT.NONE); cLabel.setText("CLabel"); // Text styledText = new StyledText(customGroup, SWT.BORDER); styledText.setFont(new Font(display, "Tahoma", 18, SWT.BOLD | SWT.ITALIC)); styledText.setForeground(display.getSystemColor(SWT.COLOR_DARK_BLUE)); styledText.setText("Styled Text"); styledText.append("\n"); styledText.append("Example_string"); styledText.append("\n"); styledText.append("One_Two"); styledText.append("\n"); styledText.append("Two_Three"); // CTabFolder cTab = new CTabFolder(containerGroup, SWT.BORDER); for (int i = 0; i < 2; i++) { CTabItem cTabItem = new CTabItem(cTab, SWT.CLOSE, i); cTabItem.setText("CTabItem " + (i + 1)); } cTab.setSelection(0); // Gradient CTabFolder gradientCTab = new CTabFolder(customGroup, SWT.BORDER); gradientCTab.setBackground( new Color[] { display.getSystemColor(SWT.COLOR_WHITE), display.getSystemColor(SWT.COLOR_RED) }, new int[] { 90 }, true); for (int i = 0; i < 2; i++) { CTabItem cTabItem = new CTabItem(gradientCTab, SWT.CLOSE, i); cTabItem.setText("CTabItem " + (i + 1)); } gradientCTab.setSelection(0); // Table table = new Table(itemGroup, SWT.V_SCROLL); table.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); table.setLinesVisible(true); table.setHeaderVisible(true); TableItem tableItem = new TableItem(table, SWT.NONE); tableItem.setText("TableItem - One"); tableItem = new TableItem(table, SWT.NONE); tableItem.setText("TableItem - Two"); // Tree tree = new Tree(itemGroup, SWT.NONE); TreeItem treeItem = new TreeItem(tree, SWT.NONE); treeItem.setText("Parent"); TreeItem childItem = new TreeItem(treeItem, SWT.NONE); childItem.setText("Child1"); childItem = new TreeItem(treeItem, SWT.NONE); childItem.setText("Child2"); treeItem.setExpanded(true); tree.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); // ExpandBar expandBar = new ExpandBar(itemGroup, SWT.V_SCROLL); expandBar.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); for (int i = 1; i <= 2; i++) { ExpandItem item1 = new ExpandItem(expandBar, SWT.NONE, 0); item1.setText("Expand_Bar_Entry " + i); item1.setExpanded(true); item1.setHeight(20); } shell.open(); shell.pack(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:ImageViewer.java
public ImageViewer() { shell.setText("Image viewer"); shell.setLayout(new GridLayout(1, true)); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem itemOpen = new ToolItem(toolBar, SWT.PUSH); itemOpen.setText("Open"); itemOpen.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { FileDialog dialog = new FileDialog(shell, SWT.OPEN); String file = dialog.open(); if (file != null) { if (image != null) image.dispose();/*ww w .ja v a 2 s. c om*/ image = null; try { image = new Image(display, file); } catch (RuntimeException e) { // e.printStackTrace(); } if (image != null) { fileName = file; } else { System.err.println("Failed to load image from file: " + file); } canvas.redraw(); } } }); ToolItem itemPrintPreview = new ToolItem(toolBar, SWT.PUSH); itemPrintPreview.setText("Preview"); itemPrintPreview.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { ImagePrintPreviewDialog dialog = new ImagePrintPreviewDialog(ImageViewer.this); dialog.open(); } }); ToolItem itemPrint = new ToolItem(toolBar, SWT.PUSH); itemPrint.setText("Print"); itemPrint.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { print(); } }); canvas = new Canvas(shell, SWT.BORDER); canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); canvas.setLayoutData(new GridData(GridData.FILL_BOTH)); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { if (image == null) { e.gc.drawString("No image", 0, 0); } else { e.gc.drawImage(image, 0, 0); } } }); image = new Image(display, "java2s.gif"); fileName = "scene.jpg"; shell.setSize(500, 400); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:SWTImageViewer.java
public SWTImageViewer() { shell.setText("Image viewer"); shell.setLayout(new GridLayout(1, true)); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem itemOpen = new ToolItem(toolBar, SWT.PUSH); itemOpen.setText("Open"); itemOpen.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { FileDialog dialog = new FileDialog(shell, SWT.OPEN); String file = dialog.open(); if (file != null) { if (image != null) image.dispose();//from w w w .jav a2 s .co m image = null; try { image = new Image(display, file); } catch (RuntimeException e) { // e.printStackTrace(); } if (image != null) { fileName = file; } else { System.err.println("Failed to load image from file: " + file); } canvas.redraw(); } } }); ToolItem itemPrintPreview = new ToolItem(toolBar, SWT.PUSH); itemPrintPreview.setText("Preview"); itemPrintPreview.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { ImagePrintPreviewDialog dialog = new ImagePrintPreviewDialog(ImageViewer.this); dialog.open(); } }); ToolItem itemPrint = new ToolItem(toolBar, SWT.PUSH); itemPrint.setText("Print"); itemPrint.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { print(); } }); canvas = new Canvas(shell, SWT.BORDER); canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); canvas.setLayoutData(new GridData(GridData.FILL_BOTH)); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { if (image == null) { e.gc.drawString("No image", 0, 0); } else { e.gc.drawImage(image, 0, 0); } } }); image = new Image(display, "java2s.gif"); fileName = "scene.jpg"; shell.setSize(500, 400); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:BrowserExample.java
/** * Creates an instance of a ControlExample embedded inside the supplied * parent Composite./*from w w w . j a v a 2s .c o m*/ * * @param parent * the container of the example */ public BrowserExample(Composite parent) { final Display display = parent.getDisplay(); FormLayout layout = new FormLayout(); parent.setLayout(layout); ToolBar toolbar = new ToolBar(parent, SWT.NONE); final ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH); itemBack.setText(getResourceString("Back")); final ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH); itemForward.setText(getResourceString("Forward")); final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); itemStop.setText(getResourceString("Stop")); final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); itemRefresh.setText(getResourceString("Refresh")); final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); itemGo.setText(getResourceString("Go")); location = new Text(parent, SWT.BORDER); images = new Image[] { new Image(display, "java2s.gif") }; final Canvas canvas = new Canvas(parent, SWT.NO_BACKGROUND); final Rectangle rect = images[0].getBounds(); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { Point pt = canvas.getSize(); e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y); } }); canvas.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event e) { browser.setUrl(getResourceString("Startup")); } }); display.asyncExec(new Runnable() { public void run() { if (canvas.isDisposed()) return; if (busy) { index++; if (index == images.length) index = 0; canvas.redraw(); } display.timerExec(150, this); } }); final Label status = new Label(parent, SWT.NONE); final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE); FormData data = new FormData(); data.top = new FormAttachment(0, 5); toolbar.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.top = new FormAttachment(canvas, 5, SWT.DEFAULT); data.bottom = new FormAttachment(status, -5, SWT.DEFAULT); try { browser = new Browser(parent, SWT.NONE); browser.setLayoutData(data); } catch (SWTError e) { /* Browser widget could not be instantiated */ Label label = new Label(parent, SWT.CENTER | SWT.WRAP); label.setText(getResourceString("BrowserNotCreated")); label.setLayoutData(data); } data = new FormData(); data.width = 24; data.height = 24; data.top = new FormAttachment(0, 5); data.right = new FormAttachment(100, -5); canvas.setLayoutData(data); data = new FormData(); data.top = new FormAttachment(toolbar, 0, SWT.TOP); data.left = new FormAttachment(toolbar, 5, SWT.RIGHT); data.right = new FormAttachment(canvas, -5, SWT.DEFAULT); location.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, 5); data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT); data.bottom = new FormAttachment(100, -5); status.setLayoutData(data); data = new FormData(); data.right = new FormAttachment(100, -5); data.bottom = new FormAttachment(100, -5); progressBar.setLayoutData(data); if (browser != null) { itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); Listener listener = new Listener() { public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; if (item == itemBack) browser.back(); else if (item == itemForward) browser.forward(); else if (item == itemStop) browser.stop(); else if (item == itemRefresh) browser.refresh(); else if (item == itemGo) browser.setUrl(location.getText()); } }; browser.addLocationListener(new LocationListener() { public void changed(LocationEvent event) { busy = true; if (event.top) location.setText(event.location); } public void changing(LocationEvent event) { } }); browser.addProgressListener(new ProgressListener() { public void changed(ProgressEvent event) { if (event.total == 0) return; int ratio = event.current * 100 / event.total; progressBar.setSelection(ratio); busy = event.current != event.total; if (!busy) { index = 0; canvas.redraw(); } } public void completed(ProgressEvent event) { itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); progressBar.setSelection(0); busy = false; index = 0; canvas.redraw(); } }); browser.addStatusTextListener(new StatusTextListener() { public void changed(StatusTextEvent event) { status.setText(event.text); } }); if (parent instanceof Shell) { final Shell shell = (Shell) parent; browser.addTitleListener(new TitleListener() { public void changed(TitleEvent event) { shell.setText(event.title + " - " + getResourceString("window.title")); } }); } itemBack.addListener(SWT.Selection, listener); itemForward.addListener(SWT.Selection, listener); itemStop.addListener(SWT.Selection, listener); itemRefresh.addListener(SWT.Selection, listener); itemGo.addListener(SWT.Selection, listener); location.addListener(SWT.DefaultSelection, new Listener() { public void handleEvent(Event e) { browser.setUrl(location.getText()); } }); initialize(display, browser); browser.setUrl(getResourceString("Startup")); } }
From source file:org.eclipse.swt.examples.controlexample.CanvasTab.java
/** * Creates the "Example" widgets.// w w w. j a va 2 s.c o m */ @Override void createExampleWidgets() { /* Compute the widget style */ int style = getDefaultStyle(); if (horizontalButton.getSelection()) style |= SWT.H_SCROLL; if (verticalButton.getSelection()) style |= SWT.V_SCROLL; if (borderButton.getSelection()) style |= SWT.BORDER; if (noBackgroundButton.getSelection()) style |= SWT.NO_BACKGROUND; if (noFocusButton.getSelection()) style |= SWT.NO_FOCUS; if (noMergePaintsButton.getSelection()) style |= SWT.NO_MERGE_PAINTS; if (noRedrawResizeButton.getSelection()) style |= SWT.NO_REDRAW_RESIZE; if (doubleBufferedButton.getSelection()) style |= SWT.DOUBLE_BUFFERED; /* Create the example widgets */ paintCount = 0; cx = 0; cy = 0; canvas = new Canvas(canvasGroup, style); canvas.addPaintListener(e -> { paintCount++; GC gc = e.gc; if (fillDamageButton.getSelection()) { Color color = e.display.getSystemColor(colors[paintCount % colors.length]); gc.setBackground(color); gc.fillRectangle(e.x, e.y, e.width, e.height); } Point size = canvas.getSize(); gc.drawArc(cx + 1, cy + 1, size.x - 2, size.y - 2, 0, 360); gc.drawRectangle(cx + (size.x - 10) / 2, cy + (size.y - 10) / 2, 10, 10); Point extent = gc.textExtent(canvasString); gc.drawString(canvasString, cx + (size.x - extent.x) / 2, cy - extent.y + (size.y - 10) / 2, true); }); canvas.addControlListener(ControlListener.controlResizedAdapter(e -> { Point size = canvas.getSize(); maxX = size.x * 3 / 2; maxY = size.y * 3 / 2; resizeScrollBars(); })); ScrollBar bar = canvas.getHorizontalBar(); if (bar != null) { hookListeners(bar); bar.addSelectionListener(widgetSelectedAdapter(event -> scrollHorizontal((ScrollBar) event.widget))); } bar = canvas.getVerticalBar(); if (bar != null) { hookListeners(bar); bar.addSelectionListener(widgetSelectedAdapter(event -> scrollVertical((ScrollBar) event.widget))); } }
From source file:org.eclipse.swt.examples.graphics.GradientDialog.java
/** * Creates the controls of the dialog.//www . ja v a 2 s. c o m * */ public void createDialogControls(final Shell parent) { final Display display = parent.getDisplay(); // message Label message = new Label(parent, SWT.NONE); message.setText(GraphicsExample.getResourceString("GradientDlgMsg")); GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.horizontalSpan = 2; message.setLayoutData(gridData); // default colors are white and black if (rgb1 == null || rgb2 == null) { rgb1 = display.getSystemColor(SWT.COLOR_WHITE).getRGB(); rgb2 = display.getSystemColor(SWT.COLOR_BLACK).getRGB(); } // canvas canvas = new Canvas(parent, SWT.NONE); gridData = new GridData(GridData.FILL_BOTH); gridData.widthHint = 200; gridData.heightHint = 100; canvas.setLayoutData(gridData); canvas.addListener(SWT.Paint, e -> { Image preview = null; Point size = canvas.getSize(); Color color1 = new Color(display, rgb1); Color color2 = new Color(display, rgb2); preview = GraphicsExample.createImage(display, color1, color2, size.x, size.y); if (preview != null) { e.gc.drawImage(preview, 0, 0); } preview.dispose(); color1.dispose(); color2.dispose(); }); // composite used for both color buttons Composite colorButtonComp = new Composite(parent, SWT.NONE); // layout buttons RowLayout layout = new RowLayout(); layout.type = SWT.VERTICAL; layout.pack = false; colorButtonComp.setLayout(layout); // position composite gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); colorButtonComp.setLayoutData(gridData); ColorMenu colorMenu = new ColorMenu(); // color controls: first color colorButton1 = new Button(colorButtonComp, SWT.PUSH); colorButton1.setText(GraphicsExample.getResourceString("GradientDlgButton1")); Color color1 = new Color(display, rgb1); Image img1 = GraphicsExample.createImage(display, color1); color1.dispose(); colorButton1.setImage(img1); resources.add(img1); menu1 = colorMenu.createMenu(parent.getParent(), gb -> { rgb1 = gb.getBgColor1().getRGB(); colorButton1.setImage(gb.getThumbNail()); if (canvas != null) canvas.redraw(); }); colorButton1.addListener(SWT.Selection, event -> { final Button button = (Button) event.widget; final Composite parent1 = button.getParent(); Rectangle bounds = button.getBounds(); Point point = parent1.toDisplay(new Point(bounds.x, bounds.y)); menu1.setLocation(point.x, point.y + bounds.height); menu1.setVisible(true); }); // color controls: second color colorButton2 = new Button(colorButtonComp, SWT.PUSH); colorButton2.setText(GraphicsExample.getResourceString("GradientDlgButton2")); Color color2 = new Color(display, rgb2); Image img2 = GraphicsExample.createImage(display, color2); color2.dispose(); colorButton2.setImage(img2); resources.add(img2); menu2 = colorMenu.createMenu(parent.getParent(), gb -> { rgb2 = gb.getBgColor1().getRGB(); colorButton2.setImage(gb.getThumbNail()); if (canvas != null) canvas.redraw(); }); colorButton2.addListener(SWT.Selection, event -> { final Button button = (Button) event.widget; final Composite parent1 = button.getParent(); Rectangle bounds = button.getBounds(); Point point = parent1.toDisplay(new Point(bounds.x, bounds.y)); menu2.setLocation(point.x, point.y + bounds.height); menu2.setVisible(true); }); // composite used for ok and cancel buttons Composite okCancelComp = new Composite(parent, SWT.NONE); // layout buttons RowLayout rowLayout = new RowLayout(); rowLayout.pack = false; rowLayout.marginTop = 5; okCancelComp.setLayout(rowLayout); // position composite gridData = new GridData(GridData.HORIZONTAL_ALIGN_END); gridData.horizontalSpan = 2; okCancelComp.setLayoutData(gridData); // OK button okButton = new Button(okCancelComp, SWT.PUSH); okButton.setText("&OK"); okButton.addListener(SWT.Selection, event -> { returnVal = SWT.OK; parent.close(); }); // cancel button cancelButton = new Button(okCancelComp, SWT.PUSH); cancelButton.setText("&Cancel"); cancelButton.addListener(SWT.Selection, event -> parent.close()); }
From source file:GraphicsExample.java
void createCanvas(Composite parent) { canvas = new Canvas(parent, SWT.NO_BACKGROUND); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event event) { GC gc;/* w ww . ja v a 2 s .c o m*/ Rectangle rect = canvas.getClientArea(); Image buffer = null; if (dbItem.getSelection()) { buffer = new Image(canvas.getDisplay(), rect); gc = new GC(buffer); } else { gc = event.gc; } paintBackground(gc, rect); GraphicsTab tab = getTab(); if (tab != null) tab.paint(gc, rect.width, rect.height); if (gc != event.gc) gc.dispose(); if (buffer != null) { event.gc.drawImage(buffer, 0, 0); buffer.dispose(); } } }); }
From source file:org.eclipse.swt.snippets.Snippet341.java
static void capture(GLCanvas glCanvas) { final int PAD = 4; Display display = glCanvas.getDisplay(); Rectangle bounds = glCanvas.getBounds(); int size = bounds.width * PAD * bounds.height; ByteBuffer buffer = ByteBuffer.allocateDirect(size); GL11.glReadPixels(0, 0, bounds.width, bounds.height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); byte[] bytes = new byte[size]; buffer.get(bytes);/*from w w w .j av a 2s . co m*/ /* * In OpenGL (0,0) is at the bottom-left corner, and y values ascend in the upward * direction. This is opposite to SWT which defines (0,0) to be at the top-left * corner with y values ascendingin the downwards direction. Re-order the OpenGL- * provided bytes to SWT's expected order so that the Image will not appear inverted. */ byte[] temp = new byte[bytes.length]; for (int i = 0; i < bytes.length; i += bounds.width * PAD) { System.arraycopy(bytes, bytes.length - i - bounds.width * PAD, temp, i, bounds.width * PAD); } bytes = temp; ImageData data = new ImageData(bounds.width, bounds.height, 32, new PaletteData(0xFF000000, 0xFF0000, 0xFF00), PAD, bytes); final Image image = new Image(display, data); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Canvas canvas = new Canvas(shell, SWT.NONE); canvas.setLayoutData(new GridData(bounds.width, bounds.height)); canvas.addListener(SWT.Paint, event -> event.gc.drawImage(image, 0, 0)); shell.pack(); shell.open(); shell.addListener(SWT.Dispose, event -> image.dispose()); }