资源简介
图形学 画线 画多边形 填充算法
填充有种子和扫描线法
代码片段和文件信息
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class test implements ActionListener{
frame f=new frame();//主框架
canvas mycanvas=new canvas();
TextArea text=new TextArea(22);
MenuBar mb=new MenuBar();
Menu menu1menu2menu3menu4;
MenuItem mimi11mi12mi21mi31mi41mi42;
String menuname;
String info;
ArrayList points=new ArrayList();
Graphics g;
Point p1p2screen;
Robot bt;
line_DDA line=new line_DDA();
duobianxing dbx=new duobianxing();
saomiao sm=new saomiao();
zhongzi zz=new zhongzi();
public static void main(String args[]){
test t=new test();
t.go();
}
public void go(){
f.setLayout(new BorderLayout());
f.add(mycanvasBorderLayout.CENTER);
mycanvas.setBackground(Color.white);
text.setBackground(Color.green);
text.setText(“请选择操作“);
text.setEnabled(false);
f.add(text BorderLayout.NORTH);
f.setMenuBar(mb);
f.setSize(500500);
f.setLocation(200 200);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
menu1=new Menu(“画线“);
mi11=new MenuItem(“DDA算法“);
menu1.add(mi11);
mb.add(menu1);
mi11.addActionListener(this);
menu2=new Menu(“画多边形“);
mi21=new MenuItem(“画多边形“);
menu2.add(mi21);
mb.add(menu2);
mi21.addActionListener(this);
menu3=new Menu(“画圆“);
mi31=new MenuItem(“画圆“);
menu3.add(mi31);
mb.add(menu3);
mi31.addActionListener(this);
menu4=new Menu(“填充“);
mi41=new MenuItem(“扫描线填充“);
mi42=new MenuItem(“种子填充“);
menu4.add(mi41);
menu4.add(mi42);
mb.add(menu4);
mi41.addActionListener(this);
mi42.addActionListener(this);
menu4.setEnabled(false);
Menu m=new Menu(“清屏“);
mi=new MenuItem(“清屏“);
mb.add(m);
m.add(mi);
mi.addActionListener(this);
p1=new Point();
p2=new Point();
try {
bt = new Robot();
}
catch (Exception e) {
System.out.print(e.getMessage());
}
}
public void init(){
mycanvas.removeMouseListener(line);
mycanvas.removeMouseListener(dbx);
mycanvas.removeMouseListener(sm);
mycanvas.removeMouseListener(zz);
points.clear();
menuname=““;
menu4.setEnabled(false);
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if(command.equals(“DDA算法“)){
init();
menuname=“DDA算法“;
text.setText(“DDA画线算法:请单击并拖动鼠标以画线。“);
mycanvas.addMouseListener(line);
}
else if(command.equals(“画多边形“)){
init();
menuname=“画多边形“;
text.setText(“请单击鼠标画出多边形的端点,右击表示结束。“);
mycanvas.addMouseListener(dbx);
menu4.setEnabled(true);
}
else if(command.equals(“扫描线填充“)){
mycanvas.removeMouseListener(line);
mycanvas.removeMouseListener(dbx);
mycanvas.removeMouseListener(sm);
mycanvas.removeMouseListener(zz);
menu4.setEnabled(false);
menuname=“扫描线填充“;
text.setText(“扫描线填充法,只能填充最新画的图形。“);
评论
共有 条评论