1 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 28 public class ImageUtilities { 29 30 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 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 |