/******************************************************************************* * Copyright (c) 2000, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.snippets; /* * Use transformation matrices to reflect, rotate and shear images * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */ import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class Snippet207 { public static void main(String[] args) { final Display display = new Display(); final Image image = new Image(display, 110, 60); GC gc = new GC(image); Font font = new Font(display, "Times", 30, SWT.BOLD); gc.setFont(font); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.fillRectangle(0, 0, 110, 60); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.drawText("HOV", 10, 10, true); gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); gc.fillRectangle(0, 0, 10, 10); font.dispose(); gc.dispose(); final Rectangle rect = image.getBounds(); Shell shell = new Shell(display); shell.setText("Matrix Tranformations"); shell.setLayout(new FillLayout()); final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED); canvas.addPaintListener(new PaintListener () { public void paintControl(PaintEvent e) { GC gc = e.gc; gc.setAdvanced(true); if (!gc.getAdvanced()){ gc.drawText("Advanced graphics not supported", 30, 30, true); return; } // Original image int x = 30, y = 30; gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA)); // gc.drawLine(20, 20, 580, 580); // gc.drawImage(image, x, y); Transform transform = new Transform(display); // transform.setElements(1, 0, 0, 1, 0 ,0); transform.scale(1, 1); // transform.translate(0.5f, 0.5f); // transform.invert(); gc.setTransform(transform); gc.drawImage(image, x, y); x += rect.width + 30; // Note that the tranform is applied to the whole GC therefore // the coordinates need to be adjusted too. // Reflect around the y axis. transform.setElements(-1, 0, 0, 1, 0 ,0); gc.setTransform(transform); //(-30-110-30-110,30) gc.drawImage(image, -1*x-rect.width, y); x = 30; y += rect.height + 30; // Reflect around the x axis. transform.setElements(1, 0, 0, -1, 0, 0); gc.setTransform(transform); //(30,-30-60-30-60) gc.drawImage(image, x, -1*y-rect.height); x += rect.width + 30; // Reflect around the x and y axes transform.setElements(-1, 0, 0, -1, 0, 0); gc.setTransform(transform); //(-30-110-30-110,-30-60-30-60) gc.drawImage(image, -1*x-rect.width, -1*y-rect.height); x = 30; y += rect.height + 30; // Shear in the x-direction transform.setElements(1, 0, -1, 1, 0, 0); gc.setTransform(transform); gc.drawImage(image, 300, y); // Shear in y-direction transform.setElements(1, -1, 0, 1, 0, 0); gc.setTransform(transform); gc.drawImage(image, 170, 475); // Rotate by 45 degrees float cos45 = (float)Math.cos(Math.PI/4); float sin45 = (float)Math.sin(Math.PI/4); System.out.println(cos45); System.out.println(sin45); transform.setElements(cos45, sin45, -sin45, cos45, 0, 0); gc.setTransform(transform); gc.drawImage(image, 250, 150); /* // Shear in x-y-direction transform.setElements(1, 1, 0, 1, 0, 0); gc.setTransform(transform); gc.drawImage(image, 170, 195); // Shear in x-y-direction transform.setElements(1, 1, 0, 1, 0, 0); gc.setTransform(transform); gc.drawImage(image, 280, -85); // Shear in y-direction transform.setElements(1, -1, 0, 1, 0, 0); // transform.translate(20, 20); gc.setTransform(transform); gc.drawImage(image, 280, 755); // gc.setTransform(transform); // gc.drawText("20,20", 20, 20, true); transform = new Transform(display); transform.rotate(30); gc.setTransform(transform); gc.drawImage(image, 350, 0); */ transform.dispose(); } }); shell.setSize(600, 600); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } image.dispose(); display.dispose(); } }
1,Instances of this class represent transformation matrices for points expressed as (x, y) pairs of floating point numbers.
org.eclipse.swt.graphics(package)
??????? 例子:org.eclipse.swt.snippets.Snippet207
??????? 實現:Use transformation matrices to reflect, rotate and shear images
Transform其實就是實現了坐標的轉換,其實并沒有改變圖像本身。具體怎么改的我只是猜測,并在猜測后用程序去驗證我的猜測。
Transform用途有:(這些都是針對于圖像的)
??????? 1,放大,縮小。
??????? 2,旋轉一定得角度。
??????? 3,根據矩形運算完成圖像變換,X對稱,Y對稱,Y=X對稱等等。
注意點:
??????? 1,Transform的變換是基于坐標系的變換,所以對所有點有效。這里要特別指出的是圖像的起始坐標,也應跟著變換了。(簡單想,不是太好理解,要認真分析Snippet207就可以看出)
??????? 2,
如何使用:
??????? 1,放大,縮小——scale(3,3)放大為原來的三倍。 scale(0.5f,0.5f)為1/2. 這里與ImageData的scaledTo不同,ImageData只是針對于圖像變換,這是坐標系變大。
??????? 2,旋轉一定得角度——rotate(45) 旋轉45度,這里的參數直接是度數,而Math.cos()里面使用的弧度。坐標也轉45度。
??????? 3,矩形運算——最復雜也最靈活。setElements(float m11, float m12, float m21, float m22, float dx, float dy)
??????????????? transform.setElements(1, 0, 0, 1, 0 ,0)——原圖不變。
??????????????? transform.setElements(-1, 0, 0, 1, 0 ,0)——得到與原圖Y對稱的圖。
??????????????? transform.setElements(1, 0, 0, -1, 0 ,0)——得到與原圖X對稱的圖。
??????????????? transform.setElements(-1, 0, 0, -1, 0 ,0)——得到與原圖Y=x對稱的圖。
??????????????? transform.setElements(1, 0, -1, 1, 0 ,0)——X軸不變,Y軸向左轉45度 的圖。
??????????????? transform.setElements(1, -1, 0, 1, 0 ,0)——Y軸不變,X軸向上轉45度 的圖。
?????????
?
??????? 上圖就是Snippet207的效果圖:
??????????????? ①,原圖。
??????????????? ②,Y對稱。
??????????????? ③,X對稱。
??????????????? ④,Y=X對稱。
??????????????? ⑤,X軸不變,Y軸向左轉45度。
??????????????? ⑥,Y軸不變,X軸向上轉45度。
??????????????? ⑦,X軸Y軸一起順時針轉45度,也可以看著是圖像轉。所以,transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);和transform.rotate(30);等價。
矩陣運算的原理:
??????? 首先,看API文檔上說了。
??????????????? m11 - the first element of the first row of the matrix
??????????????? m12 - the second element of the first row of the matrix
??????????????? m21 - the first element of the second row of the matrix
??????????????? m22 - the second element of the second row of the matrix
??????????????? dx - the third element of the first row of the matrix
??????????????? dy - the third element of the second row of the matrix
??????? 也就是說表示的矩形為,這里先不說dx和dy,馬上最后說。
??????? 所以這里的坐標變換就成了。
??????????????? (1, 0, 0, 1, 0 ,0)——(X,Y)——原圖不變。
??????????????? (-1, 0, 0, 1, 0 ,0)——(-X,Y)——得到與原圖Y對稱的圖。
??????????????? (1, 0, 0, -1, 0 ,0)——(X,-Y)——得到與原圖X對稱的圖。
??????????????? (-1, 0, 0, -1, 0 ,0)——(-X,-Y)——得到與原圖Y=x對稱的圖。
??????????????? (1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉45度 的圖。
??????????????? (1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉45度 的圖。
上圖展示了三種對稱圖。
對于其他如“(1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉45度 的圖”和“(1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉45度 的圖”我是采用點帶入計算然后總結出坐標系的變換的。如下圖:
?
這就是“(1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉45度 的圖”的變換。我取了四個點【(0,0),(0,Y),(X,0),(X,Y)】這樣就可很明顯地看出等價的坐標系的變換。當然“(1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉45度 的圖”也是同理了。所以,在遇到這個矩陣變換看不懂知道用這種方法就可以看出來。
最后說一下這個dx和dy,其實看了上面的坐標變換就可以很好想象dx和dy了。dx和dy就是實現坐標系的平移了。加入dx和dy,轉換的公式就變成了:
再提一下還有個函數translate(float offsetX, float offsetY),這個的offsetX和offsetY就相當于dx和dy,這樣就好理解了。
關于Transform基本上就講完了,這里多說一下應用,矩形變化可以應用于任何的坐標系的變換。
這里補充一下為什么,(X,Y)旋轉后,坐標變為了呢?順便復習一下數學知識。
角坐標系和直角坐標系對應
所以:
當然Y也同理了。
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
