Topic: 在SWT下怎么重绘2D图形

  Print this page

1.在SWT下怎么重绘2D图形 Copy to clipboard
Posted by: huwd
Posted on: 2004-12-02 22:58

我的例子很简单,用SWT创建一个GUI,有一个按钮,只要按一下,就在SHELL上画一条线段,这些我都做好了
但窗口一最小化,线段就消失了,我知道以前在SWING里面,重写paint方法就可以了,但这里SWT下,应该怎么弄,请说的详细点吧,谢谢了,急用!!!

2.Re:在SWT下怎么重绘2D图形 [Re: huwd] Copy to clipboard
Posted by: lxh_ming
Posted on: 2004-12-03 00:22

给你一段重新继承Label 的代码,就在
    addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        if ( MyLabel.this.isSelected )
          drawFocusComponentEnvelope;
      }
    });
这一段里实现
package com.gui.application.Component.control;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gui.application.Component.base.IStyle;

public class MyLabel extends Label {

  private static final int startX = 5;
  private static final int startY = 5;
  private static final int endX = 24;
  private static final int endY = 24;

  private boolean isSelected;
  
  private static MyLabel selected;
  
  private void drawFocusComponent(PaintEvent e) {
    final GC gc = e.gc;
    gc.drawFocus(startX, startY, endX, endY);
  }

  public MyLabel(final Composite comp, int arg1) {
    super(comp, arg1);
    addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        if ( MyLabel.this.isSelected )
          drawFocusComponentEnvelope;
      }
    });
    addMouseListener(new MouseAdapter() {
      public void mouseDown(MouseEvent e) {
if ( selected == MyLabel.this )
return;
        MyLabel.this.isSelected = true;
        if ( null != selected ) {
          selected.isSelected = false;
selected.redraw();
}
        selected = MyLabel.this;
MyLabel.this.redraw();
if ( comp instanceof IStyle ) {
if ( null != MyLabel.this.getData() )
( ( IStyle )comp ).paintObjectChanged( ( Class )MyLabel.this.getData() );
}
      }
    });
  }

  public void dispose() {
    super.dispose();
  }

  protected void checkSubclass() {
  }
}

3.Re:在SWT下怎么重绘2D图形 [Re: huwd] Copy to clipboard
Posted by: huwd
Posted on: 2004-12-03 09:03

还是搞不太懂,而且我这里没有IStyle类
我的代码如下,最好能直接在我代码上改
我是在ECLIPSE里面用VISUAL EDITOR做的,大部分代码都自动生成
只有画线那段是自己写的

import org.eclipse.swt.graphics.*;

import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;

public class myswt {

  private org.eclipse.swt.widgets.Shell sShell = null;
  private Button button = null;

  protected Double line;

  protected GC gc;

  public static void main(String[] args) {
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display
        .getDefault();
    myswt thisClass = new myswt();
    thisClass.createSShell();
    thisClass.sShell.open();

    while (!thisClass.sShell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }

  /**
   * This method initializes sShell
   */
  private void createSShell() {
    sShell = new org.eclipse.swt.widgets.Shell();
    button = new Button(sShell, SWT.NONE);
    sShell.setSize(new org.eclipse.swt.graphics.Point(511, 326));
    sShell.setText("Shell");
    button.setBounds(new org.eclipse.swt.graphics.Rectangle(32, 25, 72,28));
    button.setText("OK");
    button
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
          public void widgetSelected(
              org.eclipse.swt.events.SelectionEvent e) {
            System.out.println("widgetSelected()"); // TODO
                                // Auto-generated
                                // Event stub
                                // widgetSelected()
/************画线**********/
            gc = new GC(sShell);
            gc.drawLine(100, 100, 200, 200);
            /************画线**********/
          }
        });
  }
 
}
初渉SWT,请多多包含!

4.Re:在SWT下怎么重绘2D图形 [Re: huwd] Copy to clipboard
Posted by: lxh_ming
Posted on: 2004-12-03 09:43

你的编程思路有点不对,至少你应该在你的类里面保存划线的起点和终点
我改了一下你的代码:
<code>
import org.eclipse.swt.events.PaintEvent;
import java.awt.geom.Line2D.Double;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;

public class myswt {

  private org.eclipse.swt.widgets.Shell sShell = null;

  private Button button = null;

  protected Double line;

  protected GC gc;

  private boolean canDraw;

  public static void main(String[] args) {
    org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display
        .getDefault();
    myswt thisClass = new myswt();
    thisClass.createSShell();
    thisClass.sShell.open();

    while (!thisClass.sShell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }

  /**
   * This method initializes sShell
   */
  private void createSShell() {
    sShell = new org.eclipse.swt.widgets.Shell();
    button = new Button(sShell, SWT.NONE);
    sShell.setSize(new org.eclipse.swt.graphics.Point(511, 326));
    sShell.setText("Shell");
    button
        .setBounds(new org.eclipse.swt.graphics.Rectangle(32, 25, 72,
            28));
    button.setText("OK");
    button
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
          public void widgetSelected(
              org.eclipse.swt.events.SelectionEvent e) {
            canDraw = !canDraw;
            sShell.redraw();

          }
        });
    sShell.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent arg0) {
        if (canDraw) {
          GC gc = new GC(sShell);
          gc.drawLine(100, 100, 200, 200);
          gc.dispose();
        }
      }

    });

  }
}
<code>

5.Re:在SWT下怎么重绘2D图形 [Re: huwd] Copy to clipboard
Posted by: huwd
Posted on: 2004-12-03 18:31

非常感谢,我这个问题在多个论坛都发过,这里回答的最快最好
再次感谢

6.Re:在SWT下怎么重绘2D图形 [Re: huwd] Copy to clipboard
Posted by: huwd
Posted on: 2004-12-03 18:37

另外,GC里面似乎没有draw(Shape s)的方法
这样就不能把事先保存下来的图元类,如Line2D.double,Ellipse2D.Double一下子画上去了,是这样吗?

7.Re:在SWT下怎么重绘2D图形 [Re: huwd] Copy to clipboard
Posted by: lxh_ming
Posted on: 2004-12-04 00:59

都是通过Draw2D的功能来实现的,画Shape也没有什么难度,自己定义好类就可以了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923