绘图程序

1 package 绘图程序;
2 
3 public interface Shape {
4     public void Draw();
5 }

 

 1 package 绘图程序;
 2 
 3 import org.eclipse.swt.SWT;
 4 import org.eclipse.swt.graphics.Color;
 5 import org.eclipse.swt.graphics.GC;
 6 import org.eclipse.swt.widgets.Shell;
 7 
 8 public class Rect implements Shape {
 9     private int top;
10     private int left;
11     private int width;
12     private int height;
13     private GC gcMain;
14     private int size;
15     private Color color;
16     public Rect(int top, int left, int width, int height,int size,Color color, GC gc){
17         this.top = top;
18         this.left = left;
19         this.width = width;
20         this.height = height;
21         this.gcMain = gc;
22         this.size = size;
23         this.color = color;
24         
25     }
26     @Override
27     public void Draw() {
28         gcMain.setLineWidth(size);
29         gcMain.setForeground(color);
30         gcMain.drawRectangle(top, left, width, height);
31     }
32 
33 }

 

 

 1 package 绘图程序;
 2 import org.eclipse.swt.SWT;
 3 import org.eclipse.swt.graphics.Color;
 4 
 5 import org.eclipse.swt.graphics.GC;
 6 
 7 public class Circle implements Shape {
 8     private int top;
 9     private int left;
10     private int width;
11     private int height;
12     private GC gcMain;
13     private int size;
14     private Color color; 
15     public Circle(int top, int left, int width, int height,int size,Color color, GC gc){
16         this.top = top;
17         this.left = left;
18         this.width = width;
19         this.height = height;
20         this.gcMain = gc;
21         this.size = size;
22         this.color = color; 
23     }
24     
25     @Override
26     public void Draw() {
27         gcMain.setLineWidth(size);
28         gcMain.setForeground(color);
29         gcMain.drawOval(top, left, width, height);
30     }
31 }

 

 

 1 package 绘图程序;
 2 
 3 
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.eclipse.swt.SWT;
 8 import org.eclipse.swt.graphics.GC;
 9 import org.eclipse.swt.widgets.Shell;
10 
11 public class Board {
12     private Shell shell;
13     private List<Shape> shapes;
14     
15     public Board(Shell shell){
16         shapes = new ArrayList<Shape>();
17         this.shell = shell;
18     }
19     public List getList()
20     {
21         return shapes;
22     }
23     public void InsertShape(Shape shape){
24         shapes.add(shape);
25     }
26     
27     public void Refresh(){
28         for (Shape shape : shapes) {
29             shape.Draw();
30         }
31     }
32 }

 

 



package 绘图程序;


import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import org.eclipse.swt.SWT;


import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Combo;


public class Tt {


private static GC gcMain = null;
private static int startX = 0;
private static int startY = 0;
private static boolean leftButtonDown = false;
private static int lastWidth = 0;
private static int lastHeight = 0;
private static String selectshape = "Circle";
private static int size = 0;
private static Display display = Display.getDefault();
private static Shell shell = new Shell();
private static Board board = new Board(shell);
private static Color color = shell.getBackground();
private static String selectcolor = null;

public static Color getColor() {
return color;
}


public static void setColor(Color color) {
Tt.color = color;
}


public static String getSelectcolor() {
return selectcolor;
}


public static void setSelectcolor(String selectcolor) {
Tt.selectcolor = selectcolor;
}


public static void main(String[] args) {
shell.setToolTipText("");


shell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent arg0) {
board.Refresh();
}
});


gcMain = new GC(shell);
shell.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent arg0) {
if (leftButtonDown) {
gcMain.setLineStyle(SWT.LINE_DOT);
gcMain.setForeground(shell.getBackground());
// gcMain.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gcMain.drawRectangle(startX, startY, lastWidth, lastHeight);
gcMain.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
gcMain.drawRectangle(startX, startY, arg0.x - startX, arg0.y - startY);
lastWidth = arg0.x - startX;
lastHeight = arg0.y - startY;
gcMain.setLineStyle(SWT.LINE_SOLID);
}
}
});
shell.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
if (e.button == 1) {
leftButtonDown = false;
lastWidth = e.x - startX;
lastHeight = e.y - startY;


// gcMain.setLineStyle(SWT.LINE_DOT); //线框虚线
gcMain.setForeground(shell.getBackground());

gcMain.drawRectangle(startX, startY, lastWidth, lastHeight);
gcMain.setLineStyle(SWT.LINE_SOLID);//线框实线


gcMain.setForeground(color);
if (selectshape == "Rect") {
Rect rect = new Rect(startX, startY, lastWidth, lastHeight, size, color, gcMain);
// gcMain.setForeground(display.getSystemColor(SWT.COLOR_RED));
gcMain.setLineWidth(size);
board.InsertShape(rect);
} else {


Circle circle = new Circle(startX, startY, lastWidth, lastHeight, size, color, gcMain);
board.InsertShape(circle);
gcMain.setLineWidth(size);
}
board.Refresh();
}
}


@Override
public void mouseDown(MouseEvent e) {
if (e.button == 1) {
leftButtonDown = true;
startX = e.x;
startY = e.y;
}


}
});
shell.setSize(800, 600);
shell.setText("MyDraw");
shell.setLayout(null);


Button Rect = new Button(shell, SWT.NONE);
Rect.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
selectshape = "Rect";
}
});
Rect.setBounds(100, 100, 150, 50);
Rect.setText("Rect");


Button Circle = new Button(shell, SWT.NONE);
Circle.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
selectshape = "Circle";
}
});
Circle.setBounds(300, 100, 150, 50);
Circle.setText("Circle");


Button bt1 = new Button(shell, SWT.NONE);
bt1.setSelection(true);
bt1.setBounds(100, 173, 150, 50);
bt1.setText("粗");
bt1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
size = 5;
}
});
Button bt2 = new Button(shell, SWT.NONE);
bt2.setSelection(true);
bt2.setBounds(300, 173, 150, 50);
bt2.setText("细");
bt2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
size = 2;
}
});


Combo combo = new Combo(shell, SWT.NONE);
combo.setBounds(514, 100, 137, 50);
combo.removeAll();
combo.setText("Color");
combo.add("黄");combo.add("红");combo.add("绿");combo.add("蓝");
combo.addSelectionListener( new SelectionListener(){

@Override
public void widgetSelected(SelectionEvent e) {
selectcolor = combo.getText();
switch (selectcolor)
{
case "黄" : color = display.getSystemColor(SWT.COLOR_YELLOW);break;
case "红" : color = display.getSystemColor(SWT.COLOR_RED);break;
case "绿" : color = display.getSystemColor(SWT.COLOR_GREEN);break;
case "蓝" : color = display.getSystemColor(SWT.COLOR_BLUE);break;
}
}



/// 缺省 默认值
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub

}
}

);


shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

 
posted @ 2017-05-08 17:59  千彧  阅读(369)  评论(0编辑  收藏  举报