本Demo是在SWT Canvas中画图,实现CTRL+ Z回退到前一个操作的记录的功能。
实现原理:使用java中的List实现一个对象栈的输入输出操作(为什么不用数组来实现,比list实现麻烦的多,因为数组要求是固定长度,不容易改变。List的大小可以根据需要变改变),在List中保存每一步操作的图片截图,没CTRL + Z 一次,就将List中的最后一个图片对象取出,显示到Canvas中,然后删除List中这个最后一个图片对象。(每次输入输出都是从List的最后一个元素开始的)。
实现代码:
package swt_jface.demo3;
import java.io.File;
public class CtrlZdemo {
protected Shell shell;
private Image image = null;
private int startX = 0;
private int startY = 0;
private int endX = 0;
private int endY = 0;
private int offsetX = 0;
private int offsetY = 0;
private GC gc = null;
private Image tempImage = null;
private boolean bool1 = false;
private List<Image> imagesList = new ArrayList<Image>();
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
CtrlZdemo window = new CtrlZdemo();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setMaximized(true);
shell.setText("SWT Application");
Composite composite = new Composite(shell, SWT.BORDER);
composite.setLayout(new FillLayout(SWT.HORIZONTAL));
final Canvas canvas = new Canvas(composite, SWT.NONE);
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
backOperatoin(e);
// canvas.redraw();
}
@Override
public void keyReleased(KeyEvent e) {
backOperatoin(e);
// canvas.redraw();
}
});
final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!bool1) {
bool1 = true;
button.setText("点击开始写字");
} else {
bool1 = false;
button.setText("画方框");
}
}
});
button.setText("画方框");
Button button_1 = new Button(shell, SWT.NONE);
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (imagesList!=null && imagesList.size() > 0) {
tempImage = imagesList.get(imagesList.size() - 1);
ImageData imageData = tempImage.getImageData();
ImageLoader imageLoader = new ImageLoader();
String fileName = imagesList.size()+".jpg";
imageLoader.data = new ImageData[]{imageData};
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(new File(".\\IMAGESAVER\\"+fileName));
imageLoader.save(outputStream, getImageFileType(fileName));
outputStream.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
button_1.setText("保存当前图片");
GroupLayout gl_shell = new GroupLayout(shell);
gl_shell.setHorizontalGroup(
gl_shell.createParallelGroup(GroupLayout.LEADING)
.add(composite, GroupLayout.DEFAULT_SIZE, 681, Short.MAX_VALUE)
.add(GroupLayout.TRAILING, gl_shell.createSequentialGroup()
.add(230)
.add(button, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
.add(18)
.add(button_1, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
.addContainerGap(223, Short.MAX_VALUE))
);
gl_shell.setVerticalGroup(
gl_shell.createParallelGroup(GroupLayout.LEADING)
.add(gl_shell.createSequentialGroup()
.add(composite, GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
.add(46)
.add(gl_shell.createParallelGroup(GroupLayout.BASELINE)
.add(button)
.add(button_1))
.add(51))
);
gl_shell.linkSize(new Control[] {button, button_1}, GroupLayout.VERTICAL);
gl_shell.linkSize(new Control[] {button, button_1}, GroupLayout.HORIZONTAL);
shell.setLayout(gl_shell);
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
startX = e.x;
startY = e.y;
}
@Override
public void mouseUp(MouseEvent e) {
endX = e.x;
endY = e.y;
if (bool1) {
drawRectangle(gc);
} else {
drawText(gc, "SHOW TEXT");
}
}
});
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent arg0) {
gc = new GC(canvas);
canvasDrawImage(gc);
}
});
}
public void canvasDrawImage(GC gc) {
System.out.println("初始化图像");
try {
FileInputStream input = new FileInputStream(new File(
".\\IMAGE\\Sunset.jpg"));
ImageData imageData = new ImageData(input);
image = new Image(Display.getDefault(), imageData);
gc.drawImage(image, 0, 0);
input.close();
imagesList.add(image);
} catch (Exception e) {
e.printStackTrace();
}
}
//画正方形
public void drawRectangle(GC gc) {
offsetX = endX - startX;
offsetY = endY - startY;
if (gc != null) {
gc.setLineWidth(5);
gc.setForeground(new Color(Display.getDefault(), 255, 255, 0));
gc.drawRectangle(startX, startY, offsetX, offsetY);
tempImage = new Image(Display.getDefault(), ".\\IMAGE\\Sunset.jpg");
//截取canvas中的当前图像
gc.copyArea(tempImage, 0, 0);
//添加到imagesList中
imagesList.add(image);
}
}
//画文字
public void drawText(GC gc, String str) {
offsetX = endX - startX;
offsetY = endY - startY;
if (gc != null) {
gc.setLineWidth(5);
Font font = new Font(Display.getDefault(), "Arial", 25, SWT.BOLD);
gc.setFont(font);
gc.drawText(str, startX, startY, true);
//截取canvas中的当前图像
tempImage = new Image(Display.getDefault(), ".\\IMAGE\\Sunset.jpg");
gc.copyArea(tempImage, 0, 0);
//添加到imagesList中
imagesList.add(tempImage);
}
}
//回退操作,没回退一次,取出imagesList中的最后一个图片对象出来,显示到Canvas中,并且删除imagesList中的元素
public void backOperatoin(KeyEvent e) {
if ((e.stateMask & SWT.CTRL) != 0) {
if (e.keyCode == 115) {//CTRL + S
if (imagesList != null && imagesList.size() > 0) {
int size = imagesList.size();
//取出最后一个元素
image = imagesList.get(size - 1);
//显示到Canvas中
if (image != null) {
gc.drawImage(image, 0, 0);
}
//删除该元素
imagesList.remove(size - 1);
System.out.println(imagesList.size());
} else {
canvasDrawImage(gc);
}
}
}
}
private int getImageFileType(String filename) {
String ext = filename.substring(filename.lastIndexOf('.') + 1);
if (ext.equalsIgnoreCase("gif"))
return SWT.IMAGE_GIF;
if (ext.equalsIgnoreCase("ico"))
return SWT.IMAGE_ICO;
if (ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg"))
return SWT.IMAGE_JPEG;
if (ext.equalsIgnoreCase("png"))
return SWT.IMAGE_PNG;
return SWT.IMAGE_UNDEFINED;
}
}
实现效果: