---
title: "Jexi设计 (10) 坐标转换"
author: "mmdev"
date: "2004-08-03T00:32:00Z"
tags: []
slug: "jexi-10"
draft: false
lastmod: "2004-08-03T00:32:00Z"
id: 1006706
url: "https://mmdev.iteye.com/blog/1006706"
---

坐标转换(修订)

视图保存**(offsetX, offsetY)**，表示文档相对于视图的起始坐标。

文档在绘制时需要这个起始坐标(offsetX, offsetY)，表示整个文档应该从此处绘制。

![](/images/blog_csdn_net/asklxf/16543/o_jexi_calc_1.gif)

上图中：

> **viewWidth–docWidth viewHeight–docHeight**

如果视图比文档还大：

![](/images/blog_csdn_net/asklxf/16543/o_jexi_calc_2.gif)

> **offsetX = ( viewWidth - docWidth ) / 2**  
> **offsetY = ( viewHeight - viewHeight ) / 2**

视图保存关于文档的4个重要的值：docWidth，docHeight，offsetX，offsetY

将视图的坐标转换成文档坐标的方法：

> **int transToDoc(int x) {**  
> **??? return x + offsetX;**  
> **}**
> 
> **int transToDoc(int y) {**  
> **??? return y + offsetY;**  
> **}**

文档的绘图操作起始坐标由(offsetX, offsetY)确定：

> **void View::updateView(Graphics g) {**  
> **??? g.moveTo(offsetX, offsetY);**  
> **??? document.draw(g);**  
> **}**