简体   繁体   English

带有鼠标事件的画布上的Java SWT绘图形状

[英]Java SWT Drawing Shapes on canvas with mouse events

I want to make an application in order to draw forms (rectangle, line, square, arrow) like in paint using Java SWT Canvas. 我想制作一个应用程序,以便使用Java SWT Canvas绘制绘画形式(矩形,直线,正方形,箭头),就像在绘画中那样。 I'm using mouse events (Up, Down and move) to get the canvas Y and X position. 我正在使用鼠标事件(上,下和移动)来获取画布的Y和X位置。 And i have a button for each form types that get canvas mouse position and draw the selected form using the mouse events. 我为每种表单类型都有一个按钮,该按钮可以获取画布的鼠标位置并使用鼠标事件绘制所选表单。 My problem is, when i draw the first form (Circle, square, line) everything works, but when draw the second, the first erase. 我的问题是,当我绘制第一种形式(圆形,正方形,直线)时,一切正常,但是当绘制第二种形式时,则是第一种擦除。 How can I make the first form stay on drawn after redraw the canvas? 重新绘制画布后,如何使第一个表单保持在绘制状态?

Variables: 变量:

private static boolean drag = false;
private Canvas compCanvas;
private Button btnSend, btnAdd,btnFreeHand,btnArrow,btnCircle,btnSquare,btnLine;
private Composite mainPanel;
compCanvas = new Canvas(mainPanel, SWT.NONE);

mouseEvents(): mouseEvents():

private void mouseEvents(){
    compCanvas.addListener(SWT.MouseDown, new Listener(){
        public void handleEvent(Event e){
            System.out.println("Mouse event on canvas DOWN: X VALUE:"+e.x+"Y VALUE:"+e.y);
            startY = e.y;
            startX = e.x;
            drag = true;
        }
    });

    compCanvas.addListener(SWT.MouseUp, new Listener(){
        public void handleEvent(Event e){
            System.out.println("Mouse event on canvas UP: X VALUE:"+e.x+"Y VALUE:"+e.y);
            endY = e.y;
            endX = e.x;
            drag = false;

            //compCanvas.redraw();
        }
    });

    compCanvas.addListener(SWT.MouseMove, new Listener(){
        public void handleEvent(Event e){
            System.out.println("Mouse event on canvas MOVE: X VALUE:"+e.x+"Y VALUE:"+e.y);
            if(drag){
                endY = e.y;
                endX = e.x;

                compCanvas.redraw();
            }
        }
    });
};

btnSquare.selectionListener() and Declaration: btnSquare.selectionListener()和声明:

btnSquare = new Button(compSendAdd, SWT.NONE);
            btnSquare.setLayoutData(new RowData(25, 25));
            btnSquare.setImage(squareIcon);
            btnSquare.addSelectionListener(new SelectionListener(){
                private void btnSquare(){
                    mouseEvents();
                    //LightweightSystem lws = new LightweightSystem(compCanvas);
                    compCanvas.addListener(SWT.Paint, new Listener(){
                        public void handleEvent(Event e){
                            if(drag){
                                GC gc = e.gc;
                                //gc.setAlpha(128);
                                int minX = Math.min(startX,endX);
                                int minY = Math.min(startY,endY);
                                int maxX = Math.max(startX, endX);
                                int maxY = Math.max(startY, endY);
                                int width = maxX - minX;
                                int height = maxY - minY;
                                gc.fillRectangle(minX, minY,width,height);
                            }
                        }
                    });
                }
                public void widgetSelected(SelectionEvent event) {
                    btnSquare();
                }
                public void widgetDefaultSelected(SelectionEvent event) {
                    btnSquare();
                }
            });

By default controls are filled with current background color each time the SWT.Paint listener is called. 默认情况下,每次调用SWT.Paint侦听器时,控件会使用当前背景色填充。 You need to turn this off. 您需要关闭此功能。

Do this by specifying the SWT.NO_BACKGROUND style on the Canvas 通过在Canvas上指定SWT.NO_BACKGROUND样式来执行此操作

compCanvas = new Canvas(mainPanel, SWT.NO_BACKGROUND);

You will also need to fill the background the first time the canvas is drawn. 您还需要在第一次绘制画布时填充背景。

Create class shape with x, y, width, height fields 使用x,y,宽度,高度字段创建类形状

class Shape {
    public int x; // coordiates
    public int y;
    public int width;
    public int heigth;
    String type; // "rect" for example
    public Shape(int x, int y, int width, int height, String type) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = height;
        this.type = type;
    }
}

After mouse up store your shape in list according to which button is selected 鼠标悬停后,根据选择的按钮将形状存储在列表中

List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Shape(x, y, width, height, getType()));

In PainListener You MUST redraw all shapes from your list 在PainListener中,您必须从列表中重新绘制所有形状

for(Shape s: shapes) {
    //draw shape s
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM