java中全局变换和局部变换
全局变换和局部变换
1.全局变换
全局变换是应用于由给定Graphics对象绘制的每个项目的变换。创建全局变换时,可以通过操作Graphics对象的Transform属性来实现,Transform属性是Matrix类的对象。
示例
图像的全局变换
本示例实现的是,当程序运行时,单击【全局变换】按钮,在窗体中的相同位置绘制一个椭圆和一个经过全局变换处理的椭圆。示例运行结果如图1所示。

图1 图像的全局变换
Form1窗体中,在【全局变换】按钮的Click事件中,首先通过调用Graphics对象的DrawEllipse方法绘制一个普通椭圆,然后使用Graphics对象创建一个全局变换(该变换首先在y轴方向上缩放0.5倍,再在x轴方向平移50个单位,然后旋转30°),最后再次调用Graphics对象的DrawEllipse方法绘制椭圆。【全局变换】按钮的Click事件代码如下:
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 2);
graphics.DrawEllipse(myPen, 10, 30, 100, 50);
graphics.ScaleTransform(1, 0.5f);
graphics.TranslateTransform(50, 0, MatrixOrder.Append);
graphics.RotateTransform(30, MatrixOrder.Append);
graphics.DrawEllipse(myPen, 10, 30, 100, 50);
}
完整程序代码:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace _6_20
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 2);
graphics.DrawEllipse(myPen, 10, 30, 100, 50);
graphics.ScaleTransform(1, 0.5f);
graphics.TranslateTransform(50, 0, MatrixOrder.Append);
graphics.RotateTransform(30, MatrixOrder.Append);
graphics.DrawEllipse(myPen, 10, 30, 100, 50);
}
}
}
★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★
namespace _6_20
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(155, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(26, 79);
this.button1.TabIndex = 0;
this.button1.Text = "全局变换";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(194, 119);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
★ ★★★★Program.cs主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _6_20
{
static class Program
{
///
/// 应用程序的主入口点。
///