📄 draw_cut.java
字号:
import java.awt.*;
import java.awt.event.*;
class draw extends Frame implements WindowListener,ActionListener,MouseListener,ItemListener,MouseMotionListener
{
Dimension D1;
Toolkit TK;
MenuBar mb;
Menu me,mh,mpl,mpc;
Panel p1,p2;
Label l1,l2,l3,l4,l5;
Choice c1,c2,c3,c4;
Button b1,b2,b3,b4;
boolean isXOR=false;//用于确定写模式,true为异或模式,false为写模式
static int width,height,f0=1,f1=11,x0=0,x1=0,y0=0,y1=0;
static Color col=Color.black,cc=Color.red;
int linestyle=0xffff,point=1,r=0,flag=0,x00=0,x01=0,y00=0,y01=0,x,y,first=0;
static Graphics g;
static Image buffer;
public void display()
{
//获取屏幕的宽度和高度
TK=Toolkit.getDefaultToolkit();
D1=TK.getScreenSize();
width=D1.width;
height=D1.height-20;
//设置标题栏和窗口的大小
setTitle("简单绘图系统");
setSize(width,height);
setBackground(Color.white);
addWindowListener(this);
addMouseListener(this);
addMouseMotionListener(this);
//上方菜单
mb=new MenuBar();
setMenuBar(mb);
me=new Menu("编辑");
me.add(new MenuItem("清屏"));
me.addActionListener(this);
mh=new Menu("帮助");
mb.add(me);
mb.add(mh);
//下方画板
p1=new Panel();
p1.setBackground(Color.LIGHT_GRAY);
l1=new Label("颜色");
l2=new Label("线型");
l3=new Label("线宽");
l4=new Label("算法");
l5=new Label("坐标: ");
//颜色
c1=new Choice();
c1.addItem("灰");
c1.addItem("黑");
c1.addItem("红");
c1.addItem("粉红");
c1.addItem("黄");
c1.addItem("绿");
c1.addItem("蓝");
c1.addItem("更多颜色");
c1.select("黑");
c1.addItemListener(this);
//线型
c2=new Choice();
c2.addItem("___________");
c2.addItem("__ __ __ ");
c2.addItem("_ _ _ _ _ _");
c2.addItem("___.___.___");
c2.addItem("自定义");
c2.addItemListener(this);
//线宽
c3=new Choice();
c3.addItem("1");
c3.addItem("2");
c3.addItem("3");
c3.addItem("自定义");
c3.addItemListener(this);
//算法
c4=new Choice();
c4.addItem("请选择算法 ");
c4.addItemListener(this);
p1.add(l1); p1.add(c1);
p1.add(l2); p1.add(c2);
p1.add(l3); p1.add(c3);
p1.add(l4); p1.add(c4);
p1.add(l5);
//左侧画板
p2=new Panel();
p2.setBackground(Color.LIGHT_GRAY);
b1=new Button("画直线");
b2=new Button("画圆");
b3=new Button("区域填充");
b4=new Button("线段裁剪");
b1.addActionListener(this); p2.add(b1);
b2.addActionListener(this); p2.add(b2);
b3.addActionListener(this); p2.add(b3);
b4.addActionListener(this); p2.add(b4);
p2.setLayout(new GridLayout(15,1));
add(p1,BorderLayout.SOUTH);
add(p2,BorderLayout.WEST);
setVisible(true);
g=this.getGraphics();//画笔初始化
buffer=this.createImage(width,height);//创建缓冲区的大小
}//display
//实现接口ActionListener的所有方法,用于处理发生在菜单上的事件
public void actionPerformed(ActionEvent e)
{
isXOR=false;
if(e.getActionCommand()=="清屏")
{
buffer=this.createImage(width,height);//创建缓冲区的大小
g.drawImage(buffer,0,0,this);
}
if(e.getActionCommand()=="画直线")
{
f0=1;
c4.removeAll();
c4.addItem("DDA算法");
c4.addItem("中点画线算法");
c4.addItem("Bresenham算法");
}
if(e.getActionCommand()=="画圆")
{
f0=2;
c4.removeAll();
c4.addItem("多边形逼近法");
c4.addItem("中点画圆法");
}
if(e.getActionCommand()=="区域填充")
{
f0=3;
c4.removeAll();
c4.addItem("扫描线种子填充算法");
}
if(e.getActionCommand()=="线段裁剪")
{
f0=4;
c4.removeAll();
c4.addItem("编码裁减");
}
} //actionPerformed
//实现接口WindowListener的所有方法,用于处理发生在窗口上的事件
public void windowClosing(WindowEvent e)
{
System.exit(0);
}//windowClosing
public void windowOpened(WindowEvent e){ }
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
//实现接口ItemListener的所有方法,用于处理发生在选择框上的事件
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==c1)//颜色
switch(c1.getSelectedIndex())
{
case 0:col=Color.gray; cc=col;break;
case 1:col=Color.black; cc=col;break;
case 2:col=Color.red; cc=col;break;
case 3:col=Color.pink; cc=col;break;
case 4:col=Color.yellow;cc=col;break;
case 5:col=Color.green; cc=col;break;
case 6:col=Color.blue; cc=col;break;
}
if(e.getSource()==c2)//线型
switch(c2.getSelectedIndex())
{
case 0:linestyle=0xffff;break;
case 1:linestyle=0xff00;break;
case 2:linestyle=0xf0f0;break;
case 3:linestyle=0x6f6f;break;
}
if(e.getSource()==c3)//线宽
switch(c2.getSelectedIndex())
{
case 0:case 1:case 2:point=c3.getSelectedIndex()+1;break;
// case 3:
}
if(e.getSource()==c4)//算法
switch(f0)
{
case 1://画直线
switch(c4.getSelectedIndex())
{
case 0:f1=11;break;
case 1:f1=12;break;
case 2:f1=13;break;
}
break;
case 2://画圆
switch(c4.getSelectedIndex())
{
case 0:f1=21;break;
case 1:f1=22;break;
}
break;
case 3://区域填充
f1=31;break;
case 4://线段裁剪
f1=41;break;
}
}//itemStateChanged
//实现接口MouseListener的所有方法(5个),用于处理鼠标事件
public void mousePressed(MouseEvent e)
{
x0=e.getX();//起始点坐标
y0=e.getY();
}//mousePressed
public void mouseReleased(MouseEvent e)
{
isXOR=false;
x1=e.getX();//终止点坐标
y1=e.getY();
x00=x01=y00=y01=0;
flag=0;
switch(f1)
{
//画直线
case 11:
(new DrawLine()).DDALine(this.g,this.buffer,col,linestyle,point,x0,y0,x1,y1,isXOR);break;
case 12:
(new DrawLine()).MidLine(this.g,this.buffer,col,linestyle,point,x0,y0,x1,y1,isXOR);break;
case 13:
(new DrawLine()).BreLine(this.g,this.buffer,col,linestyle,point,x0,y0,x1,y1,isXOR);break;
//画圆
case 21:
r=(int)(Math.sqrt(Math.abs(x1-x0)*Math.abs(x1-x0)+Math.abs(y1-y0)*Math.abs(y1-y0))+0.5);
(new DrawCircle()).BijinCircle(this.g,this.buffer,col,x0,y0,r);break;
case 22:
r=(int)(Math.sqrt(Math.abs(x1-x0)*Math.abs(x1-x0)+Math.abs(y1-y0)*Math.abs(y1-y0))+0.5);
(new DrawCircle()).MidCircle(this.g,this.buffer,col,x0,y0,r);break;
//区域填充
case 31:
(new Fill()).FillArea(this,col,x0,y0);break;
//线段裁剪
case 41:
// (new DrawLine()).BreLine(this.g,this.buffer,col,0xffff,1,x0,y0,x1,y1,isXOR);
(new Cut()).LineCut(this,col,x0,y0,x1,y1);break;
}
}//mouseReleased
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
//实现接口MouseMotionListener的所有方法(2个),用于处理鼠标的拖拽和移动事件
public void mouseDragged(MouseEvent e) //实现橡皮筋技术
{
x=e.getX();y=e.getY();
l5.setText("坐标: "+x+","+y);
isXOR=true;
if(flag==0)//flag用于区别相邻的两次拖拽
{
x00=e.getX();
y00=e.getY();
flag++;
if(x01!=0&&y01!=0)
(new DrawLine()).DDALine(g,buffer,cc,0xf0f0,1,x0,y0,x01,y01,isXOR); //画旧的直线
(new DrawLine()).DDALine(g,buffer,col,linestyle,1,x0,y0,x00,y00,isXOR); //画新的直线
}
if(flag==1)
{
x01=e.getX();
y01=e.getY();
flag--;
(new DrawLine()).DDALine(g,buffer,cc,0xf0f0,1,x0,y0,x00,y00,isXOR); //消除旧的直线
(new DrawLine()).DDALine(g,buffer,col,linestyle,1,x0,y0,x01,y01,isXOR);//画新的直线
}
}//mouseDragged
public void mouseMoved(MouseEvent e)
{
x=e.getX();y=e.getY();
l5.setText("坐标: "+x+","+y);
}
//用于显示
public void paint(Graphics g)
{
if(first==0) //初始化缓冲区
{
buffer=this.createImage(width,height);
first=1;
}
g.drawImage(buffer,0,0,this);
}
public static void main(String args[])
{
draw d=new draw();
d.display();
}
}
//定义直线类
class DrawLine
{
public void DDALine( Graphics g,Image buffer,Color c,int linestyle,int point,int xu0,int yu0,int xu1,int yu1,boolean isXOR) //DDA算法
{
int mark=0x8000,n=0;
float increx,increy,x,y,length;
int i;
if(Math.abs(xu1-xu0)>Math.abs(yu1-yu0))
length=Math.abs(xu1-xu0);
else length=Math.abs(yu1-yu0);
increx=(xu1-xu0)/length;
increy=(yu1-yu0)/length;
x=xu0;
y=yu0;
/*在屏幕上画*/
if(isXOR) g.setXORMode(c);
else
{
g.setPaintMode();
g.setColor(c);
}
for(i=1;i<=length;i++)
{
n++;
if(n%16>1) mark=0x8000>>(n%16-1);
if(n%16==1) mark=0x8000;
if(n%16==0) mark=0x0001;
if((linestyle & mark)!=0)
if(length==Math.abs(xu1-xu0))
switch(point)
{
case 1: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
break;
case 2: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+1,1,1);
break;
case 3: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)-1,1,1);
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+1,1,1);
break;
}
else
switch(point)
{
case 1: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
break;
case 2: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5)+1,(int)Math.round(y+0.5),1,1);
break;
case 3: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5)-1,(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5)+1,(int)Math.round(y+0.5),1,1);
break;
}
x=x+increx;
y=y+increy;
}
/*在缓冲区中画*/
mark=0x8000;n=0;
x=xu0;
y=yu0;
g=buffer.getGraphics();
if(isXOR) g.setXORMode(c);
else
{
g.setPaintMode();
g.setColor(c);
}
for(i=1;i<=length;i++)
{
n++;
if(n%16>1) mark=0x8000>>(n%16-1);
if(n%16==1) mark=0x8000;
if(n%16==0) mark=0x0001;
if((linestyle & mark)!=0)
if(length==Math.abs(xu1-xu0))
switch(point)
{
case 1: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
break;
case 2: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+1,1,1);
break;
case 3: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)-1,1,1);
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+1,1,1);
break;
}
else
switch(point)
{
case 1: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
break;
case 2: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5)+1,(int)Math.round(y+0.5),1,1);
break;
case 3: g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5)-1,(int)Math.round(y+0.5),1,1);
g.fillOval((int)Math.round(x+0.5)+1,(int)Math.round(y+0.5),1,1);
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -