需要下载Filters.jar包(http://www.jhlabs.com/index.html)
package testshell; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Scale; import org.eclipse.swt.widgets.Shell; import com.jhlabs.image.MarbleFilter; public class TestShell2 extends Shell { private BufferedImage back; private BufferedImage foreg; private AlphaComposite ac; private Graphics2D g2d; private int X = 0; private int Y = 0; private int Z = 0; /** * Launch the application * @param args */ public static void main(String args[]) { try { Display display = Display.getDefault(); TestShell2 shell = new TestShell2(display, SWT.SHELL_TRIM); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } catch (Exception e) { e.printStackTrace(); } } /** * Create the shell * @param display * @param style * @throws IOException * @throws FileNotFoundException */ public TestShell2(Display display, int style) throws FileNotFoundException, IOException { super(display, style); createContents(); setLayout(new FormLayout()); } /** * Create contents of the window * @throws IOException * @throws FileNotFoundException */ protected void createContents() throws FileNotFoundException, IOException { back = ImageIO.read(new FileInputStream("c://back.jpg")); foreg = ImageIO.read(new FileInputStream("c://foreg.jpg")); ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f); g2d = back.createGraphics(); g2d.setComposite(ac); setText("SWT Application"); setSize(570, 543); final Canvas canvas = new Canvas(this, SWT.NONE); final FormData fd_canvas = new FormData(); fd_canvas.right = new FormAttachment(100, -5); fd_canvas.top = new FormAttachment(0, 0); fd_canvas.left = new FormAttachment(0, 0); canvas.setLayoutData(fd_canvas); canvas.addPaintListener(new PaintListener(){ @Override public void paintControl(PaintEvent e) { paint(e); }} ); canvas.redraw(); Composite composite; composite = new Composite(this, SWT.NONE); fd_canvas.bottom = new FormAttachment(composite, 0, SWT.TOP); final FormData fd_composite = new FormData(); fd_composite.top = new FormAttachment(0, 390); fd_composite.right = new FormAttachment(canvas, 0, SWT.RIGHT); fd_composite.bottom = new FormAttachment(0, 484); fd_composite.left = new FormAttachment(canvas, 0, SWT.LEFT); composite.setLayoutData(fd_composite); composite.setLayout(new FormLayout()); final Scale scale = new Scale(composite, SWT.NONE); final FormData fd_scale = new FormData(); fd_scale.top = new FormAttachment(0, 30); fd_scale.left = new FormAttachment(0, 5); scale.setLayoutData(fd_scale); scale.addSelectionListener(new SelectionAdapter(){ @Override public void widgetSelected(SelectionEvent arg0) { // TODO Auto-generated method stub X = scale.getSelection(); canvas.redraw(); }} ); Scale scale_1; scale_1 = new Scale(composite, SWT.NONE); fd_scale.right = new FormAttachment(scale_1, -5, SWT.LEFT); final FormData fd_scale_1 = new FormData(); fd_scale_1.bottom = new FormAttachment(scale, 42, SWT.TOP); fd_scale_1.top = new FormAttachment(scale, 0, SWT.TOP); scale_1.setLayoutData(fd_scale_1); scale_1.addSelectionListener(new SelectionAdapter(){ @Override public void widgetSelected(SelectionEvent arg0) { // TODO Auto-generated method stub Y = scale.getSelection(); canvas.redraw(); }} ); Scale scale_2; scale_2 = new Scale(composite, SWT.NONE); fd_scale_1.left = new FormAttachment(scale_2, -185, SWT.LEFT); fd_scale_1.right = new FormAttachment(scale_2, -5, SWT.LEFT); final FormData fd_scale_2 = new FormData(); fd_scale_2.left = new FormAttachment(0, 370); fd_scale_2.right = new FormAttachment(0, 550); fd_scale_2.bottom = new FormAttachment(scale_1, 42, SWT.TOP); fd_scale_2.top = new FormAttachment(scale_1, 0, SWT.TOP); scale_2.setLayoutData(fd_scale_2); scale_2.addSelectionListener(new SelectionAdapter(){ @Override public void widgetSelected(SelectionEvent arg0) { // TODO Auto-generated method stub Z = scale.getSelection(); canvas.redraw(); }} ); // } private void paint(PaintEvent e){ try { BufferedImage foregTemp = new BufferedImage(foreg.getWidth(), foreg.getHeight(),BufferedImage.TYPE_INT_RGB); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); if(X+Y+Z!=0){ BufferedImage ok; MarbleFilter mf = new MarbleFilter(); mf.setTurbulence(mf.getTurbulence()+Z); mf.setXScale(mf.getXScale()+X); mf.setYScale(mf.getYScale()+Y); ok = mf.filter(foreg,foregTemp); g2d.setFont(new Font("隶书", Font.PLAIN, 42)); g2d.setColor(Color.black); g2d.drawString("方正粗宋简体透明度为0.5", 20, 40); g2d.drawImage(ok,50,50,Color.red,null); } ImageIO.write(back, "gif", imOut); InputStream is = new ByteArrayInputStream(bs.toByteArray()); ImageData id = new ImageData(is); Image image = new Image(e.display, id); e.gc.drawImage(image, 0, 0); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components } }