2016-02-24 122 views
2

我想制作一个应用程序,以便使用Java SWT Canvas在绘画中绘制窗体(矩形,线条,方形,箭头)。我使用鼠标事件(向上,向下和移动)来获取画布Y和X位置。并且我有一个按钮,用于获取画布鼠标位置的每个表单类型,并使用鼠标事件绘制选定的表单。我的问题是,当我绘制第一种形式(圆形,方形,线形)时,一切正常,但是在绘制第二种形式时,第一种删除。如何在重画画布后使第一个窗体保持绘制状态?Java SWT在画布上使用鼠标事件绘制图形

变量:

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():

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()和声明:

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(); 
       } 
      }); 

回答

0

默认情况下控制被填充,每次用当前的背景色SWT.Paint侦听器被调用。你需要关闭它。

通过指定的Canvas

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

您还需要填写的背景下首次画布绘制SWT.NO_BACKGROUND风格做到这一点。

+0

嗨格雷格,感谢您的回答。我指定'SWT.NONE'是因为我在App Launch上设置了'Canvas'背景图像,之后我需要在图像上方绘制表单,并且需要保存它; 设置画布背景代码: 'compCanvas.setBackground(new Color(Display.getDefault(),54,54,54)); compCanvas.setBackgroundImage(canvasBcg); compCanvas.setBackgroundMode(SWT.INHERIT_FORCE);' – vinistig

+0

有另一种方法来做到这一点? – vinistig

0

创建类形状的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; 
    } 
} 

后鼠标向上店按照您的列表形状的按钮选择

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

在PainListener你必须重新绘制所有形状从你的名单

for(Shape s: shapes) { 
    //draw shape s 
} 
相关问题