package DrawShape;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;;
public class MainFrame
{
private int a1,a2,b1,b2,state;//画线时的两个点的横纵坐标;
private int r,a,b,x,y;//x y是中点坐标,r是圆的半径,a是长轴,b是短轴;
public static int c = 0;
public MainFrame()
{
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(400,300);
shell.setLocation(300,100);
shell.setText("图形学作业");
final Label label1 = new Label(shell,SWT.NONE);
final Label label2 = new Label(shell,SWT.NONE);
final Label label3 = new Label(shell,SWT.NONE);
final Label label4 = new Label(shell,SWT.NONE);
final Label label5 = new Label(shell,SWT.NONE);
final Label label6 = new Label(shell,SWT.NONE);
final Label label7 = new Label(shell,SWT.NONE);
final Label label8 = new Label(shell,SWT.NONE);
final Button button1 = new Button(shell,SWT.NONE);
final Text text1 = new Text(shell,SWT.BORDER);
final Text text2 = new Text(shell,SWT.BORDER);
final Text text3 = new Text(shell,SWT.BORDER);
final Text text4 = new Text(shell,SWT.BORDER);
final Text text5 = new Text(shell,SWT.BORDER);
final Text text6 = new Text(shell,SWT.BORDER);
final Text text7 = new Text(shell,SWT.BORDER);
final Text text8 = new Text(shell,SWT.BORDER);
final Text text9 = new Text(shell,SWT.BORDER);
final Combo combo1 = new Combo(shell,SWT.DROP_DOWN);
//label1初始化
label1.setBounds(40,50,100,15);
//label1.setLocation(20,30);
label1.setText("请选择算法");
//label2 初始化
label2.setBounds(220,50,150,15);
label2.setText("请输出A点与B点坐标");
//label3 初始化
label3.setBounds(220,90,30,15);
label3.setText("A");
//label4 初始化
label4.setBounds(220,117,30,15);
label4.setText("B");
//label5初始化
label5.setBounds(220,140,30,15);
label5.setText("圆心");
//label6初始化
label6.setBounds(320,140,25,15);
label6.setText("半径");
//label7初始化
label7.setBounds(220,170,30,15);
label7.setText("长轴");
//label8初始化
label8.setBounds(285,170,30,15);
label8.setText("短轴");
//button1的初始
button1.setText("开始绘制");
button1.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
button1.setBounds(170,200,60,40);
button1.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
final Shell shell1 = new Shell(shell,SWT.NO_TRIM);
shell1.setSize(600,600);
shell1.setLocation(700,40);
shell1.setText("图像");
shell1.setLayout(new FillLayout());
shell1.open();
thePoint [] p;
GC g = new GC(shell1);
g.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
g.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
//画坐标线
g.fillRectangle(0, 0, 600,600);
g.drawLine(0, 300, 600, 300);
g.drawLine(300, 0, 300, 600);
//开始四种画线方法:
g.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
if (state == 1)
{
p = GCUtil.DDAMethod(a1, a2, b1, b2);
for(int i = 0; i <c; i++)
{
g.drawPoint(p[i].getThePointX() + 300,
300 - p[i].getThePointY());
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}else if(state == 2)
{
p = GCUtil.BMethod(a1, a2, b1, b2);
for(int i = 0; i <c; i++)
{
g.drawPoint(p[i].getThePointX() + 300,
300 - p[i].getThePointY());
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}else if(state == 3)
{
p = GCUtil.CirMethod(r);
// int x=100;
// int y =80;
for(int i = 0; i <c; i++)
{
g.drawPoint(p[i].getThePointX() + 300 + x,
300 - p[i].getThePointY() - y);
g.drawPoint(p[i].getThePointY() + 300 + x,
300 - p[i].getThePointX() - y);
g.drawPoint(-(p[i].getThePointX()) + 300 + x,
300 - p[i].getThePointY() - y);
g.drawPoint(-(p[i].getThePointY()) + 300 + x,
300 - p[i].getThePointX() - y);
g.drawPoint(p[i].getThePointY() + 300 + x,
300 + p[i].getThePointX() - y);
g.drawPoint(300 + p[i].getThePointX() + x,
p[i].getThePointY() + 300 - y);
g.drawPoint(-(p[i].getThePointX()) + 300 + x,
300 + p[i].getThePointY() - y);
g.drawPoint(300 - p[i].getThePointY() + x,
p[i].getThePointX() + 300 - y);
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}else
if(state == 4)
{
p = GCUtil.ELMethod(a,b);
for(int i = 0; i <c; i++)
{
g.drawPoint(p[i].getThePointX() + 300 + x,
300 - p[i].getThePointY() - y);
g.drawPoint(p[i].getThePointX() + 300 + x,
300 + p[i].getThePointY() - y);
g.drawPoint(-(p[i].getThePointX()) + 300 + x,
300 + p[i].getThePointY() -y);
g.drawPoint(-(p[i].getThePointX()) + 300 + x,
300 - p[i].getThePointY() - y);
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
g.dispose();
//System.out.println(state);
shell1.layout();
while (!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
//text的初始化
text1.setBounds(250,90,30,20);
text1.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
text1.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
// TODO Auto-generated method stub
if(text1.getText() != "")
a1 = Integer.parseInt(text1.getText());
else
a1 = 0;
//System.out.println("a1= " + a1);
}
});
text2.setBounds(320,90,30,20);
text2.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
text2.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
// TODO Auto-generated method stub
if(text2.getText() != "")
a2 = Integer.parseInt(text2.getText());
else
a2 = 0;
//System.out.println("a2= " + a2);
}
});
text3.setBounds(250,117,30,20);
text3.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
text3.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
// TODO Auto-generated method stub
if(text3.getText() != "")
b1 = Integer.parseInt(text3.getText());
else
b1 = 0;
//System.out.println("b1= " + b1);
}
});
text4.setBounds(320,117,30,20);
text4.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
text4.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
// TODO Auto-generated method stub
if(text4.getText() != "")
b2 = Integer.parseInt(text4.getText());
else
b2 = 0;
//System.out.println("b2= " + b2);
}
});
text5.setBounds(250,140,30,20);
text5.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
text5.addModifyListener(new ModifyListener() {
@Override
public void mod