📄 imageanalyzer.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.examples.imageanalyzer;import org.eclipse.swt.*;import org.eclipse.swt.widgets.*;import org.eclipse.swt.events.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.printing.*;import org.eclipse.swt.custom.*;import java.util.*;import java.net.*;import java.io.*;import java.text.MessageFormat;public class ImageAnalyzer { static ResourceBundle bundle = ResourceBundle.getBundle("examples_images"); Display display; Shell shell; Canvas imageCanvas, paletteCanvas; Label typeLabel, sizeLabel, depthLabel, transparentPixelLabel, timeToLoadLabel, screenSizeLabel, backgroundPixelLabel, locationLabel, disposalMethodLabel, delayTimeLabel, repeatCountLabel, paletteLabel, dataLabel, statusLabel; Combo backgroundCombo, scaleXCombo, scaleYCombo, alphaCombo; Button incrementalCheck, transparentCheck, maskCheck, backgroundCheck; Button previousButton, nextButton, animateButton; StyledText dataText; Sash sash; Color whiteColor, blackColor, redColor, greenColor, blueColor, canvasBackground; Font fixedWidthFont; Cursor crossCursor; GC imageCanvasGC; int paletteWidth = 140; // recalculated and used as a width hint int ix = 0, iy = 0, py = 0; // used to scroll the image and palette float xscale = 1, yscale = 1; // used to scale the image int alpha = 255; // used to modify the alpha value of the image boolean incremental = false; // used to incrementally display an image boolean transparent = true; // used to display an image with transparency boolean showMask = false; // used to display an icon mask or transparent image mask boolean showBackground = false; // used to display the background of an animated image boolean animate = false; // used to animate a multi-image file Thread animateThread; // draws animated images Thread incrementalThread; // draws incremental images String lastPath; // used to seed the file dialog String currentName; // the current image file or URL name String fileName; // the current image file ImageLoader loader; // the loader for the current image file ImageData[] imageDataArray; // all image data read from the current file int imageDataIndex; // the index of the current image data ImageData imageData; // the currently-displayed image data Image image; // the currently-displayed image Vector incrementalEvents; // incremental image events long loadTime = 0; // the time it took to load the current image static final int INDEX_DIGITS = 4; static final int ALPHA_CONSTANT = 0; static final int ALPHA_X = 1; static final int ALPHA_Y = 2; class TextPrompter extends Dialog { String message = ""; String result = null; Shell dialog; Text text; public TextPrompter (Shell parent, int style) { super (parent, style); } public TextPrompter (Shell parent) { this (parent, SWT.APPLICATION_MODAL); } public String getMessage () { return message; } public void setMessage (String string) { message = string; } public String open () { dialog = new Shell(getParent(), getStyle()); dialog.setText(getText()); dialog.setLayout(new GridLayout()); Label label = new Label(dialog, SWT.NULL); label.setText(message); label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); text = new Text(dialog, SWT.SINGLE | SWT.BORDER); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 300; text.setLayoutData(data); Composite buttons = new Composite(dialog, SWT.NONE); GridLayout grid = new GridLayout(); grid.numColumns = 2; buttons.setLayout(grid); buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); Button ok = new Button(buttons, SWT.PUSH); ok.setText(bundle.getString("OK")); data = new GridData(); data.widthHint = 75; ok.setLayoutData(data); ok.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { result = text.getText(); dialog.dispose(); } }); Button cancel = new Button(buttons, SWT.PUSH); cancel.setText(bundle.getString("Cancel")); data = new GridData(); data.widthHint = 75; cancel.setLayoutData(data); cancel.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { dialog.dispose(); } }); dialog.setDefaultButton(ok); dialog.pack(); dialog.open(); while (!dialog.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } return result; } } public static void main(String [] args) { Display display = new Display(); ImageAnalyzer imageAnalyzer = new ImageAnalyzer(); Shell shell = imageAnalyzer.open(display); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); display.dispose(); } public Shell open(Display dpy) { // Create a window and set its title. this.display = dpy; shell = new Shell(display); shell.setText(bundle.getString("Image_analyzer")); // Hook resize and dispose listeners. shell.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent event) { resizeShell(event); } }); shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { animate = false; // stop any animation in progress if (animateThread != null) { // wait for the thread to die before disposing the shell. while (animateThread.isAlive()) { if (!display.readAndDispatch()) display.sleep(); } } e.doit = true; } }); shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { // Clean up. if (image != null) image.dispose(); whiteColor.dispose(); blackColor.dispose(); redColor.dispose(); greenColor.dispose(); blueColor.dispose(); fixedWidthFont.dispose(); crossCursor.dispose(); } }); // Create colors and fonts. whiteColor = new Color(display, 255, 255, 255); blackColor = new Color(display, 0, 0, 0); redColor = new Color(display, 255, 0, 0); greenColor = new Color(display, 0, 255, 0); blueColor = new Color(display, 0, 0, 255); fixedWidthFont = new Font(display, "courier", 10, 0); crossCursor = new Cursor(display, SWT.CURSOR_CROSS); // Add a menu bar and widgets. createMenuBar(); createWidgets(); shell.pack(); // Create a GC for drawing, and hook the listener to dispose it. imageCanvasGC = new GC(imageCanvas); imageCanvas.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { imageCanvasGC.dispose(); } }); // Open the window shell.open(); return shell; } void createWidgets() { // Add the widgets to the shell in a grid layout. GridLayout layout = new GridLayout(); layout.marginHeight = 0; layout.numColumns = 2; shell.setLayout(layout); // Separate the menu bar from the rest of the widgets. Label separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); GridData gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; separator.setLayoutData(gridData); // Add a composite to contain some control widgets across the top. Composite controls = new Composite(shell, SWT.NULL); RowLayout rowLayout = new RowLayout(); rowLayout.marginTop = 0; rowLayout.marginBottom = 5; rowLayout.spacing = 8; controls.setLayout(rowLayout); gridData = new GridData(); gridData.horizontalSpan = 2; controls.setLayoutData(gridData); // Combo to change the background. Group group = new Group(controls, SWT.NULL); group.setLayout(new RowLayout()); group.setText(bundle.getString("Background")); backgroundCombo = new Combo(group, SWT.DROP_DOWN | SWT.READ_ONLY); backgroundCombo.setItems(new String[] { bundle.getString("None"), bundle.getString("White"), bundle.getString("Black"), bundle.getString("Red"), bundle.getString("Green"), bundle.getString("Blue")}); backgroundCombo.select(backgroundCombo.indexOf(bundle.getString("White"))); backgroundCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { changeBackground(); } }); // Combo to change the x scale. String[] values = { "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2", "3", "4", "5", "6", "7", "8", "9", "10",}; group = new Group(controls, SWT.NULL); group.setLayout(new RowLayout()); group.setText(bundle.getString("X_scale")); scaleXCombo = new Combo(group, SWT.DROP_DOWN); for (int i = 0; i < values.length; i++) { scaleXCombo.add(values[i]); } scaleXCombo.select(scaleXCombo.indexOf("1")); scaleXCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scaleX(); } }); // Combo to change the y scale. group = new Group(controls, SWT.NULL); group.setLayout(new RowLayout()); group.setText(bundle.getString("Y_scale")); scaleYCombo = new Combo(group, SWT.DROP_DOWN); for (int i = 0; i < values.length; i++) { scaleYCombo.add(values[i]); } scaleYCombo.select(scaleYCombo.indexOf("1")); scaleYCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scaleY(); } }); // Combo to change the alpha value. group = new Group(controls, SWT.NULL); group.setLayout(new RowLayout()); group.setText(bundle.getString("Alpha_K")); alphaCombo = new Combo(group, SWT.DROP_DOWN | SWT.READ_ONLY); for (int i = 0; i <= 255; i += 5) { alphaCombo.add(String.valueOf(i)); } alphaCombo.select(alphaCombo.indexOf("255")); alphaCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { alpha(); } }); // Check box to request incremental display. group = new Group(controls, SWT.NULL); group.setLayout(new RowLayout()); group.setText(bundle.getString("Display")); incrementalCheck = new Button(group, SWT.CHECK); incrementalCheck.setText(bundle.getString("Incremental")); incrementalCheck.setSelection(incremental); incrementalCheck.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { incremental = ((Button)event.widget).getSelection(); } }); // Check box to request transparent display. transparentCheck = new Button(group, SWT.CHECK); transparentCheck.setText(bundle.getString("Transparent")); transparentCheck.setSelection(transparent); transparentCheck.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { transparent = ((Button)event.widget).getSelection(); if (image != null) { imageCanvas.redraw(); } } }); // Check box to request mask display. maskCheck = new Button(group, SWT.CHECK); maskCheck.setText(bundle.getString("Mask")); maskCheck.setSelection(showMask); maskCheck.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { showMask = ((Button)event.widget).getSelection(); if (image != null) { imageCanvas.redraw(); } } }); // Check box to request background display. backgroundCheck = new Button(group, SWT.CHECK); backgroundCheck.setText(bundle.getString("Background")); backgroundCheck.setSelection(showBackground); backgroundCheck.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { showBackground = ((Button)event.widget).getSelection(); } }); // Group the animation buttons. group = new Group(controls, SWT.NULL); group.setLayout(new RowLayout()); group.setText(bundle.getString("Animation")); // Push button to display the previous image in a multi-image file. previousButton = new Button(group, SWT.PUSH); previousButton.setText(bundle.getString("Previous")); previousButton.setEnabled(false); previousButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { previous(); } }); // Push button to display the next image in a multi-image file. nextButton = new Button(group, SWT.PUSH); nextButton.setText(bundle.getString("Next")); nextButton.setEnabled(false); nextButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { next(); } }); // Push button to toggle animation of a multi-image file. animateButton = new Button(group, SWT.PUSH); animateButton.setText(bundle.getString("Animate")); animateButton.setEnabled(false); animateButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { animate(); } }); // Label to show the image file type. typeLabel = new Label(shell, SWT.NULL); typeLabel.setText(bundle.getString("Type_initial")); typeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Canvas to show the image. imageCanvas = new Canvas(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE); imageCanvas.setBackground(whiteColor); imageCanvas.setCursor(crossCursor); gridData = new GridData(); gridData.verticalSpan = 15; gridData.horizontalAlignment = GridData.FILL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -