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

📄 画图.txt

📁 java编的画图工具
💻 TXT
📖 第 1 页 / 共 2 页
字号:
      new Integer(KeyEvent.VK_L),
     KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
     
    Action qianbiAction = new MyAction("qianbi", jbtqianbi,
      "铅笔",
      new Integer(KeyEvent.VK_C),
      KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
      
    Action PrintAction = new MyAction("Print", jbPrint,
      "涮子",
      new Integer(KeyEvent.VK_R),
      KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
      
    Action xiguanAction = new MyAction("xiguan", jbxiguan,
      "吸管",
      new Integer(KeyEvent.VK_A),
      KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); 
      
    Action zitiAction = new MyAction("ziti", jbziti,
      "字体",
      new Integer(KeyEvent.VK_Z),
      KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK)); 
      
    Action zhixianAction = new MyAction("zhixian", jbzhixian,
      "直线",
      new Integer(KeyEvent.VK_W),
      KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
      
    Action tuoyuanAction = new MyAction("tuoyuan", jbtuoyuan,
      "椭圆",
      new Integer(KeyEvent.VK_T),
      KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
      
    Action juxingAction = new MyAction("juxing", jbjuxing,
      "矩形",
      new Integer(KeyEvent.VK_J),
      KeyStroke.getKeyStroke(KeyEvent.VK_J, ActionEvent.CTRL_MASK));
      
    Action duopianxingAction = new MyAction("duopianxing", jbduopianxing,
      "多边形",
      new Integer(KeyEvent.VK_D),
      KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));  
      
    Action yuanjiaojuxingAction = new MyAction("yuanjiaojuxing", jbyuanjiaojuxing,
      "圆角矩形",
      new Integer(KeyEvent.VK_Y),
      KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK)); 
   jcb1=new Choice();
   jcb1.add("1");
   jcb1.add("2");
   jcb1.add("3");
   jcb1.add("4");
   jcb1.add("5");
   jcb2=new Choice();
   jcb2.add("1");
   jcb2.add("2");
   jcb2.add("3");
   jcb2.add("4");
   jcb2.add("5");
   
    jcb3=new Choice();
    jcb3.add("不填充");
    jcb3.add("填充");
    JLabel jTable3=new JLabel("是否填充");
    JLabel jTable1=new JLabel("画笔大小");
    JLabel jTable2=new JLabel("橡皮大小");
    jcb1.addItemListener(this);
    jcb2.addItemListener(this);
    jcb3.addItemListener(this);
    JToolBar jToolBar1 = new JToolBar(JToolBar.VERTICAL);//工具栏
   
    jToolBar1.setLayout(new GridLayout(8,2));
    jToolBar1.add(xiangpiAction);
    jToolBar1.add(qianbiAction);
    jToolBar1.add(PrintAction);
    jToolBar1.add(xiguanAction);
    jToolBar1.add(zitiAction);
    jToolBar1.add(zhixianAction);
    jToolBar1.add(tuoyuanAction);
    jToolBar1.add(juxingAction);
    jToolBar1.add(duopianxingAction);
    jToolBar1.add(yuanjiaojuxingAction);
    jToolBar1.add(jTable1);
    jToolBar1.add(jcb1);
    jToolBar1.add(jTable2);
    jToolBar1.add(jcb2);
    jToolBar1.add(jTable3);
    jToolBar1.add(jcb3);
    add(jToolBar1, BorderLayout.WEST);  
      
   ////////////////////////////////////////////////////////////////////////////////    
      yanseRed = new JButton("Red", redicon);
     
     yanseBlack = new JButton("Black", blackicon);
      
     yanseBule = new JButton("Bule", buleicon);
      
    yanseCyan = new JButton("Cyan", cyanicon); 
      
     yanseDarkGray = new JButton("DarkGray", drakgrayicon); 
      
     yanseGray = new JButton("Gray", grayicon);
      
     yanseGreen = new JButton("Green", greenicon);
      
    yanseLightGray = new JButton("lightGray", lightgrayicon);
      
     yanseMagenta = new JButton("magenta", magentaicon);  
      
    yanseOrange = new JButton("orange", orangeicon); 
     
     yansePink = new JButton("pink", pinkicon);
     yanseYellow = new JButton("yellow", yellowicon); 
    SecondListender secondListender=new SecondListender();
    yanseRed.addActionListener(secondListender);
    yanseBlack.addActionListener(secondListender);
    yanseBule.addActionListener(secondListender);
    yanseCyan.addActionListener(secondListender);
    yanseDarkGray.addActionListener(secondListender);
    yanseGray.addActionListener(secondListender);
    yanseGreen.addActionListener(secondListender);
    yanseLightGray.addActionListener(secondListender);
    yanseMagenta.addActionListener(secondListender);
    yanseOrange.addActionListener(secondListender);
    yansePink.addActionListener(secondListender);
    yanseYellow.addActionListener(secondListender);
     
    JToolBar jToolBar2 = new JToolBar();
    jToolBar2.setFloatable(true);
    jToolBar2.setLayout(new GridLayout(2,6));
    jToolBar2.add(yanseRed);
    jToolBar2.add(yanseBlack);
    jToolBar2.add(yanseBule);
    jToolBar2.add(yanseCyan);
    jToolBar2.add(yanseDarkGray);
    jToolBar2.add(yanseGray);
    jToolBar2.add(yanseGreen);
    jToolBar2.add(yanseLightGray);
    jToolBar2.add(yanseMagenta);
    jToolBar2.add(yanseOrange);
    jToolBar2.add(yansePink);
    jToolBar2.add(yanseYellow);
    add(jToolBar2, BorderLayout.SOUTH);  
    
   /////////////////////////////////////////////////////////////
   
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) { 
            g=getGraphics();
        	if(theButton.equals("xiangpi"))//橡皮
        	{
        		g.setColor(getBackground());
        		if(xiangpiFirst&&name2!="2"){
        			name2="1";
        			xiangpiFirst=false;
        		}
        	   if(name2=="1")
        	    g.fillOval(lineStart.x,lineStart.y,2,2);
        	   else if(name2=="2")
        	    g.fillOval(lineStart.x,lineStart.y,5,5);
        	   else if(name2=="3")
        	     g.fillOval(lineStart.x,lineStart.y,8,8);
        	   else if(name2=="4")
        	     g.fillOval(lineStart.x,lineStart.y,11,11);
        	   else if(name2=="5")
        	     g.fillOval(lineStart.x,lineStart.y,14,14);
	    	
	    	     lineStart.move(e.getX(),e.getY(),Color.red,50);
        	 }
        	   else if(theButton.equals("qianbi"))//铅笔
        	  {  
        	     g.setColor(cc); 
        	    if(qianbiFirst&&name1!="2"){
        		 name1="1";
        		  qianbiFirst=false;
        	 }
        	   if(name1=="1")
        	    g.drawLine(lineStart.x,lineStart.y,e.getX(),e.getY());
        	   else if(name1=="2")
        	    g.fillOval(lineStart.x,lineStart.y,2,2);
        	   else if(name1=="3")
        	     g.fillOval(lineStart.x,lineStart.y,5,5);
        	   else if(name1=="4")
        	     g.fillOval(lineStart.x,lineStart.y,8,8);
        	   else if(name1=="5")
        	     g.fillOval(lineStart.x,lineStart.y,11,11);
	    		lineStart.move(e.getX(),e.getY(),Color.red,50);	
        	}
        	   else if(theButton.equals("Print")){//刷子
                 g.setColor(cc);
                 g.fillOval(e.getX()-10,e.getY()-10,20,20);
	             lineStart.move(e.getX(),e.getY(),Color.red,50);
        	}	
	    }
      });
      
   
     addMouseListener(new MouseAdapter() { 
     public void mousePressed(MouseEvent e) { 
        if(dianFirst){
        	theButton="qianbi";
        	dianFirst=false;
        }
       if(text1Int!=-1){//画抛物线
          	    g.setColor(cc);
    		 	g.drawLine(150,300,690,300);
    		 	g.drawLine(375,10,375,450);
    		 	g.drawLine(370,15,375,10);
    		 	g.drawLine(380,15,375,10);
    		 	g.drawLine(685,295,690,300);
    		 	g.drawLine(685,305,690,300);
    		 	Polygon p=new Polygon();
		        double scaleFactor=0.1;
	         	for(int x=-100;x<=100;x++){
	        	p.addPoint(x+375,(int)(300-(int)text1Int*(scaleFactor*x*x)-(int)text2Int));	
		        }
		        g.drawPolygon(p.xpoints,p.ypoints,p.npoints);
		
    		 	text1Int=-1;
    		 }
     if(text3Int!=-1){//画直线
     	        g.setColor(cc);
    		 	g.drawLine(150,300,690,300);
    		 	g.drawLine(375,10,375,450);
    		 	g.drawLine(370,15,375,10);
    		 	g.drawLine(380,15,375,10);
    		 	g.drawLine(685,295,690,300);
    		 	g.drawLine(685,305,690,300);
    		 	Polygon p=new Polygon();
		        double scaleFactor=0.1;
	         	for(int x=-100;x<=100;x++){
	        	p.addPoint(x+375,(int)(300-(int)text3Int*x-(int)text4Int));	
		        }
		       g.drawPolygon(p.xpoints,p.ypoints,p.npoints);
                text3Int=-1;
     }
     if(text5Int!=-1){//画双曲线
     	  g.setColor(cc);
    	  g.drawLine(150,300,690,300);
    	  g.drawLine(375,10,375,450);
    	  g.drawLine(370,15,375,10);
    	  g.drawLine(380,15,375,10);
    	  g.drawLine(685,295,690,300);
    	  g.drawLine(685,305,690,300);
    	  Polygon p=new Polygon();
		   double scaleFactor=0.1;
	       for(int x=-100;x<=-text5Int*Math.sqrt(10);x++){
	       	p.addPoint(x+375,(int)(300-Math.sqrt((text6Int*text6Int*(scaleFactor*x*x-text5Int*text5Int)/(text5Int*text5Int)))));	
		   }
		   g.drawPolyline(p.xpoints,p.ypoints,p.npoints);
		   for(int x=-100;x<=-text5Int*Math.sqrt(10);x++){
	         p.addPoint(x+375,(int)(300+Math.sqrt((text6Int*text6Int*(scaleFactor*x*x-text5Int*text5Int)/(text5Int*text5Int)))));
		   }
		   g.drawPolyline(p.xpoints,p.ypoints,p.npoints);
		  for (double x=text5Int*Math.sqrt(10);x<=100;x++){
		  	p.addPoint((int)x+375,(int)(300-Math.sqrt((text6Int*text6Int*(scaleFactor*x*x-text5Int*text5Int)/(text5Int*text5Int)))));
		  }
		    g.drawPolyline(p.xpoints,p.ypoints,p.npoints);
		  for (double x=text5Int*Math.sqrt(10);x<=100;x++){
		  	p.addPoint((int)x+375,(int)(300+Math.sqrt((text6Int*text6Int*(scaleFactor*x*x-text5Int*text5Int)/(text5Int*text5Int)))));
		  }
		    g.drawPolyline(p.xpoints,p.ypoints,p.npoints); 
           text5Int=-1;
     }
     if(text7Int!=-1){//画椭圆
     	  g.setColor(cc);
    	  g.drawLine(150,300,690,300);
    	  g.drawLine(375,10,375,450);
    	  g.drawLine(370,15,375,10);
    	  g.drawLine(380,15,375,10);
    	  g.drawLine(685,295,690,300);
    	  g.drawLine(685,305,690,300);
    	  Polygon p=new Polygon();
		   double scaleFactor=0.1;
		   for(int x=-text7Int;x<=text7Int;x++){
		   	p.addPoint((int)x+375,(int)(300-(text8Int/text7Int)*Math.sqrt((text7Int*text7Int-x*x))));
		   	}
		     g.drawPolyline(p.xpoints,p.ypoints,p.npoints);
		    for(int x=-text7Int;x<=text7Int;x++){
		   	p.addPoint((int)x+375,(int)(300+(text8Int/text7Int)*Math.sqrt((text7Int*text7Int-x*x))));
		   	}
		     g.drawPolyline(p.xpoints,p.ypoints,p.npoints);  
            text7Int=-1;
         }       
      if(theButton.equals("ziti")){
             g.setColor(cc);
             g.drawString(msr,e.getX(),e.getY());
            }
       //if(theButton.equals("xiguan")){//吸管
       	// cc=huatu.getForeground();
      // }
       if(theButton.equals("zhixian")){//直线
       	g.setColor(cc);
       	//g.drawLine(lineStart.x,lineStart.y,e.getX(),e.getY());
       	lineStart.move(e.getX(),e.getY(),Color.red,50);	
       }
       if(theButton.equals("duopianxing")){//多边性
       	g.setColor(cc);
       	g.drawLine(lineStart.x,lineStart.y,e.getX(),e.getY());
       }
       lineStart.move(e.getX(),e.getY(),Color.red,50);		
      } 
     public void mouseReleased(MouseEvent e) { 
       if(isFirst){
       	isFirst=false;
       	lineStart.move(e.getX(),e.getY(),Color.red,50);	
       }
       if(theButton.equals("zhixian")){//直线
       	g.setColor(cc);
       	g.drawLine(lineStart.x,lineStart.y,e.getX(),e.getY());
       	lineStart.move(e.getX(),e.getY(),Color.red,50);	
       }
     if(theButton.equals("tuoyuan")){//椭圆
       	g.setColor(cc);
       	if(tuoyuanFirst&&name3!="填充"){
       		name3="不填充";
       		tuoyuanFirst=false;
       	}
       	
      if(name3=="不填充")
       	g.drawOval(lineStart.x,lineStart.y,Math.abs((e.getX()-lineStart.x)),Math.abs((e.getY()-lineStart.y)));
      if(name3=="填充")
      g.fillOval(lineStart.x,lineStart.y,Math.abs((e.getX()-lineStart.x)),Math.abs((e.getY()-lineStart.y)));
       
     }
     if(theButton.equals("duopianxing")){//多边性
       	g.setColor(cc);
       	g.drawLine(lineStart.x,lineStart.y,e.getX(),e.getY());
       }
       if(theButton.equals("juxing")){//矩形
       	g.setColor(cc);
       	if(juxingFirst&&name3!="填充"){
       		name3="不填充";
       		juxingFirst=false;
       	}
       	if(name3=="不填充")
       	  g.drawRect(lineStart.x,lineStart.y,Math.abs((e.getX()-lineStart.x)),Math.abs((e.getY()-lineStart.y)));
        if(name3=="填充")
          g.fillRect(lineStart.x,lineStart.y,Math.abs((e.getX()-lineStart.x)),Math.abs((e.getY()-lineStart.y)));
       
       }
       if(theButton.equals("yuanjiaojuxing")){//圆角矩形
       	g.setColor(cc);
       	if(yuanjiaoFirst&&name3!="填充"){
       		name3="不填充";
       		yuanjiaoFirst=false;
       	}
       	
        if(name3=="不填充")
       	g.drawRoundRect(lineStart.x,lineStart.y,Math.abs((e.getX()-lineStart.x)),Math.abs((e.getY()-lineStart.y)),Math.abs((e.getX()-lineStart.x))/2,Math.abs((e.getY()-lineStart.y))/2);
       if(name3=="填充")
        g.fillRoundRect(lineStart.x,lineStart.y,Math.abs((e.getX()-lineStart.x)),Math.abs((e.getY()-lineStart.y)),Math.abs((e.getX()-lineStart.x))/2,Math.abs((e.getY()-lineStart.y))/2);
       
       }
       
    lineStart.move(e.getX(),e.getY(),Color.red,50);		
   } 
   }); 
    }
    
   private class MyAction extends AbstractAction {
    String name;
    
    MyAction(String name, Icon icon) {
      super(name, icon);
      this.name = name;
    }
    MyAction(String name, Icon icon, String desc, Integer mnemonic,
      KeyStroke accelerator) {
      super(name, icon);
      putValue(Action.SHORT_DESCRIPTION, desc);
      putValue(Action.MNEMONIC_KEY, mnemonic);
      putValue(Action.ACCELERATOR_KEY, accelerator);
      this.name = name;
    }
   
   
    public void actionPerformed(ActionEvent e) {
            theButton=name;
            if(name.equals("ziti")){
            msr=JOptionPane.showInputDialog("请输入你要画的字体");
            }
      	    
      }
  
  }
   public void itemStateChanged(ItemEvent e){
   	    if(e.getSource()==jcb1)
      	   name1=jcb1.getSelectedItem();
      	else if(e.getSource()==jcb2)
      	   name2=jcb2.getSelectedItem();
      	else if(e.getSource()==jcb3)
           name3=jcb3.getSelectedItem();
    }
    
	}
  static class SecondListender implements ActionListener{
	 public void actionPerformed(ActionEvent e){
	 	if(e.getActionCommand().equals("Red"))	{
	 	cc=Color.red;
	 	System.out.println("ww");	
	 	}
	 	
	 	if(e.getActionCommand().equals("Black"))	
	 	cc=Color.black;
	 	if(e.getActionCommand().equals("Bule"))	
	 	cc=Color.blue;
	 	if(e.getActionCommand().equals("Cyan"))	
	 	cc=Color.cyan;
	 	if(e.getActionCommand().equals("DarkGray"))	
	 	cc=Color.darkGray;
	 	if(e.getActionCommand().equals("Gray"))	
	 	cc=Color.gray;
	 	if(e.getActionCommand().equals("Green"))	
	 	cc=Color.green;
	 	if(e.getActionCommand().equals("lightGray"))	
	 	cc=Color.lightGray;
	 	if(e.getActionCommand().equals("magenta"))	
	 	cc=Color.magenta;
	 	if(e.getActionCommand().equals("orange"))	
	 	cc=Color.orange;
	 	if(e.getActionCommand().equals("pink"))	
	 	cc=Color.pink;
	 	if(e.getActionCommand().equals("yellow"))	
	 	cc=Color.yellow;
	 	
    }	
	}
	}
	
	

	
	

⌨️ 快捷键说明

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