图形学课程设计

Code:
  1. package  myproject.lesson;   
  2. import   myproject.*;   
  3. import java.awt.*;   
  4. import javax.swing.*;   
  5. import java.awt.Graphics;   
  6. import java.awt.Point;   
  7. import java.awt.event.*;   
  8.   
  9.   
  10. //   
  11. public class Draw extends Canvas implements  MouseListener,MouseMotionListener,ActionListener{   
  12.     private static int x0,x1,y0,y1,r=100;   
  13.     private float buffer1,buffer2,buffer3,buffer4;   
  14.     private  static int type=1;   
  15.     private static Draw dr;   
  16.     private static float u1,u2;   
  17.     private static JButton button1,button2,button3,button4,button5,clear;   
  18.     private static User s;   
  19.     private static JSplitPane  split;    
  20.     private  int a,b;    
  21.        
  22.     public  Draw()  //画布类的构造函数   
  23.     {      
  24.         //this.setSize(400,300);   
  25.         //this.setBounds(0,0,10,70);   
  26.         this.setBounds(50,50,700,780);   
  27.         this.setBackground(Color.white);   
  28.         this.addMouseListener(this);   
  29.         this.addMouseMotionListener(this);   
  30.            
  31.        
  32.     }   
  33.        
  34.        
  35.     //主函数   
  36.     public static void main (String[] args) {   
  37.         JFrame.setDefaultLookAndFeelDecorated(true);   
  38.         JFrame fr=new JFrame();   
  39.         fr.setTitle("画图小程序");   
  40.         //fr.setLayout(null);   
  41.          button1=new JButton(new ImageIcon("1.jpg"));   
  42.          //button1.setForeground(Color.blue);   
  43.             
  44.          button2=new JButton(new ImageIcon("33.jpg"));   
  45.          //button2.setForeground(Color.blue);   
  46.             
  47.          button3=new JButton(new ImageIcon("43.jpg"));   
  48.          //button3.setForeground(Color.blue);   
  49.             
  50.          button4=new JButton("矩形");   
  51.          button5=new JButton("直线裁剪");   
  52.          clear=new JButton("清除");   
  53.            
  54.         JToolBar bar=new JToolBar();   
  55.         bar.add(button1);   
  56.         bar.add(button2);   
  57.         bar.add(button3);   
  58.         bar.add(button4);   
  59.         bar.add(button5);   
  60.         bar.add(clear);   
  61.         dr=new Draw();   
  62.          TabbedPaneFrame tabbe=new  TabbedPaneFrame();   
  63.         split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tabbe,dr);   
  64.         split.setDividerLocation(200);   
  65.         fr.add(bar,BorderLayout.NORTH);  //添加工具条   
  66.            
  67.         //fr.add("West",new TabbedPaneFrame());   //图形面板   
  68.         button1.setEnabled(false);   
  69.            
  70.             
  71.          //添加事件   
  72.         button1.addActionListener(dr);   
  73.         button2.addActionListener(dr);   
  74.         button3.addActionListener(dr);   
  75.         button4.addActionListener(dr);   
  76.         button5.addActionListener(dr);   
  77.         clear.addActionListener(dr);   
  78.          //添加菜单栏   
  79.         fr.setJMenuBar(MenuExmple.MFrame());   
  80.        
  81.         //添加画布   
  82.         //fr.add(dr,BorderLayout.CENTER);   
  83.         fr.add(split);   
  84.         fr.setBounds(50,50,1100,700);   
  85.         fr.setResizable(false);   
  86.         //fr.setLocation(150,30);   
  87.         fr.setVisible(true);   
  88.            
  89.         //关闭窗口事件   
  90.         fr.addWindowListener(new WindowAdapter(){   
  91.             public  void windowClosing(WindowEvent e){   
  92.               System.exit(0);   
  93.             }});   
  94.                
  95.            
  96.           
  97.     //  fr.validate();   
  98.     }   
  99.        
  100.        
  101.     //按钮触发事件   
  102.     public void  actionPerformed(ActionEvent e){   
  103.         JButton btn=(JButton) e.getSource();   
  104.         if(btn==button1){   
  105.             type=1//btn.setVisible(true);   
  106.             button1.setEnabled(false);   
  107.             button2.setEnabled(true);   
  108.             button3.setEnabled(true);   
  109.             button4.setEnabled(true);   
  110.             button5.setEnabled(true);   
  111.         }   
  112.         if(btn==button2){   
  113.                
  114.              s=new User();   
  115.             s.setAlwaysOnTop(true);   
  116.             type=2;   
  117.                    
  118.             button2.setEnabled(false);   
  119.             button1.setEnabled(true);   
  120.             button3.setEnabled(true);   
  121.             button4.setEnabled(true);   
  122.             button5.setEnabled(true);   
  123.         }   
  124.         if(btn==button3){   
  125.             a=TabbedPaneFrame.getA();   
  126.             b=TabbedPaneFrame.getB();    
  127.            
  128.              System.out.println(a);   
  129.              System.out.println(b);   
  130.              
  131.             type=3;   
  132.             button3.setEnabled(false);   
  133.             button1.setEnabled(true);   
  134.             button2.setEnabled(true);   
  135.             button4.setEnabled(true);   
  136.             button5.setEnabled(true);   
  137.         }   
  138.            
  139.        if(btn==button4){   
  140.             type=4;   
  141.             button4.setEnabled(false);   
  142.             button1.setEnabled(true);   
  143.             button2.setEnabled(true);   
  144.             button3.setEnabled(true);   
  145.             button5.setEnabled(true);   
  146.        }   
  147.        if(btn==button5){   
  148.            if(buffer1==0&&buffer2==0){   
  149.                
  150.             JOptionPane.showMessageDialog(dr,"请先画出一条直线");   
  151.            }   
  152.               
  153.             type=5;   
  154.             button5.setEnabled(false);   
  155.             button4.setEnabled(true);   
  156.             button1.setEnabled(true);   
  157.             button2.setEnabled(true);   
  158.             button3.setEnabled(true);   
  159.        }   
  160.        if(btn==clear){   
  161.            type=-1;   
  162.            buffer1=0;buffer2=0;   
  163.            button5.setEnabled(true);   
  164.             button4.setEnabled(true);   
  165.             button1.setEnabled(true);   
  166.             button2.setEnabled(true);   
  167.             button3.setEnabled(true);   
  168.             repaint();   
  169.        }   
  170.            
  171.            
  172.            
  173.     }   
  174.            
  175.        
  176. //鼠标按下时,返回鼠标的坐标。   
  177. public void mousePressed(MouseEvent e){    
  178. x0=e.getX();    
  179. y0=e.getY();    
  180.  //repaint();   
  181. }    
  182. //鼠标释放时,放回鼠标的坐标。   
  183. public void mouseReleased(MouseEvent e){    
  184. x1=e.getX();    
  185. y1=e.getY();   
  186. repaint();     
  187.     
  188. }    
  189. public void mouseEntered(MouseEvent e){    
  190.   
  191. }    
  192. public void mouseExited(MouseEvent e){    
  193.   
  194. }    
  195. public void mouseClicked(MouseEvent e){    
  196.   
  197. }    
  198.   
  199. public void mouseMoved(MouseEvent e){    
  200. //int flag=1;    
  201. //x1=e.getX();    
  202. //y1=e.getY();    
  203. //repaint();    
  204. }    
  205.   
  206. //鼠标按下到释放的过程。   
  207. public void mouseDragged(MouseEvent e){    
  208. //int flag=2;    
  209. x1=e.getX();    
  210. y1=e.getY();    
  211.   
  212. //repaint();    
  213. }    
  214.   
  215.   
  216.   
  217.     //生成画布时调用的画图方法。   
  218.     public  void  paint(Graphics e){   
  219.            //super.paint(e);   
  220.            e.setColor(TabbedPaneFrame.getColor());   
  221.               
  222.     
  223.           switch(type){   
  224.                
  225.               case 1:  ddaline(x0,y0,x1,y1,e);buffer1=x0;   
  226.               break//直线扫描   
  227.               case 2:  midpointcircle(x0,y0, s.getRadius(),e);break;  //中点画圆   
  228.               case 3:  MidpointEllipe(x0,y0,a,b,e);break;//椭圆   
  229.               case 4: fillRectangle(x0, y0,x1,y1, e);   
  230.                        break;//矩形的扫描   
  231.               case 5:   e.clearRect(0,0,dr.getWidth(),dr.getHeight());   
  232.                        //floodFill(x0,y0, e); //BoundaryFill4(x0,y0,Color.blue.getRGB(),Color.red,e);   
  233.                       lineClip(buffer1,buffer2,buffer3,buffer4,x0,y0,x1,y1,e);   
  234.                       type=0;   
  235.                          
  236.                       break;   
  237.               default: e.clearRect(0,0,dr.getWidth(),dr.getHeight()); System.out.print("erro!");   
  238.           }   
  239.              
  240.              
  241.     }   
  242.        
  243.   
  244.        
  245.     //刷新画布调用的方法   
  246.     public void update(Graphics  e){    
  247.         //super.paint(e);   
  248.          paint(e);   
  249.        
  250.     }   
  251.        
  252.   
  253.        
  254.     //DDA画线方法   
  255.     public  void DDAline(int x0,int y0,int x1,int y1,Graphics g){   
  256.         int x,y3;   
  257.         float dx=x1-x0,dy=y1-y0;   
  258.         float k=dy/dx,y=y0,x3=x0;   
  259.         if(k>=-1||k<=1)   
  260.         {   
  261.            
  262.         for(x=x0;x<=x1;x++){   
  263.         g.drawLine(x,(int) (y+0.5),x,(int) (y+0.5));   
  264.         y=y+k;   
  265.         }   
  266.         }   
  267.        
  268.         else  
  269.        
  270.             for(y3=y0;y3<=y1;y3++){   
  271.                 g.drawLine((int) (x3+0.5),y3,(int) (x3+0.5),y3);   
  272.                 x3=x3+(1/k);   
  273.             }   
  274.            
  275.            
  276.     }   
  277.        
  278.        
  279.   
  280.   
  281.   
  282. //中点画椭圆算法   
  283. public void pointellipse(int x0,int y0, int x1,int y1,Graphics g)//椭圆的对称性。   
  284. {   
  285. //g.setColor(Color.blue);   
  286. g.drawLine(x0+x1,y0+y1,x0+x1,y0+y1);  //椭圆上的一个点,   
  287. g.drawLine(x0+x1,y0-y1,x0+x1,y0-y1);  //跟给出点关于x轴对称的点;    
  288. g.drawLine(x0-x1,y0+y1,x0-x1,y0+y1);   
  289. g.drawLine(x0-x1,y0-y1,x0-x1,y0-y1);   
  290.   
  291. }   
  292.   
  293. //以点(x0,y0)为中心,画椭圆   
  294.   void MidpointEllipe(int x0,int y0,int a,int b,Graphics e)   
  295.     {     
  296.        int x,y;  float d1,d2;   
  297.         x = 0; y = b;   
  298.         d1 = b*b +a*a*(-b+0.25f);   
  299.            
  300.      pointellipse(x0,y0,x,y,e);   
  301.        while(b*b*(x+1)< a*a*(y-0.5f))            //用椭圆点上的法向量判断它是y分量增加的快的话   
  302.        {      if (d1<0){   
  303.           
  304.             d1+=b*b*(2*x+3); x++;  }   
  305.             else  {  d1 +=(b*b*(2*x+3)+a*a*(-2*y+2)) ;   
  306.                 x++;  y--;  }    
  307.         pointellipse(x0,y0,x,y,e);   
  308.     }//上部分   
  309.        
  310.        
  311.       //下部分   
  312.         d2=(float)Math.sqrt(b*(x+0.5f))+(float)Math.sqrt(a*(y-1))-(float)Math.sqrt(a*b);   
  313.        while(y >0)   
  314.       {     
  315.         if (d2 <0) { d2 +=b*b*(2*x+2f)+a*a*(-2*y+3f);   
  316.               x++;  y--;}   
  317.           else  {d2 += a*a*(-2*y+3f);  y--; }   
  318.            pointellipse(x0,y0,x,y,e);    
  319.            
  320.         }   
  321.         }   
  322.   
  323.   
  324.   
  325. //中点画圆算法。   
  326.  public void circlepoint(int x0,int y0,int x1,int y1,Graphics e) /*利用圆的八对称性*/  
  327. {   
  328. e.drawLine(x0+x1,y0+y1,x0+x1,y0+y1);   
  329. e.drawLine(x0+y1,y0+x1,x0+y1,y0+x1);   
  330. e.drawLine(x0+x1,y0-y1,x0+x1,y0-y1);   
  331. e.drawLine(x0+y1,y0-x1,x0+y1,y0-x1);   
  332. e.drawLine(x0-x1,y0+y1,x0-x1,y0+y1);   
  333. e.drawLine(x0-y1,y0+x1,x0-y1,y0+x1);   
  334. e.drawLine(x0-x1,y0-y1,x0-x1,y0-y1);   
  335. e.drawLine(x0-y1,y0-x1,x0-y1,y0-x1);   
  336. }   
  337.   
  338. public void midpointcircle(int x0,int y0,int r,Graphics g)   
  339. {   
  340. int x,y,d,same,change;   
  341. x=0;y=r;d=5-4*r;same=12;change=20-8*r;   /*从点(0,r)画到点(r/1.413,r/1.414)*/  
  342. circlepoint(x0,y0,x,y,g);   
  343. while(y>x)   
  344. {   
  345. if(d>0)           /*下一点y要变*/  
  346. {   
  347. d+=change;   
  348. change+=16;   
  349. y--;   
  350. }   
  351. else             /*下一点y不变*/  
  352. {   
  353. d+=same;   
  354. change+=8;   
  355. }   
  356. x++;   
  357. same+=8;   
  358. circlepoint(x0,y0,x,y,g);   
  359. }   
  360. }   
  361.   
  362. //画矩形的算法   
  363. public  void  fillRectangle(int x0,int y0,int width,int height,Graphics e){   
  364.     int x,y;   
  365.      // e.setColor(Color.blue);   
  366.          
  367.          
  368.         for(y=y0;y<=height;y++){   
  369.                
  370.         for(x=x0;x<=width;x++){   
  371.             e.drawLine(x,y,x,y);   
  372.         }   
  373.     }   
  374.        
  375.         
  376.        
  377. }   
  378.   
  379.   
  380.   
  381. //边界填充算法   
  382. void BoundaryFill4(int x,int y,int  boundaryColor,Color red,Graphics e)   
  383. {   int color=0;   
  384.             
  385.             
  386.     try{   
  387.       Robot robot=new Robot();   
  388.       Color co=robot.getPixelColor(x,y);   
  389.       color=co.getRGB();   
  390.       }   
  391.       catch(AWTException ea){   
  392.         ea.printStackTrace();   
  393.       }   
  394.       e.setColor(red);   
  395.     if((color != boundaryColor) && (color != red.getRGB()))   
  396.     {   e.drawLine(x,y,x,y);   
  397.         BoundaryFill4(x,y+1,boundaryColor,red,e);   
  398.         BoundaryFill4(x+1,y,boundaryColor,red,e);   
  399.         BoundaryFill4(x-1,y,boundaryColor,red,e);   
  400.         BoundaryFill4(x,y-1,boundaryColor,red,e);   
  401.     }   
  402.     else{   
  403.         System.out.print("erro!");   
  404.     }   
  405. }/*end of BoundaryFill4()   */    
  406.   
  407.   
  408.   
  409.   
  410. //直线裁剪算法   
  411. void lineClip(float x1,float y1,float x2,float y2,int XL,int XR,int YB,int YT,Graphics e)   
  412.   
  413. {   System.out.println(buffer1);   
  414.     System.out.println(buffer2);   
  415.     float dx,dy;     
  416.      u1=0; u2=1 ;   
  417.      dx =x2-x1;dy=y2-y1;   
  418.    if(ClipT(-dx,x1-XL))   
  419.      if(ClipT(dx,XR-x1))   
  420.       if(ClipT(-dy,y1-YB))   
  421.        if(ClipT(dy,YT-y1))   
  422.        {   
  423.         e.setColor(Color.red);   
  424.         System.out.print(u2);   
  425.         System.out.print(u1);   
  426.         e.drawLine((int)(x1+u1*dx),(int)(y1+u1*dy),(int) (x1+u2*dx),(int)(y1+u2*dy));   
  427.        
  428.        }   
  429.  }   
  430.     
  431. public  boolean ClipT(float p,float q)   
  432.   
  433. {  //System.out.print("dd");   
  434.   float r;     
  435.  if(p<0)   
  436. {   r=q/p;   
  437.     if(r>                                     u2)  return false;   
  438.     else if(r>u1)   
  439.     {   u1=r;   return true;    }   
  440. }   
  441. else if(p>0)   
  442. {   r=p/q;   
  443.     if(r<u1)   
  444.     return false;   
  445.        
  446.     else if(r<u2)   
  447.     {   u2=r;return true;}   
  448.        
  449. }   
  450.   
  451.    else if(q<0return false;   
  452.       
  453.    return true;   
  454. }   
  455.   
  456.   
  457. //dda直线扫描转换算法   
  458. public void ddaline(int x0,int y0,int x1,int y1,Graphics g)   
  459.        
  460. {   
  461. int t,dx,dy;   
  462. float k,x,y;   
  463. if(x0==x1)   
  464. {   
  465. if(y0>y1)   
  466. {   
  467. t=y0;   
  468. y0=y1;   
  469. y1=t;   
  470. }   
  471.   
  472. for(y=y0;y<y1;y=y+1.0f)   
  473. g.drawLine(x0,(int)y,x0,(int) y);   
  474. }   
  475. else  
  476. {   
  477. dx=x1-x0;   
  478. dy=y1-y0;   
  479. k=(float)((float)dy/(float)dx);   
  480. if((-1.0<=k)&&(k<=1.0))   
  481. {   
  482. if(x0>x1)   
  483. {   
  484. t=x0;   
  485. x0=x1;   
  486. x1=t;   
  487. t=y0;   
  488. y0=y1;   
  489. y1=t;   
  490. }   
  491. y=(float)y0;   
  492. for(x=(float)x0;x<(float)x1;x=x+1.0f)   
  493. {   
  494. g.drawLine((int)x,(int)(y+0.5),(int) x,(int) (y+0.5));   
  495. y+=k;   
  496. }   
  497. }   
  498. else  
  499. {   
  500. if(y0>y1)   
  501. {   
  502. t=x0;   
  503. x0=x1;   
  504. x1=t;   
  505. t=y0;   
  506. y0=y1;   
  507. y1=t;   
  508. }   
  509. k=(float)((float)dx/(float)dy);   
  510. x=(float)x0;   
  511. for(y=(float)y0;y<(float)y1;y=y+1.0f)   
  512. {   
  513. g.drawLine((int)(x+0.5),(int)y,(int) (x+0.5),(int) y);   
  514. x+=k;   
  515. }   
  516. }   
  517. }   
  518.   
  519. buffer1=x0;buffer2=y0;buffer3=x1;buffer4=y1;   
  520. }   
  521.   
  522.   
  523. public void fillPoly(int x1,int y1,Graphics e){   
  524.        
  525.     ddaline(x0,x1,x1,y1,e);   
  526.     ddaline((int)buffer3,(int)buffer4,x1,y1,e);   
  527.        
  528.        
  529. }   
  530.   
  531.   
  532. }   

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验1 直线段的扫描转换 实验类型:设计性 实验类别:专业实验 实验目的 1. 通过实验,进一步理解直线段扫描转换的DDA算法、中点bresenham算法及bresenham算法的基本原理; 2. 掌握以上算法生成直线段的基本过程; 3. 通过编程,会在C/C++环境下完成用DDA算法、中点bresenham算法及bresenham算法对任意直线段的扫描转换。 实验设备及实验环境 计算机(每人一台) VC++6.0或其他C/C++语言程序设计环境 实验时:2时 实验内容 用DDA算法中点bresenham算法及bresenham算法实现任意给定两点的直线段的绘制(直线宽度和线型可自定)。 实验步骤: 1、 复习有关算法的基本原理,明确实验目的和要求; 2、 依据算法思想,绘制程序流程图; 3、 设计程序界面,要求操作方便; 4、 用C/C++语言编写源程序并调试、执行; 5、 分析实验结果 6、 对程序设计过程中出现的问题进行分析与总结; 7、 打印源程序或把源程序以文件的形式提交; 8、 按格式要求完成实验报告。 实验报告要求: 1、 各种算法的基本原理; 2、 各算法的流程图 3、 实验结果及分析(比较三种算法的特点,界面插图并注明实验条件) 4、 实验总结(含问题分析及解决方法) 实验2 圆的扫描转换 实验类型:设计性 实验类别:专业实验 实验目的 1、 通过实验,进一步理解和掌握中点bresenham画圆算法的基本原理; 2、 掌握以上算法生成圆和圆弧的基本过程; 3、 掌握在C/C++环境下完成用中点bresenham算法圆或圆弧的绘制方法。 实验设备及实验环境 计算机(每人一台) VC++6.0或其他C/C++语言程序设计环境 实验时:2时 实验内容 用中点(Besenham)算法实现圆或圆弧的绘制。 实验步骤 1. 复习有关圆的生成算法,明确实验目的和要求; 2. 依据算法思想,绘制程序流程图(注意圆弧生成时的输入条件); 3. 设计程序界面,要求操作方便; 4. 用C/C++语言编写源程序并调试、执行; 5. 分析实验结果 6. 对程序设计过程中出现的问题进行分析与总结; 7. 打印源程序或把源程序以文件的形式提交; 8. 按格式要求完成实验报告。 实验报告要求: 1. 分析算法的工作原理; 2. 画出算法的流程图 3. 实验结果及分析(比较圆与圆弧生成算法的不同) 4. 实验总结(含问题分析及解决方法) 实验3 多边形的区域填充 时:2 实验类型:设计性 实验类别:专业实验 实验目的 1. 通过实验,进一步理解和掌握几种常用多边形填充算法的基本原理 2. 掌握多边形区域填充算法的基本过程 3. 掌握在C/C++环境下用多边形填充算法编程实现指定多边形的填充。 实验设备及实验环境 计算机(每人一台) VC++6.0或其他C/C++语言程序设计环境 实验时:2时 实验内容 用种子填充算法和扫描线填充算法等任意两种算法实现指定多边形的区域填充。 实验步骤 1. 复习有关算法,明确实验目的和要求; 2. 依据算法思想,绘制程序流程图(指定填充多边形); 3. 设计程序界面,要求操作方便; 4. 用C/C++语言编写源程序并调试、执行(最好能用动画显示填充过程); 5. 分析实验结果 6. 对程序设计过程中出现的问题进行分析与总结; 7. 打印源程序或把源程序以文件的形式提交; 8. 按格式要求完成实验报告。 实验报告要求: 1. 分析算法的工作原理; 2. 画出算法的流程图 3. 实验结果及分析(比较两种算法的不同) 4. 实验总结(含问题分析及解决方法)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值