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

📄 o.java

📁 这是一个小型的画图程序,有普通的话图功能,方便易用
💻 JAVA
字号:
import java.applet.*;import java.lang.*;import java.awt.*;import java.util.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.JColorChooser;/*class Point                                                         //定义点类{     int x,y,con,cho;Color col;                                       //X,Y轴坐标,点的大小,自动或手动,点的颜色   Point(int x,int y,Color col,int con,int cho)   {       this.x=x;this.y=y;this.col=col;this.con=con;this.cho=cho;    //构造函数初始化   }   int dis(Point p)                                                 //求两点之间的距离   {        double a,b,c,d;       a=p.x;b=this.x;c=p.y;d=this.y;       a=Math.pow(a-b,2);c=Math.pow(c-d,2);       d=(int)Math.sqrt(a+c);       return (int)d;   }}*/public class o extends Applet implements MouseMotionListener,ActionListener,ItemListener,MouseListener{ Panel pan1=new Panel();Panel pan2=new Panel();                     //设置两个面板 Choice choice1,choice2,choice3,choice4; Label label1,label2,label3,label4; int x=-1,y=-1,rubberanouce=0,clearanouce=0; Vector v1=null;Vector v2=null;                                     //设置两个向量,一个存用来存储点对象,一个用来判断  Color c=new Color(255,0,0,0);                                       //设置一个颜色对象,初始化为红色  int cho=0;int con=1;int xin=0;int r=0;int temp=0;int t=0;  int n1=1;int n2=1;  Button clear,rubber,tiao;  public void init()                                                //初始化程序  {    rubber=new Button("橡皮擦");    clear=new Button("清除器");    tiao=new Button("调色板");    Label label1=new Label("选择:");    Label label2=new Label("颜色:");    Label label3=new Label("粗细:");    Label label4=new Label("图形:");    choice1=new Choice(); choice2=new Choice(); choice3=new Choice();choice4=new Choice();    v1=new Vector();v2=new Vector();    choice1.add("蓝色");choice1.add("绿色");choice1.add("红色");choice1.add("橙色");       choice2.add("细线");choice2.add("中线");choice2.add("粗线");     choice3.add("直线");choice3.add("圆形");choice3.add("矩形");    choice4.add("手动");choice4.add("自动");   // setLayout(new BorderLayout());    pan1.add(label1);pan1.add(choice4);pan1.add(label2);pan1.add(choice1);pan1.add(label3);    pan1.add(choice2);pan1.add(label4);pan1.add(choice3);add(pan1,BorderLayout.CENTER);    pan2.add(rubber);pan2.add(tiao);pan2.add(clear);add(pan2,BorderLayout.SOUTH);     choice1.addItemListener(this);    choice2.addItemListener(this);    choice3.addItemListener(this);    choice4.addItemListener(this);    rubber.addActionListener(this);    clear.addActionListener(this);    tiao.addActionListener(this);    addMouseMotionListener(this);     addMouseListener(this); } //*********************************************************************************************************************// public void itemStateChanged(ItemEvent e) {      int index1=choice1.getSelectedIndex();                //下拉框1设置颜色      if(index1==1){c= new Color(0,255,0,0);}      else if(index1==2){c=new Color(255,0,0,0);}      else if(index1==0){c=new Color(0,0,255,0);}      else if(index1==4){c=new Color(0,0,0,255);}      int index2=choice2.getSelectedIndex();               //下拉框2设置点的大小(线的粗细)      if(index2==2){con=5;}      else if(index2==1) {con=3;}      else {con=1;}      int index3=choice3.getSelectedIndex();               //下拉框3设置画的图形(直线,圆形,矩形)      if(index3==0){xin=0;cho=1;}      else if(index3==1){xin=1;cho=1;}      else {xin=2;cho=1;}      int index4=choice4.getSelectedIndex();               //下拉框4设置手工或是自动      if(index4==1)cho=1;      if(index4==0)cho=0;      rubberanouce=0;clearanouce=0; } //*********************************************************************************************************************// public void paint(Graphics g)                            //画图主程序,重画的调用 {         Graphics2D g2d=(Graphics2D)g;                       //转化化2D图     if(clearanouce==1)g2d.clearRect(0,0,getSize().width,getSize().height);clearanouce=0;     if(x!=-1&&y!=-1)     {        n1=v1.size();                                    //取向量的个数       for(int i=0;i<n1-1;i++)       {        Point p1=(Point)v1.elementAt(i);         Point p2=(Point)v1.elementAt(i+1);        BasicStroke bs=new BasicStroke(p1.con);          //设置线的粗细        g.setColor(p1.col);                             //设置线的颜色        int flag=1;n2=v2.size();        temp=p1.cho;                                     //用来判断怎样画图        for(int j=0;j<n2;j++)                            //判断是否鼠标放点        {Point p=(Point)v2.elementAt(j);if((p1.x==p.x)&&(p1.y==p.y))flag=0;}        if(temp==0&&flag==1)                             //手动画图          {              Line2D line=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);              g2d.draw(line);          }         if(flag==1||temp>=1)                            //自动画图        {         if(temp>=1)i++;                                 //自动画图时每画一个跳过一个点         g2d.setStroke(bs);                              //取点的大小作为线的粗细         r=p1.dis(p2);                                   //当画圆的时候求出p1与p2之间的距离作为半径         if(temp==1)                                     //画直线         {Line2D line=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);         g2d.draw(line);}         if(temp==2)                                     //画圆形         {         Ellipse2D ellipse=new Ellipse2D.Double(p1.x-r,p1.y-r,r*2,r*2);         g2d.draw(ellipse);         }         if(temp==3)                                     //画矩形         {           Rectangle2D rect=new Rectangle2D.Double(0,0,0,0);//画矩形的四种可能           if((p1.x<p2.x)&&(p1.y<p2.y))           rect=new Rectangle2D.Double(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);           if((p1.x>p2.x)&&(p1.y>p2.y))           rect=new Rectangle2D.Double(p2.x,p2.y,p1.x-p2.x,p1.y-p2.y);           if((p1.x<p2.x)&&(p1.y>p2.y))           rect=new Rectangle2D.Double(p1.x,p2.y,p2.x-p1.x,p1.y-p2.y);           if((p1.x>p2.x)&&(p1.y<p2.y))           rect=new Rectangle2D.Double(p2.x,p1.y,p1.x-p2.x,p2.y-p1.y);           g2d.draw(rect);           }     }     }     }}//*************************************************************************************************************************//public void actionPerformed(ActionEvent e){     if(e.getSource()==rubber)                             //响应橡皮擦事件     {         rubberanouce=1;clearanouce=0 ;c= new Color(255,255,255);con=10;     }     if(e.getSource()==clear)                             //响应清除器事件     {         clearanouce=1;         rubberanouce=0;v1.removeAllElements();v2.removeAllElements();         repaint();     }     if(e.getSource()==tiao)                              //响应调色板事件     {      c=JColorChooser.showDialog(this,"",c);     }}public void mouseDragged(MouseEvent e){     if(cho==0)                                          //控制自动画图还是手动画图的参数     {     x=(int)e.getX();y=(int)e.getY();                    //取当前鼠标前的X轴和Y轴坐标用来存入点中              Point p=new Point(x,y,c,con,cho);                   //X坐标,Y坐标,颜色,点的大小,用来作自动画图还是手动画图     v1.addElement(p);                                   //把点对象存入向量中     repaint();     }}public void update(Graphics g){      paint(g);} public void mouseClicked(MouseEvent e){}public void mousePressed(MouseEvent e){    if(cho==1)                                            //用来判断自动画图    {          t=xin+1;                                        //临时变量t为控制画图的参数          x=(int)e.getX();y=(int)e.getY();           Point p=new Point(x,y,c,con,t);          v1.addElement(p);          repaint();    }} public void mouseReleased(MouseEvent e){      x=(int)e.getX();y=(int)e.getY();                  //用来断开连续的向量      Point p=new Point(x,y,c,con,cho);      v2.addElement(p);      repaint();}  public void mouseMoved(MouseEvent e){}   public void mouseEntered(MouseEvent e){}  public void mouseExited(MouseEvent e){}} 

⌨️ 快捷键说明

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