Getting Started with Eclipse RCP Developer Study Notes - 05.2D Drawing

-------------------------------------------------- --------------------------------------------------

Study: http://www.blogjava.net/youxia/archive/2006/12/01/84855.html

-------------------------------------------------- --------------------------------------------------

1. New View CanvasView:

package hellorcp.canvas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class CanvasView extends ViewPart {
        public Canvas canvas;

        @Override
        public void createPartControl(Composite parent) {
                // TODO  Automatically generate method stub
                canvas = new Canvas(parent, SWT.NONE);
        }

        @Override
        public void setFocus() {
                // TODO  Automatically generate method stub
        }

}

2. Draw an oval menu DrawOvalAction:

package hellorcp.canvas;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.GC;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class DrawOvalAction implements IWorkbenchWindowActionDelegate {

        IWorkbenchWindow window;

        public void dispose() {
                // TODO Auto-generated method stub

        }

        public void init(IWorkbenchWindow arg0) {
                this.window = arg0;
        }

        public void run(IAction arg0) {
                IViewReference[] vfs = window.getActivePage().getViewReferences();
                IViewPart vw;
                for (int i = 0; i < vfs.length; i++) {
                        vw = vfs[i].getView(false);
                        if (vw.getTitle().equals(" Paint  ")) {
                                GC gc = new GC(((CanvasView) vw).canvas);
                                gc.drawOval(80, 50, 100, 100);
                                gc.dispose();
                        }
                }
        }

        public void selectionChanged(IAction arg0, ISelection arg1) {
                // TODO Auto-generated method stub
        }

}

3. Perspective.java amended as follows:

package hellorcp;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

        public void createInitialLayout(IPageLayout layout) {
                // FirstView
                String editorArea = layout.getEditorArea();
//              layout.addView("hellorcp.view.FirstView", IPageLayout.RIGHT, 0.2f,
//                              editorArea);

                // SecondView
//              layout.setEditorAreaVisible(false);
//              layout.addView("hellorcp.view.SecondView", IPageLayout.RIGHT, 0.5f,
//                              editorArea);

                // OleView
//              layout.addView("hellorcp.ole.OleView", IPageLayout.RIGHT, 0.5f,
//                              editorArea);

                // CanvasView
                layout.addView("hellorcp.canvas.CanvasView", IPageLayout.RIGHT, 0.5f,
                                editorArea);
        }

}

4. Configuration plugin.xml, fragments are as follows:

1) action:

                   <action
                                class ="hellorcp.canvas.DrawOvalAction"
                icon ="icons/alt_window_16.gif"
                id ="hellorcp.canvas.DrawOvalAction"
                    label =" Drawing an ellipse  "
                                menubarPath ="hellorcp.firstmenu/additions"
                style ="push"
                toolbarPath ="additions"
                tooltip =" Drawing an ellipse  " />

2) view:

         <view

                name=" Paint  " />

5. To save, run, the effect is as follows:

Getting Started with Eclipse RCP Developer Study Notes - 05.2D Drawing

6. Draw a rectangle, gradient rectangle, Photo:

1) Rectangular action:

package hellorcp.canvas;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.GC;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class DrawRectAction implements IWorkbenchWindowActionDelegate {

        IWorkbenchWindow window;

        public void dispose() {
                // TODO Auto-generated method stub

        }

        public void init(IWorkbenchWindow arg0) {
                this.window = arg0;
        }

        public void run(IAction arg0) {
                IViewReference[] vfs = window.getActivePage().getViewReferences();
                IViewPart vw;
                for (int i = 0; i < vfs.length; i++) {
                        vw = vfs[i].getView(false);
                        if (vw.getTitle().equals(" Paint  ")) {
                                GC gc = new GC(((CanvasView) vw).canvas);
                                gc.drawRectangle(280, 50, 100, 100);
                                gc.dispose();
                        }
                }
        }

        public void selectionChanged(IAction arg0, ISelection arg1) {
                // TODO Auto-generated method stub
        }

}

2) The gradient rectangle action:

package hellorcp.canvas;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class DrawGradientAction implements IWorkbenchWindowActionDelegate {

        IWorkbenchWindow window;

        public void dispose() {
                // TODO Auto-generated method stub

        }

        public void init(IWorkbenchWindow arg0) {
                this.window = arg0;
        }

        public void run(IAction arg0) {
                IViewReference[] vfs = window.getActivePage().getViewReferences();
                IViewPart vw;
                for (int i = 0; i < vfs.length; i++) {
                        vw = vfs[i].getView(false);
                        if (vw.getTitle().equals(" Paint  ")) {
                                GC gc = new GC(((CanvasView) vw).canvas);
                                gc.setBackground(window.getShell().getDisplay().getSystemColor(
                                                SWT.COLOR_BLUE));
                                gc.fillGradientRectangle(80, 200, 100, 100, false);
                                gc.dispose();
                        }
                }
        }

        public void selectionChanged(IAction arg0, ISelection arg1) {
                // TODO Auto-generated method stub
        }

}

3) Picture action:

package hellorcp.canvas;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class DrawImageAction implements IWorkbenchWindowActionDelegate {

        IWorkbenchWindow window;

        public void dispose() {
                // TODO Auto-generated method stub

        }

        public void init(IWorkbenchWindow arg0) {
                this.window = arg0;
        }

        public void run(IAction arg0) {
                IViewReference[] vfs = window.getActivePage().getViewReferences();
                IViewPart vw;
                for (int i = 0; i < vfs.length; i++) {
                        vw = vfs[i].getView(false);
                        if (vw.getTitle().equals(" Paint  ")) {
                                GC gc = new GC(((CanvasView) vw).canvas);
                                Image img = new Image(window.getShell().getDisplay(), "E:\\develop\\RCP\\HelloRCP\\splash.bmp");
                                gc.drawImage(img, 280, 200);
                                gc.dispose();
                        }
                }
        }

        public void selectionChanged(IAction arg0, ISelection arg1) {
                // TODO Auto-generated method stub
        }

}

4) action configuration:

                   <action
                                class ="hellorcp.canvas.DrawRectAction"
                icon ="icons/alt_window_16.gif"
                id ="hellorcp.canvas.DrawRectAction"
                    label =" Draw a rectangle  "
                                menubarPath ="hellorcp.firstmenu/additions"
                style ="push"
                toolbarPath ="additions"
                tooltip =" Draw a rectangle  " />
                <action
                                class ="hellorcp.canvas.DrawGradientAction"
                icon ="icons/alt_window_16.gif"
                id ="hellorcp.canvas.DrawGradientAction"
                    label =" Draw a gradient rectangles  "
                                menubarPath ="hellorcp.firstmenu/additions"
                style ="push"
                toolbarPath ="additions"
                tooltip =" Draw a gradient rectangles  " />
                <action
                                class ="hellorcp.canvas.DrawImageAction"
                icon ="icons/alt_window_16.gif"
                id ="hellorcp.canvas.DrawImageAction"
                    label =" Drawn pictures  "
                                menubarPath ="hellorcp.firstmenu/additions"
                style ="push"
                toolbarPath ="additions"
                tooltip =" Drawn pictures  " />

5) Save and run, the effect is as follows:

Getting Started with Eclipse RCP Developer Study Notes - 05.2D Drawing

7. Add the Paint event listener (settlement of the window minimized or obscured by another window, the graphics will go away), the modified CanvasView:

package hellorcp.canvas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class CanvasView extends ViewPart {
        public Canvas canvas;

        @Override
        public void createPartControl(Composite parent) {
                // TODO  Automatically generate method stub
                canvas = new Canvas(parent, SWT.NONE);

                //  Increase Paint listener  , Avoid window changes disappear after drawing
                canvas.addPaintListener(new PaintListener() {
                        public void paintControl(PaintEvent e) {
                                //  Draws an ellipse
                                e.gc.drawOval(80, 50, 100, 100);
                                //  Draw a rectangle
                                e.gc.drawRectangle(280, 50, 100, 100);
                                //  To draw a gradient rectangles
                                e.gc.setBackground(PlatformUI.getWorkbench().getDisplay()
                                                .getSystemColor(SWT.COLOR_BLUE));
                                e.gc.fillGradientRectangle(80, 200, 100, 100, false);
                                //  Draw graphics
                                Image img = new Image(PlatformUI.getWorkbench().getDisplay(),
                                                "E:\\develop\\RCP\\HelloRCP\\splash.bmp");
                                e.gc.drawImage(img, 280, 200);
                        }
                });
        }

        @Override
        public void setFocus() {
                // TODO  Automatically generate method stub
        }

}

分类:Java 时间:2010-04-15 人气:520
本文关键词:
分享到:
blog comments powered by Disqus

相关文章

  • Getting Started with Eclipse RCP Developer Study Notes - 01.Hello RCP 2010-03-29

    Recent eager to want to learn all aspects of the desktop client development, in addition to work is only beginning to learn a bit of time outside of Delphi, as well as U read a little bit of time VB, VC contacts, in this area can be called super do n

  • Getting Started with Eclipse RCP Developer Study Notes - 06.OpenGL 2010-04-16

    -------------------------------------------------- ------------------------------------------------- Study: http://www.blogjava.net/youxia/archive/2006/12/09/86513.html -------------------------------------------------- ------------------------------

  • Getting Started with Eclipse RCP development study notes - 03. View 2010-04-13

    -------------------------------------------------- ---------------------------------------------- Study: http://www.blogjava.net/youxia/archive/2006/11/24/83377.html -------------------------------------------------- ---------------------------------

  • Getting Started with Eclipse RCP development study notes - 02. Menus and dialog boxes 2010-04-09

    -------------------------------------------------- -------------------------------------------- Study: http://www.blogjava.net/youxia/archive/2006/11/20/82388.html -------------------------------------------------- -----------------------------------

  • Getting Started with Eclipse RCP development study notes - 04. In Windows, use the Active X control 2010-04-14

    -------------------------------------------------- ------------------------------------------------- Study: http://www.blogjava.net/youxia/archive/2006/11/28/84013.html -------------------------------------------------- ------------------------------

  • Getting Started with Eclipse RCP development study notes - 07. Summary 2010-04-16

    Follow the "Seaside Momo" series of articles had started studying the development of the RCP has been a general understanding of the increasing interest in this area, of course, if you really want to develop a practical small Dongdong years, Lea

  • windows programming study notes 05 2011-04-10

    Chapter graphics-based 1.GDI principle GDI32.DLL is a dynamic link library, GDI's main purpose is to support a device-independent graphics. 2.GDI function call GDI includes many functions, probably divided into the following categories: Get and relea

  • Colleagues maven study notes 2011-01-17

    Directory Foreword maven Summary not a maven What is maven maven life cycle maven's "convention over configuration" maven version of specification an integral part of the maven maven dependency management Multi-project management Property maven'

  • To talk about the Eclipse RCP in the dialogue 2009-06-16

    Reprinted: http://it.kswchina.com/java/zd/486283.html In this article, I want you to explore is the dialog box, do not belittle this dialog, although the JFace which provide a good base class to use the dialog box, but if you do not understand the SW

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.128 (s). 12 q(s)