📄 imageanalyzer.java
字号:
// 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;
gridData.verticalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
imageCanvas.setLayoutData(gridData);
imageCanvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
if (image != null)
paintImage(event);
}
});
imageCanvas.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent event) {
if (image != null) {
showColorAt(event.x, event.y);
}
}
});
// Set up the image canvas scroll bars.
ScrollBar horizontal = imageCanvas.getHorizontalBar();
horizontal.setVisible(true);
horizontal.setMinimum(0);
horizontal.setEnabled(false);
horizontal.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
scrollHorizontally((ScrollBar)event.widget);
}
});
ScrollBar vertical = imageCanvas.getVerticalBar();
vertical.setVisible(true);
vertical.setMinimum(0);
vertical.setEnabled(false);
vertical.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
scrollVertically((ScrollBar)event.widget);
}
});
// Label to show the image size.
sizeLabel = new Label(shell, SWT.NULL);
sizeLabel.setText(bundle.getString("Size_initial"));
sizeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the image depth.
depthLabel = new Label(shell, SWT.NULL);
depthLabel.setText(bundle.getString("Depth_initial"));
depthLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the transparent pixel.
transparentPixelLabel = new Label(shell, SWT.NULL);
transparentPixelLabel.setText(bundle.getString("Transparent_pixel_initial"));
transparentPixelLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the time to load.
timeToLoadLabel = new Label(shell, SWT.NULL);
timeToLoadLabel.setText(bundle.getString("Time_to_load_initial"));
timeToLoadLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Separate the animation fields from the rest of the fields.
separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
separator.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the logical screen size for animation.
screenSizeLabel = new Label(shell, SWT.NULL);
screenSizeLabel.setText(bundle.getString("Animation_size_initial"));
screenSizeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the background pixel.
backgroundPixelLabel = new Label(shell, SWT.NULL);
backgroundPixelLabel.setText(bundle.getString("Background_pixel_initial"));
backgroundPixelLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the image location (x, y).
locationLabel = new Label(shell, SWT.NULL);
locationLabel.setText(bundle.getString("Image_location_initial"));
locationLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the image disposal method.
disposalMethodLabel = new Label(shell, SWT.NULL);
disposalMethodLabel.setText(bundle.getString("Disposal_initial"));
disposalMethodLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the image delay time.
delayTimeLabel = new Label(shell, SWT.NULL);
delayTimeLabel.setText(bundle.getString("Delay_initial"));
delayTimeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show the background pixel.
repeatCountLabel = new Label(shell, SWT.NULL);
repeatCountLabel.setText(bundle.getString("Repeats_initial"));
repeatCountLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Separate the animation fields from the palette.
separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
separator.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Label to show if the image has a direct or indexed palette.
paletteLabel = new Label(shell, SWT.NULL);
paletteLabel.setText(bundle.getString("Palette_initial"));
paletteLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// Canvas to show the image's palette.
paletteCanvas = new Canvas(shell, SWT.BORDER | SWT.V_SCROLL | SWT.NO_REDRAW_RESIZE);
paletteCanvas.setFont(fixedWidthFont);
paletteCanvas.getVerticalBar().setVisible(true);
gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
GC gc = new GC(paletteLabel);
paletteWidth = gc.stringExtent(bundle.getString("Max_length_string")).x;
gc.dispose();
gridData.widthHint = paletteWidth;
gridData.heightHint = 16 * 11; // show at least 16 colors
paletteCanvas.setLayoutData(gridData);
paletteCanvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
if (image != null)
paintPalette(event);
}
});
// Set up the palette canvas scroll bar.
vertical = paletteCanvas.getVerticalBar();
vertical.setVisible(true);
vertical.setMinimum(0);
vertical.setIncrement(10);
vertical.setEnabled(false);
vertical.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
scrollPalette((ScrollBar)event.widget);
}
});
// Sash to see more of image or image data.
sash = new Sash(shell, SWT.HORIZONTAL);
gridData = new GridData();
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
sash.setLayoutData(gridData);
sash.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
if (event.detail != SWT.DRAG) {
((GridData)paletteCanvas.getLayoutData()).heightHint = SWT.DEFAULT;
Rectangle paletteCanvasBounds = paletteCanvas.getBounds();
int minY = paletteCanvasBounds.y + 20;
Rectangle dataLabelBounds = dataLabel.getBounds();
int maxY = statusLabel.getBounds().y - dataLabelBounds.height - 20;
if (event.y > minY && event.y < maxY) {
Rectangle oldSash = sash.getBounds();
sash.setBounds(event.x, event.y, event.width, event.height);
int diff = event.y - oldSash.y;
Rectangle bounds = imageCanvas.getBounds();
imageCanvas.setBounds(bounds.x, bounds.y, bounds.width, bounds.height + diff);
bounds = paletteCanvasBounds;
paletteCanvas.setBounds(bounds.x, bounds.y, bounds.width, bounds.height + diff);
bounds = dataLabelBounds;
dataLabel.setBounds(bounds.x, bounds.y + diff, bounds.width, bounds.height);
bounds = dataText.getBounds();
dataText.setBounds(bounds.x, bounds.y + diff, bounds.width, bounds.height - diff);
//shell.layout(true);
}
}
}
});
// Label to show data-specific fields.
dataLabel = new Label(shell, SWT.NULL);
dataLabel.setText(bundle.getString("Pixel_data_initial"));
gridData = new GridData();
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
dataLabel.setLayoutData(gridData);
// Text to show a dump of the data.
dataText = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
dataText.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
dataText.setFont(fixedWidthFont);
gridData = new GridData();
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
gridData.heightHint = 128;
gridData.grabExcessVerticalSpace = true;
dataText.setLayoutData(gridData);
dataText.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent event) {
if (image != null && event.button == 1) {
showColorForData();
}
}
});
dataText.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
if (image != null) {
showColorForData();
}
}
});
// Label to show status and cursor location in image.
statusLabel = new Label(shell, SWT.NULL);
statusLabel.setText("");
gridData = new GridData();
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
statusLabel.setLayoutData(gridData);
}
Menu createMenuBar() {
// Menu bar.
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
createFileMenu(menuBar);
createAlphaMenu(menuBar);
return menuBar;
}
void createFileMenu(Menu menuBar) {
// File menu
MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
item.setText(bundle.getString("File"));
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(fileMenu);
// File -> Open File...
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("OpenFile"));
item.setAccelerator(SWT.MOD1 + 'O');
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuOpenFile();
}
});
// File -> Open URL...
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("OpenURL"));
item.setAccelerator(SWT.MOD1 + 'U');
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuOpenURL();
}
});
// File -> Reopen
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("Reopen"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuReopen();
}
});
new MenuItem(fileMenu, SWT.SEPARATOR);
// File -> Save
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("Save"));
item.setAccelerator(SWT.MOD1 + 'S');
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuSave();
}
});
// File -> Save As...
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("Save_as"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuSaveAs();
}
});
// File -> Save Mask As...
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("Save_mask_as"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuSaveMaskAs();
}
});
new MenuItem(fileMenu, SWT.SEPARATOR);
// File -> Print
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("Print"));
item.setAccelerator(SWT.MOD1 + 'P');
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuPrint();
}
});
new MenuItem(fileMenu, SWT.SEPARATOR);
// File -> Exit
item = new MenuItem(fileMenu, SWT.NULL);
item.setText(bundle.getString("Exit"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.close();
}
});
}
void createAlphaMenu(Menu menuBar) {
// Alpha menu
MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
item.setText(bundle.getString("Alpha"));
Menu alphaMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(alphaMenu);
// Alpha -> K
item = new MenuItem(alphaMenu, SWT.NULL);
item.setText("K");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuComposeAlpha(ALPHA_CONSTANT);
}
});
// Alpha -> (K + x) % 256
item = new MenuItem(alphaMenu, SWT.NULL);
item.setText("(K + x) % 256");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuComposeAlpha(ALPHA_X);
}
});
// Alpha -> (K + y) % 256
item = new MenuItem(alphaMenu, SWT.NULL);
item.setText("(K + y) % 256");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuComposeAlpha(ALPHA_Y);
}
});
}
void menuComposeAlpha(int alpha_op) {
if (image == null) return;
animate = false; // stop any animation in progress
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
imageCanvas.setCursor(waitCursor);
try {
if (alpha_op == ALPHA_CONSTANT) {
imageData.alpha = alpha;
} else {
imageData.alpha = -1;
switch (alpha_op) {
case ALPHA_X:
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
imageData.setAlpha(x, y, (x + alpha) % 256);
}
}
break;
case ALPHA_Y:
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
imageData.setAlpha(x, y, (y + alpha) % 256);
}
}
break;
default: break;
}
}
displayImage(imageData);
} finally {
shell.setCursor(null);
imageCanvas.setCursor(crossCursor);
waitCursor.dispose();
}
}
void menuOpenFile() {
animate = false; // stop any animation in progress
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -