Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1e8cb7c4ebb69d92c130bff5f114328e79bea683 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.eclipse.swt.examples.paint;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved
 */

import org.eclipse.swt.graphics.*;

/**
 * A rectangle drawing tool
 */
public class FilledRectangleTool extends DragInteractivePaintSession implements PaintTool {
	private Color temporaryFGColor;
	private Color temporaryBGColor;
	private Color drawFGColor;
	private Color drawBGColor;

	/**
	 * Create a RectangleTool.
	 * 
	 * @param toolSettings the new tool settings
	 * @param paintSurface the PaintSurface we will render on.
	 */
	public FilledRectangleTool(ToolSettings toolSettings, PaintSurface paintSurface) {
		super(paintSurface);
		set(toolSettings);
		temporaryFGColor = new Color(null, 255, 255, 255);
		temporaryBGColor = new Color(null, 127, 127, 127);
	}
	
	/**
	 * Set the tool's settings
	 * 
	 * @param toolSettings the new tool settings
	 */
	public void set(ToolSettings toolSettings) {
		drawFGColor = toolSettings.commonForegroundColor;
		drawBGColor = toolSettings.commonBackgroundColor;
	}
	
	/**
	 * Get name associated with this tool
	 * 
	 * @return the localized name of this tool
	 */
	public String getDisplayName() {
		return PaintPlugin.getResourceString("tool.FilledRectangle.displayname");
	}

	/*
	 * Template methods for drawing
	 */
	protected void drawPermanent(GC gc, Point a, Point b) {
		gc.setForeground(drawFGColor);
		gc.setBackground(drawBGColor);
		fillRectangle(gc, a, b);
	}		

	protected void drawTemporary(GC gc, Point a, Point b) {
		gc.setForeground(temporaryFGColor);
		gc.setBackground(temporaryBGColor);
		gc.setXORMode(true);
		fillRectangle(gc, a, b);
		gc.setXORMode(false);
	}		

	protected void eraseTemporary(GC gc, Point a, Point b) {
		drawTemporary(gc, a, b);
	}

	/**
	 * Draw a filled rectangle
	 */
	protected void fillRectangle(GC gc, Point a, Point b) {
		final int left = Math.min(a.x, b.x);
		final int top = Math.min(a.y, b.y);
		final int width = Math.abs(a.x - b.x) + 1;
		final int height = Math.abs(a.y - b.y) + 1;
		
		gc.drawRectangle(left, top, width, height);
		if ((width > 2) && (height > 2))
			gc.fillRectangle(left + 1, top + 1,
				width - 1/*- 2*/, height - 1/*- 2*/); // BUG in GC
	}
}

Back to the top