SWT Tips and Samples 基礎編 > GC |
|
GCGC(Graphics Context)は描画を行うためのクラスです。Drawableインタフェースをもつすべてのウィジェットの上に描画が行えます。Imageオブジェクトは、Drawableインターフェースを持つため、以下のようにしてロードした画像の上に四角形を描画できます。なお生成したImageは、どこかで適切にdisposeしてやる必要があります。 Image image = new Image(display, "sky.jpg"); //コンストラクタ GC(Drawable d) GC gc = new GC(image); gc.drawRectangle(...); .... image.dispose(); また、ウィジェットもDrawableインターフェースを持つため、その上に描画できます。しかし一般には上で示したImageクラスをコンストラクタとして渡すやり方ではなく、PaintListenerを加えることで描画処理を記述します。 shell.addPaintListener(new PaintListener(){ public void paintControl(PaintEvent e) { //shellで描画可能な領域を取り出す Rectangle bound = shell.getClientArea(); e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); e.gc.fillRectangle(10, 10, bound.width - 20, bound.height - 20); } }); 以下のサンプルでは、GCのメソッドを使ってCanvasに画像、図形、文字列を表示しています。 スクリーンショットソースコード (GCTest.java)import org.eclipse.swt.widgets.*; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; public class GCTest { public static void main(String[] args) { new GCTest(); } public GCTest() { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("GCTest"); shell.setLayout(new RowLayout()); final Font font = new Font(display, "MS UI Gothic", 15, SWT.BOLD); final Image img = new Image(display, "sky.jpg"); GC gc = new GC(img); final Rectangle rec = img.getBounds(); System.out.println(rec); gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); gc.setLineWidth(5); gc.drawRectangle(10, 10, rec.width - 20, rec.height - 20); Canvas c = new Canvas(shell, SWT.NONE); c.setLayoutData(new RowData(200, 200)); c.addPaintListener(new PaintListener(){ public void paintControl(PaintEvent e) { GC gc = e.gc; //イメージの描画 gc.drawImage(img, 0, 0, rec.width, rec.height, 0, 0, 100, 100); //四角形の描画 gc.setLineStyle(SWT.LINE_DOT); gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); gc.drawRectangle(10, 110, 80, 80); //円の描画 gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); gc.fillOval(110, 10, 80, 80); //文字の描画 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_GRAY)); gc.setFont(font); gc.drawText("GCのテスト", 110, 110); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } img.dispose(); gc.dispose(); font.dispose(); display.dispose(); } } 参考
最新更新日: 2004年8月28日 |
- PR -
【Sony】大手他社よりも安い!ビジネス向け光・100Mしかも固定IP付!今なら更に初期費用最大15,000円OFF!
オフィス用品・オフィス家具 価 格 交 渉 可!
|
||||
|
|||||
Copyright(C) 2003,2004 Jasmin Project. All Right Reserved. |
SEO | [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送 | ||