基于SWT的可绘图拓展程序

import java.util.ArrayList;

import java.util.List;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.MouseAdapter;

import org.eclipse.swt.events.MouseEvent;

import org.eclipse.swt.graphics.Cursor;

import org.eclipse.swt.graphics.GC;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import cn.abc.shapes.Circle;

import cn.abc.shapes.Rect;

import cn.abc.shapes.Shape;

import org.eclipse.swt.events.PaintListener;

import org.eclipse.swt.events.PaintEvent;

import org.eclipse.swt.events.MouseMoveListener;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

public class Mainwindow {

 

/**

 * Launch the application.

 * @param args

 */

static GC gcMain = null;

static int startx;

static int starty;

static boolean leftButtonDown=false;

static String shapeType = "Rect";

public static void main(String[] args) {

Display display = Display.getDefault();

Shell shell = new Shell();

shell.setText("画图");

gcMain = new GC(shell);

shell.setSize(1000, 800);

Board board=new Board();

shell.addPaintListener(new PaintListener() {

public void paintControl(PaintEvent arg0) {

board.Refresh();

}

});

shell.addMouseListener(new MouseAdapter() {

 

public void mouseUp(MouseEvent e) {

if(e.button==1) {

shell.setCursor(new Cursor(null,SWT.CURSOR_ARROW));

leftButtonDown=false;

int width=e.x-startx;

int height=e.y-starty;

gcMain.setForeground(shell.getBackground());

gcMain.drawRectangle(startx,starty,e.x-startx,e.y-starty);

gcMain.setForeground(display.getSystemColor(SWT.COLOR_BLUE));

Shape shape = null;

try {

Class shapeClass = Class.forName(shapeType);

shape = (Shape)shapeClass.newInstance();

shape.setGcMain(gcMain);

shape.setX(startx);

shape.setY(starty);

shape.setWidth(width);

shape.setHeight(height);

board.Add(shape);

board.Refresh();

}

catch(Exception ex) {

shape = null;

}

}

}

public void mouseDown(MouseEvent e) {

if(e.button==1) {

shell.setCursor(new Cursor(null,SWT.CURSOR_CROSS));

leftButtonDown=true;

startx=e.x;

starty=e.y;

}

}

});

shell.addMouseMoveListener(new MouseMoveListener() {

 int lastwidth=0;

 int lastheight=0;

public void mouseMove(MouseEvent e) {

if(leftButtonDown) {

gcMain.setLineStyle(SWT.LINE_DOT);

gcMain.setForeground(shell.getBackground());

gcMain.drawRectangle(startx,starty,lastwidth,lastheight);

gcMain.setForeground(display.getSystemColor(SWT.COLOR_BLUE));

gcMain.drawRectangle(startx,starty,e.x-startx,e.y-starty);

lastwidth=e.x-startx;

lastheight=e.y-starty;

gcMain.setLineStyle(SWT.LINE_SOLID);

}

}

});

List listClass = null;

String pkg = "cn.abc.shapes";

listClass = ClassUtil.getClassList(pkg, true, null);

ArrayList<String>shapeTypes = new ArrayList<String>();

for(Object object:listClass) {

String name = ((Class<?>)object).getName();

if(!name.equals("cn.abc.shapes.Shape")) {

shapeTypes.add(name);

}

}

int indexButton = 0;

for(String strClass:shapeTypes) {

Button btn = new Button(shell, SWT.NONE);

btn.addSelectionListener(new SelectionAdapter() {

@Override

public void widgetSelected(SelectionEvent e) {

shapeType = strClass;

}

});

btn.setBounds(84*indexButton,0,80,27);

indexButton++;

try {

Class<?> shapeClass = Class.forName(strClass);

btn.setText(shapeClass.getName().substring(14));

btn.setData("shapeType",strClass);

}catch(Exception e){

btn.setText(strClass);

btn.setData("shapetype",strClass);

}

}

shell.open();

shell.layout();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) {

display.sleep();

}

}

}

}

import java.util.ArrayList;

import java.util.List;

import cn.abc.shapes.Shape;

public class Board {

private List<Shape> shapes;

public Board() {

shapes = new ArrayList<Shape>();

}

public void Refresh() {

for(Shape shape:shapes) {

shape.Draw();

}

}

public void Add(Shape shape) {

shapes.add(shape);

}

}

 

package cn.abc.shapes;

import org.eclipse.swt.graphics.GC;

 

public interface Shape {

void Draw();

public void setX(int x);

public void setY(int y);

public void setWidth(int width);

public void setHeight(int height);

public void setGcMain(GC gcMain);

}

package cn.abc.shapes;

import org.eclipse.swt.graphics.GC;

public class Circle implements Shape {

private int x;

private int y;

private int width;

private int height;

private GC gcMain;

public Circle() {

}

public void setX(int x) {

this.x = x;

}

public void setY(int y) {

this.y = y;

}

public void setWidth(int width) {

this.width = width;

}

public void setHeight(int height) {

this.height = height;

}

public void setGcMain(GC gcMain) {

this.gcMain = gcMain;

}

public Circle(int x,int y,int width,int height,GC gc){

gcMain=gc;

this.width=width;

this.height=height;

this.x=x;

this.y=y;

}

public void Draw() {

if(gcMain!=null) {

gcMain.drawOval(x, y, width, height);

}

}

}

package cn.abc.shapes;

import org.eclipse.swt.graphics.GC;

public class Rect implements Shape {

private int x;

private int y;

private int width;

private int height;

private GC gcMain;

public Rect() {

}

public void setX(int x) {

this.x = x;

}

public void setY(int y) {

this.y = y;

}

public void setWidth(int width) {

this.width = width;

}

public void setHeight(int height) {

this.height = height;

}

public void setGcMain(GC gcMain) {

this.gcMain = gcMain;

}

public Rect(int x,int y,int width,int height,GC gc) {

gcMain=gc;

this.x=x;

this.y=y;

this.width=width;

this.height=height;

}

@Override

public void Draw() {

// TODO Auto-generated method stub

if(gcMain != null) {

gcMain.drawRectangle(x,y,width,height);

}

}

}

package cn.abc.shapes;

import org.eclipse.swt.graphics.GC;

public class RoundRect implements Shape {

private int x;

private int y;

private int width;

private int height;

private GC gcMain;

public RoundRect() {

}

public void setX(int x) {

this.x = x;

}

public void setY(int y) {

this.y = y;

}

public void setWidth(int width) {

this.width = width;

}

public void setHeight(int height) {

this.height = height;

}

public void setGcMain(GC gcMain) {

this.gcMain = gcMain;

}

public RoundRect(int x,int y,int width,int height,GC gc) {

gcMain=gc;

this.x=x;

this.y=y;

this.width=width;

this.height=height;

}

@Override

public void Draw() {

// TODO Auto-generated method stub

if(gcMain != null) {

gcMain.drawRoundRectangle(x,y,width,height,30,30);

}

}

}

package cn.abc.shapes;

import org.eclipse.swt.graphics.GC;

public class Wujiaoxing implements Shape {

private int[] x = new int[5];

private int[] y = new int[5];

private int[] x_ = new int[5];

private int[] y_ = new int[5];

private int X;

private int Y;

private int width;

private int height;

private double r;

private GC gcMain;

public Wujiaoxing () {

}

public Wujiaoxing(int x,int y,int width, int height,GC gc) {

this.X = x;

this.Y = y;

this.width = width;

this.height = height;

this.gcMain = gc;

}

@Override

public void setX(int x) {

this.X = x;

}

 

@Override

public void setY(int y) {

this.Y = y;

}

 

@Override

public void setWidth(int width) {

this.width = width;

}

 

@Override

public void setHeight(int height) {

this.height = height;

}

 

@Override

public void setGcMain(GC gcMain) {

this.gcMain = gcMain;

}

public void Draw() {

if(gcMain!=null) {

int c = 360 / 5;

r = (height+width)/4;

for (int i = 0; i < 5; i++) {

x[i] = (int) (Math.cos(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r + X);

y[i] = (int) (Math.sin(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r + Y);

}

int r_ = (int) (r * Math.sin(18 * Math.PI / 180) / Math.sin(126 * Math.PI / 180));

for (int i = 0; i < 5; i++) {

x_[i] = (int) (Math.cos((i * c + 18) * Math.PI / 30 - Math.PI / 2) * (r_) + r + X);

y_[i] = (int) (Math.sin((i * c + 18) * Math.PI / 30 - Math.PI / 2) * (r_) + r + Y);

}

gcMain.drawLine(x[0], y[0], x[2], y[2]);

gcMain.drawLine(x[0], y[0], x[3], y[3]);

gcMain.drawLine(x[1], y[1], x[3], y[3]);

gcMain.drawLine(x[1], y[1], x[4], y[4]);

gcMain.drawLine(x[2], y[2], x[4], y[4]);

gcMain.drawLine(x[2], y[2], x[0], y[0]);

}

}

 

}

package cn.abc.shapes;

import org.eclipse.swt.graphics.GC;

public class Wubianxing implements Shape {

private int[] x_ = new int[10];

private int[] y_ = new int[10];

private int x;

private int y;

private int width;

private int height;

private GC gcMain;

public Wubianxing(){

}

public Wubianxing(int x,int y,int width,int height,GC gc){

gcMain=gc;

this.x=x;

this.y=y;

this.width=width;

this.height=height;

}

@Override

public void Draw() {

if(gcMain!=null) {

int c = 360 / 5;

int r = (height+width)/4;

for (int i = 0; i < 10; i+=2) {

x_[i] = (int) (Math.cos(i * c * Math.PI / 15 - Math.PI / 2) * (r) + r + x);

}

for (int i = 1; i < 10; i+=2) {

x_[i] = (int) (Math.sin((i * c + 18) * Math.PI / 15 - Math.PI / 2) * (r) + r + y);

}

gcMain.drawPolygon(x_);

}

}

@Override

public void setX(int x) {

this.x = x;

}

@Override

public void setY(int y) {

this.y = y;

}

 

@Override

public void setWidth(int width) {

this.width = width;

}

 

@Override

public void setHeight(int height) {

this.height = height;

}

 

@Override

public void setGcMain(GC gcMain) {

this.gcMain = gcMain;

}

}

附图一张
在这里插入图片描述

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值