`

Figure中的paint

 
阅读更多

尽管一个新的figure可以由多个自组合而成,但是归根结底figure还是画出来的,所以了解figure中GC画的顺序和步骤,对自己扩展一个新的figure是有帮助的。

 

1.paint:

public void paint(Graphics graphics) {
		if (getLocalBackgroundColor() != null)
			graphics.setBackgroundColor(getLocalBackgroundColor());
		if (getLocalForegroundColor() != null)
			graphics.setForegroundColor(getLocalForegroundColor());
		if (font != null)
			graphics.setFont(font);

		graphics.pushState();
		try {
			paintFigure(graphics);
			graphics.restoreState();
			paintClientArea(graphics);
			paintBorder(graphics);
		} finally {
			graphics.popState();
		}
	}

 在figure类中有paint的默认实现,在paint中基本上有四个步骤:

1.设置背景,前景颜色,设置字体样式

2.paintFigure描绘figure自身特点,包括背景颜色之类

3.paintClientArea描绘的区域,规范点说,这里才是我们具体绘制图形的地方

4.paintBorder描绘border

从这个顺序可以看出,孩子是在自己的上面的,边框是在孩子和自己的上面的。

 

2.paintFigure:

protected void paintFigure(Graphics graphics) {
		if (isOpaque())
			graphics.fillRectangle(getBounds());
		if (getBorder() instanceof AbstractBackground)
			((AbstractBackground) getBorder()).paintBackground(this, graphics,
					NO_INSETS);
	}

 

Figure类里面,对自己的描绘内容很简单,判断是否为透明,如果不是就填充颜色。另外,AbstractBackground我就

看到了一个实现类还是emf的,emf用于画阴影在这。其实上面有一个绘制Border的地方,为啥这个地方还来一个,不清

楚。

 

3.paintClientArea:

 

protected void paintClientArea(Graphics graphics) {
		if (children.isEmpty())
			return;

		boolean optimizeClip = getBorder() == null || getBorder().isOpaque();

		if (useLocalCoordinates()) {
			graphics.translate(getBounds().x + getInsets().left, getBounds().y
					+ getInsets().top);
			if (!optimizeClip)
				graphics.clipRect(getClientArea(PRIVATE_RECT));
			graphics.pushState();
			paintChildren(graphics);
			graphics.popState();
			graphics.restoreState();
		} else {
			if (optimizeClip)
				paintChildren(graphics);
			else {
				graphics.clipRect(getClientArea(PRIVATE_RECT));
				graphics.pushState();
				paintChildren(graphics);
				graphics.popState();
				graphics.restoreState();
			}
		}
	}

 

如果无子直接返回,说明这个方法里面确实是主要用于绘制子的内容的。

optimizeClip判断是否选用已经设置的区域进行绘制,如果你自己设置了Bounds的大小,这里是会获取到的。useLocalCoordinates是否选用相对坐标

graphics.translate:这个方法是用来确定相对坐标的起始位置,也就是相对的原点。

paintChildren:绘制子

 

4.paintChildren:

protected void paintChildren(Graphics graphics) {
		for (int i = 0; i < children.size(); i++) {
			IFigure child = (IFigure) children.get(i);
			if (child.isVisible()) {
				// determine clipping areas for child
				Rectangle[] clipping = null;
				if (clippingStrategy != null) {
					clipping = clippingStrategy.getClip(child);
				} else {
					// default clipping behaviour is to clip at bounds
					clipping = new Rectangle[] { child.getBounds() };
				}
				// child may now paint inside the clipping areas
				for (int j = 0; j < clipping.length; j++) {
					if (clipping[j].intersects(graphics
							.getClip(Rectangle.SINGLETON))) {
						graphics.clipRect(clipping[j]);
						child.paint(graphics);
						graphics.restoreState();
					}
				}
			}
		}
	}

 

先判断子是否可见,可见才绘制。

clippingStrategy:这里的裁剪策略,把孩子裁剪成很多块(胡乱理解的),因为有超出的问题。这个东西没有仔细研究,抽时间研究研究。从循环中的逻辑来看,或许是子的内容太多分多次画。

 

 

 

分享到:
评论

相关推荐

    paint_center_X.zip_Center

    在给定的“paint_center_X.zip_Center”压缩包中,包含的核心知识点是关于图形处理和编程的一个简单任务,即“将图形的中心点绘制到坐标X”。这个任务可能是在一个MATLAB环境中执行的,因为文件列表中提到了“paint_...

    用paint方法渐变的窗口背景,using System.Drawing.Drawing2D的应用,C#源代码Graphics g=e.Graphics;

    用paint方法渐变的窗口背景,using System.Drawing.Drawing2D的应用,C#源代码Graphics g=e.Graphics; Color FColor=Color.Blue; Color TColor=Color.Yellow; Brush b =new LinearGradientBrush(this....

    Matlab中图片保存的四种方法.docx

    - 使用菜单`Edit` -&gt; `Copy Figure`,然后在其他图像编辑软件(如Paint、Photoshop等)中粘贴并保存。这种方法允许你利用其他软件的高级功能进行进一步编辑,但同样可能影响图像质量。 3. **使用`saveas`命令**: ...

    swt总结draw2d绘图

    3) 在Canvas的`paint`方法中,获取GraphicsContext,并调用其绘图方法绘制Figure。 4) 处理用户交互,如鼠标点击和拖拽,更新Figure的状态并重新绘制。 5. 图形变换: Draw2D支持对图形进行矩阵变换,如...

    GEF教程及demo源码GEF_RCP_DEMO.zip

    GEF中的Figure类负责图形绘制,通过重写paint()方法实现自定义图形的绘制,同时支持事件监听和交互。 5. **编辑操作与命令模式** GEF使用命令模式来处理用户的编辑操作,每个编辑操作对应一个Command对象。当用户...

    QT的graphicsview高级例程

    在Draw_Figure-master这个项目中,可能包含了实现上述高级功能的代码示例,通过分析和学习这些代码,开发者可以更好地理解和掌握Qt GraphicsView框架的高级特性,从而在自己的应用中创建更丰富的用户界面和图形交互...

    Draw2D-document.rar_Windows编程_Java_

    在绘制过程中,我们需要覆盖Figure类的paint方法,指定如何在Canvas上绘制每个图形。同时,通过监听和响应用户的交互事件,如点击、拖动等,实现图形的动态更新和编辑功能。 在Windows编程环境中,Java的Swing或...

    C#调用matlab画图,解决图像嵌入Winform窗体和首次画图慢的问题

    private void pictureBox_Paint(object sender, PaintEventArgs e) { // 从MATLAB DLL获取图像数据并绘制到pictureBox Bitmap image = GetImageFromMatlab(); e.Graphics.DrawImage(image, 0, 0); } } ``` ...

    VC++工程中加入 SplashScreen 原理释解源代码

    - `Figure-3.JPG`、`Figure-1.JPG`、`Figure-2.JPG`:可能为SplashScreen设计的示例图片,或是代码实现过程的关键步骤截图。 通过以上步骤,开发者可以成功地在VC++ MFC工程中集成SplashScreen,为用户提供更流畅的...

    QT 绘图函数

    要在绘图设备(paint device,一般是一个控件)上开始绘制,我们只要创建一个QPainter,把绘图设备指针传给QPainter对象。例如: oid MyWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); ... } ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: When the curve contain more then one figure and they were all unconfined and curve have alternative brush then whole flex-object bounding rectangle was filed. - FIX: The MaskColor property ...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    Add advanced charting to your ASP.NET applications....Point and Figure Chart Type (Enterprise Edition only) - Point and Figure Chart uses a series of X's and O's to determine price trends. They show ...

Global site tag (gtag.js) - Google Analytics