2006年02月24日

图形学实验,在此仅供个人娱乐之用,可以转用。

简单的绘图程序,在Eclipse3.1上编译通过,可以绘制直线,圆,矩形和分形树


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyGraphics extends JFrame{
 private thePanel myPanel ;
 private JPanel panel;
 private JLabel lable;
 String[] theStatus={"直线","圆_由中心","圆_由边界点","矩形_由中心","矩形_由顶点","分形树"};
 private CheckboxGroup checkg;
 private Checkbox check1,check2,check3,check4,check5,check6;
 public MyGraphics(){
  super("图形学试验_计算机0306班_孟庆伟");
  myPanel = new thePanel();
  panel= new JPanel();
  lable= new JLabel();
  panel.setLayout(new FlowLayout());
  check1 = new Checkbox("直线");
  check2 = new Checkbox("圆_由中心");
  check3 = new Checkbox("圆_由边界点");
  check4 = new Checkbox("矩形_由中心");
  check5 = new Checkbox("矩形_由顶点");
  check6 = new Checkbox("分形树");
  checkg = new CheckboxGroup();
  check1.setCheckboxGroup(checkg);
  check2.setCheckboxGroup(checkg);
  check3.setCheckboxGroup(checkg);
  check4.setCheckboxGroup(checkg);
  check5.setCheckboxGroup(checkg);
  check6.setCheckboxGroup(checkg);
  checkg.setSelectedCheckbox(check1);
  
  panel.add(check1);
  panel.add(check2);
  panel.add(check3);
  panel.add(check4);
  panel.add(check5);
  panel.add(check6);
  Container container=getContentPane();
  container.setLayout(new BorderLayout());
  container.add(myPanel,BorderLayout.CENTER);
  container.add(panel,BorderLayout.NORTH);
  container.add(lable,BorderLayout.SOUTH);

   myPanel.addMouseMotionListener(new MouseMotionListener()
    {
     public void mouseDragged(MouseEvent event)
     {
      lable.setText(theStatus[myPanel.getStar()-1]+"   Form "+myPanel.getX1()+","+myPanel.getY1()+" to "+ event.getX()+","+event.getY());
     }
     public void mouseMoved(MouseEvent event)
     {
      lable.setText(theStatus[myPanel.getStar()-1]+"  Position:"+ event.getX()+","+event.getY());
     }
    });
   check1.addItemListener(new ItemListener()
    {
     public void itemStateChanged(ItemEvent e)
     {
      myPanel.setStar(1);
     }
    });
   check2.addItemListener(new ItemListener()
    {
     public void itemStateChanged(ItemEvent e)
     {
      myPanel.setStar(2);
     }
    });
   check3.addItemListener(new ItemListener()
    {
     public void itemStateChanged(ItemEvent e)
     {
      myPanel.setStar(3);
     }
    });
   check4.addItemListener(new ItemListener()
    {
     public void itemStateChanged(ItemEvent e)
     {
      myPanel.setStar(4);
     }
    });
   check5.addItemListener(new ItemListener()
    {
     public void itemStateChanged(ItemEvent e)
     {
      myPanel.setStar(5);
     }
    });
   check6.addItemListener(new ItemListener()
    {
     public void itemStateChanged(ItemEvent e)
     {
      myPanel.setStar(6);
     }
    });
  try
  {
   UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  }
  catch(Exception e){}

  setSize(700,500);
  setResizable(true);
  setVisible(true);
 }
 public static void main (String[] args){
  MyGraphics theGraphics = new MyGraphics();
  theGraphics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}


class thePanel extends JPanel{
 private int x1,x2,y1,y2;
 public int star;
 final double PI=Math.PI/180;
 public thePanel(){
  this.addMouseListener(new MouseAdapter()
      {
       public void mousePressed(MouseEvent event)
       {
        x1=event.getX();
        y1=event.getY();
       }
       public void mouseReleased(MouseEvent event)
       {
        x2=event.getX();
        y2=event.getY();
        repaint();
       }
      }
      );
 
  this.addMouseMotionListener(new MouseMotionAdapter()
      {
       public void mouseDragged(MouseEvent event)
       {
        x2=event.getX();
        y2=event.getY();
        repaint();
       }
      });
  star=1;
 }
 public void setStar(int i){
  star = i;
 }
 public void paintComponent(Graphics g){
  super.paintComponent(g);
  switch(star){
   case 1 :
    g.setColor(Color.BLACK);
    this.drawLine(x1,y1,x2,y2,g);
    break;
   case 2 :
    g.setColor(Color.red);
    this.drawCircle(x1,y1,x2,y2,g);
    break;
   case 3 :
    g.setColor(Color.red);
    this.drawCircle_1(x1,y1,x2,y2,g);
    break;
   case 4 :
    g.setColor(Color.BLUE);
    this.drawCuboid_1(x1,y1,x2,y2,g);
    break;
   case 5 :
    g.setColor(Color.BLUE);
    this.drawCuboid(x1,y1,x2,y2,g);
    break;
   case 6 :
    g.setColor(new Color(67,156,53));
    this.drawLeaf(300.0,500.0,100.0,270.0, g);
    break;
  }
 }
 public int getX1(){
  return x1;
 }
 public int getY1(){
  return y1;
 }
 public int getStar(){
  return star;
 }
 private void drawPoint(int x,int y,Graphics g){
  g.drawLine(x,y,x,y);
 }
 private void drawLine(int x1,int y1,int x2,int y2,Graphics g)
 {
  int dx,dy,d,UpIncre,DownIncre,x,y;
  float k;
  if(x1>x2)
  {
   x=x2;x2=x1;x1=x;
   y=y2;y2=y1;y1=y;
  }
  else
  {
   x=x1;y=y1;
  }
  dx=x2-x1;dy=y2-y1;
  if(dx==0)
  {
   for(y=Math.min(y1,y2);y<=Math.max(y1,y2);y++)
    this.drawPoint(x,y,g);
   return;
  }
  k=(float)dy/(float)dx;
  if(k>=0)
  {
   if(k<=1)
   {
    d=dx-2*dy;
    UpIncre=2*dx-2*dy;DownIncre=-2*dy;
    while(x<=x2)
    {
      this.drawPoint(x,y,g);
     x++;
     if(d<0)
     {
      y++;
      d+=UpIncre;
     }
     else d+=DownIncre;
    }
   }
   else
   {
    d=2*dx-dy;
    DownIncre=2*dx;UpIncre=2*dx-2*dy;
    while(y<=y2)
    {
      this.drawPoint(x,y,g);
     y++;
     if(d>0)
     {
      x++;
      d+=UpIncre;
     }
     else d+=DownIncre;
    }    
   }
  }
  else
  {
   if(k>=-1)
   {
    d=-dx-2*dy;
    UpIncre=-2*dx-2*dy;DownIncre=-2*dy;
    while(x<=x2)
    {
      this.drawPoint(x,y,g);
     x++;
     if(d>0)
     {
      y–;
      d+=UpIncre;
     }
     else d+=DownIncre;
    }
   }
   else
   {
    d=-2*dx-dy;
    UpIncre=-2*dx-2*dy;DownIncre=-2*dx;
    while(y>=y2)
    {
      this.drawPoint(x,y,g);
     y–;
     if(d<0)
     {
      x++;
      d+=UpIncre;
     }
     else d+=DownIncre;
    }    
   }
   
  }
 }
 private void drawCircle(int x1,int y1,int x2,int y2,Graphics g)
 {
  int x,y,d,r;
  r=(int)Math.sqrt((double)(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
  x=0;y=r;d=1-r;
  while(x<=y)
  {
   this.drawPoint(x1+x,y1+y,g);
   this.drawPoint(x1+x,y1-y,g);
   this.drawPoint(x1-x,y1+y,g);
   this.drawPoint(x1-x,y1-y,g);
   this.drawPoint(x1+y,y1+x,g);
   this.drawPoint(x1+y,y1-x,g);
   this.drawPoint(x1-y,y1+x,g);
   this.drawPoint(x1-y,y1-x,g);
   if(d<0)
    d+=2*x+3;
   else
   {
    d+=2*(x-y)+5;
    y–;
   }
   x++;
  }
 }
 public void drawLeaf(double x,double y,double L,double a,Graphics g){
  double x1,x2,x1L,x2L,x1R,x2R;
  double y1,y2,y1L,y2L,y1R,y2R;
  
  float B = 50;
  float C = 9;
  float s1 = 2;
  float s2 = 3;
  float s3 = 1.2f;
   
  if(L>s1){
   x2 = x+L*Math.cos (a*PI);
   y2 = y+L*Math.sin (a*PI);
   x2R = x2+L/s2*Math.cos((a+B)*PI);
   y2R = y2+L/s2*Math.sin((a+B)*PI);
   x2L = x2+L/s2*Math.cos((a-B)*PI);
   y2L = y2+L/s2*Math.sin((a-B)*PI);
   
   x1 = x+L*Math.cos (a*PI);
   y1 = y+L*Math.sin (a*PI);
   x1R = x1+L/s2*Math.cos((a-B)*PI);
   y1R = y1+L/s2*Math.sin((a-B)*PI);
   x1L = x1+L/s2*Math.cos((a+B)*PI);
   y1L = y1+L/s2*Math.sin((a+B)*PI);
   
   g.drawLine((int) x, (int) y,(int) x2,(int) y2);
   g.drawLine((int) x2,(int) y2,(int) x2R,(int) y2R);
   g.drawLine((int) x2,(int) y2,(int) x2L,(int) y2L);
   g.drawLine((int) x1,(int) y1,(int) x1L,(int) y1L);
   g.drawLine((int) x1,(int) y1,(int) x1R,(int) y1R);
   
   drawLeaf(x2,y2,L/s3,a+C,g);
   drawLeaf(x2R,y2R,L/s2,a+B,g);
   drawLeaf(x2L,y2L,L/s2,a-B,g);
   drawLeaf(x1L,y1L,L/s2,a-B,g);
   drawLeaf(x1R,y1R,L/s2,a+B,g);
  }
 }
 public void drawCuboid(int x1,int y1,int x2,int y2,Graphics g){
  this.drawLine(x1,y1,x2,y1,g);
  this.drawLine(x1,y1,x1,y2,g);
  this.drawLine(x2,y1,x2,y2,g);
  this.drawLine(x1,y2,x2,y2,g);
 }
 public void drawCircle_1(int x1,int y1,int x2,int y2,Graphics g){
  this.drawCircle((x1+x2)/2,(y1+y2)/2,x2,y2,g);
 }
 public void drawCuboid_1(int x1,int y1,int x2,int y2,Graphics g){
  this.drawCuboid(2*x1-x2,2*y1-y2,x2,y2,g);
 }
}

以下是用java语言实现的简单的计算器的源代码

在Eclipes3.1上编译通过,仅供个人练习之用,可以随意转用。


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CalculatorSwing extends JFrame{
 public Double numone,numtwo,memoryspace;
 public StringBuffer text = new StringBuffer(27);
 public boolean haveDot=false;
 JTextField textfield;
 JLabel label;
 final String[] values = {"7","8","9","4","5","6","1","2","3","0","+/-","."};
 int i,status;
 String s;
 public CalculatorSwing(){
  text.append(" ");
  JFrame frame = new JFrame("计算器_计算机0306班_孟庆伟");
  frame.setBounds(546,296,338,274); //设定窗口在屏幕的位置;
  frame.setResizable(false);
  frame.setLayout(new FlowLayout());    
  
  JPanel panelA= new JPanel();
  panelA.setLayout(new BorderLayout());
  JPanel panelB= new JPanel();
  panelB.setLayout(new BorderLayout());

  JPanel panel1=new JPanel();
  JPanel panel2=new JPanel();
  panel2.setLayout(new BorderLayout());
  JPanel panel3=new JPanel();
  panel3.setLayout(new GridLayout(5,1,3,3));
  panel3.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
  
  JPanel panel4=new JPanel();
  panel4.setLayout(new GridLayout(4,3,3,3));
  JPanel panel5=new JPanel();
  panel5.setLayout(new GridLayout(4,2,3,3));
  panel5.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
  
  textfield=new JTextField();
  textfield.setHorizontalAlignment(JTextField.RIGHT);
  textfield.setText("0.");
  textfield.setColumns(28);
  textfield.setEnabled(false);
  panel1.add(textfield);
  
  label = new JLabel("     ");
  panel3.add(label);
  label.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
  
  String[] operations= {"Backspace","CE","      C      "};  /*以下内容初始化相关按钮内容*/
  JButton[] operation = new JButton[3];
  for (int i=0;i<3;i++){
   operation[i]=new JButton(operations[i]); 
   operation[i].setForeground(new Color(255, 0, 0));
  } 
  panel2.add(operation[0],BorderLayout.WEST); 
  operation[0].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                                if(text.length()>1){
                                 text.deleteCharAt(text.length()-1);
                                  if(text.length()==1){
                                 text=new StringBuffer(" ");
                                 textfield.setText("0.");
                                }
                                else textfield.setText(text.toString());
                               }
                             }
                            });
  panel2.add(operation[1],BorderLayout.CENTER);
  operation[1].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                                numtwo=new Double(0.0);
                                text=new StringBuffer(27);
                                text.append(" ");                               
                                textfield.setText("0.");
                             }
                            });
  panel2.add(operation[2],BorderLayout.EAST);
  operation[2].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){                            
                                numone=new Double(0.0);
                                numtwo=new Double(0.0);
                                text=new StringBuffer(27);
                                text.append(" ");                            
                                textfield.setText("0.");
                             }
                            });
  panel2.setBorder(BorderFactory.createEmptyBorder(4,0,9,0));
  JButton[] value= new JButton [12];     
  for ( i=0;i<12;i++){
   value [i]=new JButton(values[i]);
   value[i].setForeground(new Color(0, 0, 255));
   panel4.add(value[i]);
   switch(i){
    case  0 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("7");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  1 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("8");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  2 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("9");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  3 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("4");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  4 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("5");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  5 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("6");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  6 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("1");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  7 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("2");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  8 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("3");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  9 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27){
                                text.append("0");
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
    case  11 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                              if(text.length()<=27&&haveDot==false){
                                text.append(".");
                                haveDot=true;
                                textfield.setText(new String(text));
                              }
                             }
                            });break;
        case 10 :value[i].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               if(text.charAt(0)==’ ‘) text.setCharAt(0,’-');
                               else text.setCharAt(0,’ ‘);
                               textfield.setText(new String(text));
                             }
                            });break;
      }                                           
  }    

  String[] calcus = {"/","sqrt","*","%","-","1/x","+","="};
  JButton[] calcu = new JButton[8];
  for (int i=0;i<8;i++){
   calcu[i] = new JButton(calcus[i]);
   if(i==1||i==3||i==5){
    calcu[i].setForeground(new Color(0, 0, 255));
   }
   else calcu[i].setForeground(new Color(255, 0, 0));
   panel5.add(calcu[i]);
  }
   calcu[0].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=0;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[1].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=1;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[2].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=2;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[3].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=3;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[4].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=4;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[5].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=5;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[6].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=6;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(27);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[7].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               status=7;
                               try{
                                numone=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}
                               text=new StringBuffer(30);text.append(" ");
                               haveDot=false;
                             }
                            });
  calcu[7].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               if(status!=5&&status!=3){try{
                                numtwo=new Double(Double.valueOf(textfield.getText()).doubleValue());
                               }catch(Exception ex){ return;}}
                               text=new StringBuffer(27);text.append(" ");
                               switch(status){
                                case 0: if(numtwo.doubleValue()!=0){
                                     numtwo=new Double(numone.doubleValue()/numtwo.doubleValue());textfield.setText(numtwo.toString());
                                    }else textfield.setText("除数不能为0");numone=numtwo; break;
                                case 1: if(numone.doubleValue()>=0){
                                     numtwo=new Double(Math.sqrt(numone.doubleValue()));
                                     textfield.setText(numtwo.toString());
                                     numone=numtwo;
                                    }
                                    else textfield.setText("负数不能开方");break;
                                case 2: numtwo=new Double(numone.doubleValue()*numtwo.doubleValue());textfield.setText(numtwo.toString());numone=numtwo;break;
                                case 3: numtwo=new Double(numtwo.doubleValue()/100.0);textfield.setText(numtwo.toString());numone=numtwo;break;
                                case 4: numtwo=new Double(numone.doubleValue()-numtwo.doubleValue());textfield.setText(numtwo.toString());numone=numtwo;break;
                                case 5: if(numone.doubleValue()!=0){numtwo=new Double(1.0/numone.doubleValue());textfield.setText(numtwo.toString());}
                                        else textfield.setText("分母不能为0");numone=numtwo;break;
                                case 6: numtwo=new Double(numone.doubleValue()+numtwo.doubleValue());textfield.setText(numtwo.toString());numone=numtwo;break;       
                               }
                               haveDot=false;
                             }
                            });
  String[] memorys = {"MC","MR","MS","M+"};
  JButton[] memory = new JButton[4];
  for (int i=0;i<4;i++){
   memory[i]=new JButton(memorys[i]);
   memory[i].setForeground(new Color(255, 0, 0));
   panel3.add(memory[i]);
  }
  memory[0].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               try{
                                memoryspace=new Double(0.0);
                               }catch(Exception ex){ return;}
                               label.setText(" ");
                               haveDot=false;
                             }
                            });
  memory[1].addActionListener(new ActionListener(){
                             public void actionPerformed(ActionEvent e){
                               try{                              
                               text=new StringBuffer(27);text.append(" ");text.append(memoryspace.toString());
                               }catch(Exception ex){ return;}