⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw.java

📁 mpl.add(new MenuItem("DDA画线")) mpl.add(new MenuItem("中点画线")) mpl.add(new MenuItem("Bresenham
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.JColorChooser;


public class draw extends Frame implements WindowListener,ActionListener,MouseListener,ItemListener,MouseMotionListener
{
	MenuBar mb;
	Menu me,mp,mh,mpl,mp2,me2;
	MenuItem me1,yanse,xianxing,xiankuan,mp3;
	Panel p0,p1,p2,p3;
	static Color col=Color.black,cc=Color.red;
	Choice c1,c2,c3;
	Label prompt1,prompt2,prompt3,lable1,lable2,lable3;
	Button b14,button1,button2,button3;
	static int A,B,C,D,x0=0,x1=0,y0=0,y1=0,width,height,p=11,q=0,x_0,x_1,y_0,y_1;
	int linestyle=0xffff,x00=0,x01=0,y00=0,y01=0;
	int point=1,flag=0,r=0;
	Dimension D1;
	Toolkit TK;
	boolean isXOR=false;//用于确定写模式,true为异或模式,false为写模式
	static Graphics g;
	Image buffer;
	int first=0;
	TextField txt1,txt2,txt3,txt4,txt5;
	Dialog dialog1,dialog2,dialog3;
	TextArea te;
		
   	public void display()

	{	//获取屏幕的宽度和高度
		TK=Toolkit.getDefaultToolkit();
		D1=TK.getScreenSize();
		//设置标题栏和窗口的大小		
		setTitle("绘图系统");
		setSize(D1.width,D1.height);
		width=D1.width;
		height=D1.height;
		//为窗口添加事件监听器
		addWindowListener(this);
		addMouseListener(this);
		addMouseMotionListener(this);
		//菜单
		mb=new MenuBar();
		setMenuBar(mb);
		//mf=new Menu("文件");
		me=new Menu("编辑");
		mp=new Menu("绘图");
	    mh=new Menu("帮助");
	    mb.add(mp);
	    mb.add(me);
	    mb.add(mh);
	    
	    
	    //绘图下面的子菜单
	    mpl=new Menu("画直线");
	    mp.add(mpl);
	    mpl.add(new MenuItem("DDA画线"));
	    mpl.add(new MenuItem("中点画线"));
	    mpl.add(new MenuItem("Bresenham画线"));
	    mpl.addActionListener(this);
	    mp2=new Menu("画圆");
	    mp.add(mp2);
	    mp2.add(new MenuItem("公式法画圆"));
	    mp2.add(new MenuItem("多边形逼近圆"));
	    mp2.add(new MenuItem("中点画圆"));
	    mp2.addActionListener(this);
	    mp3=new MenuItem("填充");
	    mp3.addActionListener(this);
	    mp.add(mp3);
	    
	    //编辑下面的子菜单
	    me1 =new MenuItem("清屏");//清屏子菜单
	    me.add(me1);
	    me1.addActionListener(this);
	   
	    
	    p0=new Panel();//画板
	    p0.setBackground(Color.pink);
	    prompt1=new Label("颜色");
	    prompt2=new Label("线型");
	    prompt3=new Label("线宽");
	    
	    //颜色
	    c1=new Choice();
	    c1.addItem("灰");
	    c1.addItem("黑");
	    c1.addItem("红");
	    c1.addItem("粉红");
	    c1.addItem("黄");
	    c1.addItem("绿");
	    c1.addItem("蓝");
	    c1.addItem("自定义");
	    c1.select("黑");
	    
	    
	    	
	    
	    //线型
	    c2=new Choice();
	    c2.addItem("___________");
	    c2.addItem("__  __  __ ");
	    c2.addItem("_ _ _ _ _ _");
	    c2.addItem("___.___.___");
	    c2.addItem("自定义");
	    
	    	
	    	
	    	
	    //线宽
	    c3=new Choice();
	    c3.addItem("1");
	    c3.addItem("2");
	    c3.addItem("3");
	    c3.addItem("自定义");
	    
	    
	    p0.add(prompt1);     p0.add(c1);       
	    p0.add(prompt2);     p0.add(c2);
	    p0.add(prompt3);     p0.add(c3);
	    add(p0,BorderLayout.NORTH);   
	    c1.addItemListener(this);
	    c2.addItemListener(this);
	    c3.addItemListener(this);
	    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()=="DDA")p=11;
        if(e.getActionCommand()=="中点画线")p=12;
        if(e.getActionCommand()=="Bresenham画线")p=13;
         if(e.getActionCommand()=="公式法画圆")  p=21;      ;
        if(e.getActionCommand()=="多边形逼近圆") p=22;
        if(e.getActionCommand()=="中点画圆")  p=23;
       if(e.getActionCommand()=="填充")  p=3;
       

	 } //actionPerformed
	  //将数字字符串转换为数字
	 public long ToData(String s)
	 {
	 	int k=s.length();
	 	long data=s.charAt(0)-48;//charAt:返回指定位置的字符
	 	for(int i=1;i<=k-1;i++)
	 	   {
	 	   	data=10*data+s.charAt(i)-48;
	 	   }
	 	return data;
	 }
	 
	 
	 //实现接口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;
	 	    case 7:{Color col= JColorChooser.showDialog(te, "字体颜色", Color.BLACK);
	 	    cc=col;break;}
	 	  }
	        
	    	
	 	 
	 	  
	 	 if(e.getSource()==c3)//线宽
	 	 {  
	 	 
	 	 	String inpute=JOptionPane.showInputDialog(this,"please input xiankuan");
	 	   point=(int)(ToData(inpute));
	 	   }
	 	   
	 	   
	 	 
	 	 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;
	 	    case 4:{String input=JOptionPane.showInputDialog(this,"please input xianxing");
	 	           linestyle=(int)(ToData(input));
	 	           break;}
	 	           
	 	   	}//if
	 	 
	 	   
	   }//itemStateChanged
	
	//实现接口MouseListener的所有方法(5个),用于处理鼠标事件    
	public void mousePressed(MouseEvent e) 
	{
		x0=e.getX();
		y0=e.getY();
		x_0=x0;//起始点坐标
		y_0=y0;
		 	
	 }//mousePressed   
	 
	  
	 public void mouseReleased(MouseEvent e) 
	 {
	 	int m,n,r=0;
	 	
	 	isXOR=false;
	 	x1=e.getX();
	 	y1=e.getY();
	 	x_1=x1;//终止点坐标
		y_1=y1;
       
		x01=0;
		y01=0;
		x00=0;
		y00=0;
		flag=0;
		 
	 	
      if(p==11)//DDA   
      
      	(new DrawLine()).DDALine(this.g,this.buffer,col,linestyle,point,x0,y0,x1,y1,isXOR);
      
      if(p==12)//中点画线  
      {
      (new DrawLine()).midLine(this.g,this.buffer,col,linestyle,point,x0,y0,x1,y1,isXOR);} 
      
      if(p==13)//bresenham画线  
      {
      (new DrawLine()).BresenhamLine(this.g,this.buffer,col,linestyle,point,x0,y0,x1,y1,isXOR);}
       if(p==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()).midCircle1(this.g,this.buffer,col,linestyle,point,x0,y0,r);}
      if(p==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()).midCircle2(this.g,this.buffer,col,linestyle,point,x0,y0,r);}
      if(p==23)//中点画圆  
      {r=(int)(Math.sqrt(Math.abs(x1-x0)*Math.abs(x1-x0)+Math.abs(y1-y0)*Math.abs(y1-y0))+0.5);
      (new DrawCircle()).midCircle3(this.g,this.buffer,col,linestyle,point,x0,y0,r);}
      if(p==3)//填充图形
      {
      	Fill fill=new Fill();  
      fill.fill_area(this,x1,y1,col);
      }
    }//mouseReleased

     
    public void mouseClicked(MouseEvent e){ }
    
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
            
    //实现接口MouseMotionListener的所有方法(2个),用于处理鼠标的拖拽和移动事件  
    public void mouseDragged(MouseEvent e) //实现橡皮筋技术
    {  
       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){ }
    
   
   //用于显示 
   public void paint(Graphics g)
	{
		
		if(first==0) //初始化缓冲区
    	{
    		buffer=this.createImage(width,height);
    		first=1;
    	}    
    	g.drawImage(buffer,0,0,this);
    	
	
   } 
   
/****main*/
    public static void main(String args[])
    {    
    	draw f= new draw();
    	f.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,j;
                  
        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;
        	     case 4: {if(point%2==0){
        	     	        for(j=0;j<=j/2;j++)
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+j,1,1);
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)-j+1,1,1);}
        	     	      else {for(j=0;j<=j/2;j++)
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+j,1,1);
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)-j,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;
        	     case 4: {if(point%2==0){
        	     	        for(j=0;j<=j/2;j++)
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+j,1,1);
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)-j+1,1,1);}
        	     	      else {for(j=0;j<=j/2;j++)
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)+j,1,1);
        	     	        g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5)-j,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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -