亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Image中的transformation理解【swt.snippet】

系統 1637 0
    /*******************************************************************************
 * 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度 的圖。
?????????
Image中的transformation理解【swt.snippet】
?
??????? 上圖就是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度 的圖。


Image中的transformation理解【swt.snippet】
?

上圖展示了三種對稱圖。
對于其他如“(1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉45度 的圖”和“(1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉45度 的圖”我是采用點帶入計算然后總結出坐標系的變換的。如下圖:

?


Image中的transformation理解【swt.snippet】
?

Image中的transformation理解【swt.snippet】
?

這就是“(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也同理了。

?

?

?

Image中的transformation理解【swt.snippet】


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产亚洲精品久久久久91网站 | 国产看片视频 | 在线免费h| 久久9966e这里只有精品 | 日本免费不卡视频一区二区三区 | 久久成人免费视频 | 老色鬼a∨在线视频在线观看 | 青青热久久国产久精品秒播 | 伊人网综合在线观看 | 手机看片福利盒子久久青 | 天天躁夜夜躁很很躁麻豆 | 亚洲一级毛片视频 | 成年女人免费看 | 欧美午夜艳片欧美精品 | 日韩精品你懂的在线播放 | 美女一级毛片免费观看 | 久久精品国产欧美成人 | 狠狠色婷婷狠狠狠亚洲综合 | 丁香婷婷影音先锋5566 | 亚洲欧美日韩国产精品第不页 | 欧美日韩中文亚洲v在线综合 | 精品久久久久久中文字幕网 | 一区二区三区免费视频网站 | 亚洲一区免费在线观看 | 精品影视 | 九九久久九九久久 | 中文字幕第66页永久乱码 | 国产高清不卡视频 | 成人国产在线观看 | 国产高清在线观看麻豆 | 一级特黄aaa大片免色 | 日韩欧美在线观看视频 | 欧美性猛交xxxx免费看久久久 | 国产日产精品久久久久快鸭 | 欧美日韩中文字幕在线视频 | 日本在线黄 | 免费一级毛片在线播放欧美 | 福利姬在线播放 | 国产精品成 | 国产成人做受免费视频 | 成人性色生活片免费网 |