KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > ImageUtilities


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.source;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.FontMetrics;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Rectangle;
18 import org.eclipse.swt.widgets.Canvas;
19
20 /**
21  * Provides methods for drawing images onto a canvas.
22  * <p>
23  * This class is neither intended to be instantiated nor instantiated.
24  * </p>
25  *
26  * @since 3.0
27  */

28 public class ImageUtilities {
29
30     /**
31      * Draws an image aligned inside the given rectangle on the given canvas.
32      *
33      * @param image the image to be drawn
34      * @param gc the drawing GC
35      * @param canvas the canvas on which to draw
36      * @param r the clipping rectangle
37      * @param halign the horizontal alignment of the image to be drawn
38      * @param valign the vertical alignment of the image to be drawn
39      */

40     public static void drawImage(Image image, GC gc, Canvas canvas, Rectangle r, int halign, int valign) {
41         if (image != null) {
42
43             Rectangle bounds= image.getBounds();
44
45             int x= 0;
46             switch(halign) {
47             case SWT.LEFT:
48                 break;
49             case SWT.CENTER:
50                 x= (r.width - bounds.width) / 2;
51                 break;
52             case SWT.RIGHT:
53                 x= r.width - bounds.width;
54                 break;
55             }
56
57             int y= 0;
58             switch (valign) {
59             case SWT.TOP: {
60                 FontMetrics fontMetrics= gc.getFontMetrics();
61                 y= (fontMetrics.getHeight() - bounds.height)/2;
62                 break;
63             }
64             case SWT.CENTER:
65                 y= (r.height - bounds.height) / 2;
66                 break;
67             case SWT.BOTTOM: {
68                 FontMetrics fontMetrics= gc.getFontMetrics();
69                 y= r.height - (fontMetrics.getHeight() + bounds.height)/2;
70                 break;
71             }
72             }
73
74             gc.drawImage(image, r.x+x, r.y+y);
75         }
76     }
77
78     /**
79      * Draws an image aligned inside the given rectangle on the given canvas.
80      *
81      * @param image the image to be drawn
82      * @param gc the drawing GC
83      * @param canvas the canvas on which to draw
84      * @param r the clipping rectangle
85      * @param align the alignment of the image to be drawn
86      */

87     public static void drawImage(Image image, GC gc, Canvas canvas, Rectangle r, int align) {
88         drawImage(image, gc, canvas, r, align, SWT.CENTER);
89     }
90 }
91
Popular Tags