欢迎光临
我们一直在努力

JAVA小画板(应用程序)-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

 04年接触了一段时间的java,期间写了比较多小程序段,写得最完整的就是这个java小画板程序,涉及到鼠标事件、画图函数、文件流、文件的打开与保存等内容。

//powered by compower
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;

class point implements serializable
{
 int x,y;
 color col;
 int tool;
 int boarder;

 point(int x, int y, color col, int tool, int boarder)
 {
  this.x = x;
  this.y = y;
  this.col = col;
  this.tool = tool;
  this.boarder = boarder;
  }
}

class paintboard extends frame implements actionlistener,mousemotionlistener,mouselistener,itemlistener
{
 int x = -1, y = -1;
 int con = 1;//画笔大小
 int econ = 5;//橡皮大小

 int toolflag = 0;//toolflag:工具标记
      //toolflag工具对应表:
      //(0–画笔);(1–橡皮);(2–清除);
      //(3–直线);(4–圆);(5–矩形);

 color c = new color(0,0,0); //画笔颜色
 basicstroke size = new basicstroke(con,basicstroke.cap_butt,basicstroke.join_bevel);//画笔粗细
 point cutflag = new point(-1, -1, c, 6, con);//截断标志

 vector paintinfo = null;//点信息向量组
 int n = 1;

 fileinputstream picin = null;
 fileoutputstream picout = null;
 
 objectinputstream vin = null;
 objectoutputstream vout = null;

// *工具面板–画笔,直线,圆,矩形,多边形,橡皮,清除*/
 panel toolpanel;
 button eraser, drline,drcircle,drrect;
 button clear ,pen;
 choice colchoice,sizechoice,eraserchoice;
 button colchooser;
 label 颜色,大小b,大小e;
 //保存功能
 button openpic,savepic;
 filedialog openpicture,savepicture;
 

  paintboard(string s)
 { 
  super(s);
  addmousemotionlistener(this);
  addmouselistener(this);

  paintinfo = new vector();

/*各工具按钮及选择项*/
  //颜色选择
  colchoice = new choice();
  colchoice.add(“black”);
  colchoice.add(“red”);
  colchoice.add(“blue”);
  colchoice.add(“green”);
  colchoice.additemlistener(this);
  //画笔大小选择
  sizechoice = new choice();
  sizechoice.add(“1”);
  sizechoice.add(“3”);  
  sizechoice.add(“5”);
  sizechoice.add(“7”);
  sizechoice.add(“9”);
  sizechoice.additemlistener(this);
  //橡皮大小选择
  eraserchoice = new choice();
  eraserchoice.add(“5”);
  eraserchoice.add(“9”);
  eraserchoice.add(“13”);
  eraserchoice.add(“17”);
  eraserchoice.additemlistener(this);
  ////////////////////////////////////////////////////
  toolpanel = new panel();

  clear = new button(“清除”);
  eraser = new button(“橡皮”);
  pen = new button(“画笔”);
  drline = new button(“画直线”);
  drcircle = new button(“画圆形”);
  drrect = new button(“画矩形”);

  openpic = new button(“打开图画”);
  savepic = new button(“保存图画”);
  
  colchooser = new button(“显示调色板”);

  //各组件事件监听
  clear.addactionlistener(this);
  eraser.addactionlistener(this);
  pen.addactionlistener(this);
  drline.addactionlistener(this);
  drcircle.addactionlistener(this);
  drrect.addactionlistener(this);
  openpic.addactionlistener(this);
  savepic.addactionlistener(this);
  colchooser.addactionlistener(this);

  
  颜色 = new label(“画笔颜色”,label.center);
  大小b = new label(“画笔大小”,label.center);
  大小e = new label(“橡皮大小”,label.center);
  //面板添加组件
  toolpanel.add(openpic);
  toolpanel.add(savepic);
  
  toolpanel.add(pen);
  toolpanel.add(drline);
  toolpanel.add(drcircle);
  toolpanel.add(drrect);

  toolpanel.add(颜色); toolpanel.add(colchoice);
  toolpanel.add(大小b); toolpanel.add(sizechoice);
  toolpanel.add(colchooser);

  toolpanel.add(eraser);
  toolpanel.add(大小e); toolpanel.add(eraserchoice);

  toolpanel.add(clear);
  //工具面板到applet面板
  add(toolpanel,borderlayout.north);

  setbounds(60,60,900,600); setvisible(true);
  validate();
  //dialog for save and load

  openpicture = new filedialog(this,”打开图画”,filedialog.load);
  openpicture.setvisible(false);
  savepicture = new filedialog(this,”保存图画”,filedialog.save);
  savepicture.setvisible(false);

  openpicture.addwindowlistener(new windowadapter()
  {
   public void windowclosing(windowevent e)
   { openpicture.setvisible(false); }
  });

  savepicture.addwindowlistener(new windowadapter()
  {
   public void windowclosing(windowevent e)
   { savepicture.setvisible(false); }
  });

  addwindowlistener(new windowadapter()
  {
   public void windowclosing(windowevent e)
   { system.exit(0);}
  });
 
  
 }

 public void paint(graphics g)
 {
  graphics2d g2d = (graphics2d)g;

  point p1,p2;

  n = paintinfo.size();
  
  if(toolflag==2)
    g.clearrect(0,0,getsize().width,getsize().height);//清除

  for(int i=0; i<n-1; i++)
  {
   p1 = (point)paintinfo.elementat(i);
   p2 = (point)paintinfo.elementat(i+1);
   size = new basicstroke(p1.boarder,basicstroke.cap_butt,basicstroke.join_bevel);

      g2d.setcolor(p1.col);
   g2d.setstroke(size);

  if(p1.tool==p2.tool)
   {
   switch(p1.tool)
   {
    case 0://画笔

      line2d line1 = new line2d.double(p1.x, p1.y, p2.x, p2.y);
      g2d.draw(line1);      
     break;

    case 1://橡皮
      g.clearrect(p1.x, p1.y, p1.boarder, p1.boarder);
     break;

    case 3://画直线
      line2d line2 = new line2d.double(p1.x, p1.y, p2.x, p2.y);
      g2d.draw(line2);
     break;

    case 4://画圆
       ellipse2d ellipse = new ellipse2d.double(p1.x, p1.y, math.abs(p2.x-p1.x) , math.abs(p2.y-p1.y));
       g2d.draw(ellipse);
     break;

    case 5://画矩形
       rectangle2d rect = new rectangle2d.double(p1.x, p1.y, math.abs(p2.x-p1.x) , math.abs(p2.y-p1.y));
       g2d.draw(rect);
     break;

    case 6://截断,跳过
      i=i+1;
     break;

    default :
   }//end switch
   }//end if
  }//end for
 }

 public void itemstatechanged(itemevent e)
 {
  if(e.getsource()==colchoice)//预选颜色
  {
   string name = colchoice.getselecteditem();
   
   if(name==”black”)
   { c = new color(0,0,0); }
   else if(name==”red”)
   { c = new color(255,0,0); }
   else if(name==”green”)
   { c = new color(0,255,0); }
   else if(name==”blue”)
   { c = new color(0,0,255); }
  }
  else if(e.getsource()==sizechoice)//画笔大小
  {
   string selected = sizechoice.getselecteditem();
   
   if(selected==”1″)
   { 
    con = 1;
    size = new basicstroke(con,basicstroke.cap_butt,basicstroke.join_bevel);

   }
   else if(selected==”3″)
   { 
    con = 3;
    size = new basicstroke(con,basicstroke.cap_butt,basicstroke.join_bevel);

   }
   else if(selected==”5″)
   { con = 5;
    size = new basicstroke(con,basicstroke.cap_butt,basicstroke.join_bevel);

   }
   else if(selected==”7″)
   { con = 7;
    size = new basicstroke(con,basicstroke.cap_butt,basicstroke.join_bevel);

   }
   else if(selected==”9″)
   { con = 9;
    size = new basicstroke(con,basicstroke.cap_butt,basicstroke.join_bevel);

   }
  }
  else if(e.getsource()==eraserchoice)//橡皮大小
  {
   string esize = eraserchoice.getselecteditem();

   if(esize==”5″)
   {  econ = 5*2; }
   else if(esize==”9″)
   {  econ = 9*2; }
   else if(esize==”13″)
   {  econ = 13*2; }
   else if(esize==”17″)
   {  econ = 17*3; }

  }

 }

 public void mousedragged(mouseevent e)
 {
  point p1 ;
  switch(toolflag){
   case 0://画笔
     x = (int)e.getx();
     y = (int)e.gety();
     p1 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p1);
     repaint();
     break;

   case 1://橡皮
     x = (int)e.getx();
     y = (int)e.gety();
     p1 = new point(x, y, null, toolflag, econ);
     paintinfo.addelement(p1);
     repaint();
     break;

   default :
  }
 }

 public void mousemoved(mouseevent e) {}

 public void update(graphics g)
 {
  paint(g);
 }

 public void mousepressed(mouseevent e)
 { 
  point p2;
  switch(toolflag){
   case 3://直线
     x = (int)e.getx();
     y = (int)e.gety();
     p2 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p2);
     break;

   case 4: //圆
     x = (int)e.getx();
     y = (int)e.gety();
     p2 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p2);
     break;

   case 5: //矩形
     x = (int)e.getx();
     y = (int)e.gety();
     p2 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p2);
     break;

   default :
  } 
 }

 public void mousereleased(mouseevent e)
 { 
  point p3; 
  switch(toolflag){
   case 0: //画笔
     paintinfo.addelement(cutflag);
     break;

   case 1: //eraser
     paintinfo.addelement(cutflag);
     break;

   case 3: //直线
     x = (int)e.getx();
     y = (int)e.gety();
     p3 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p3);
     paintinfo.addelement(cutflag);
     repaint();
     break;

   case 4: //圆
     x = (int)e.getx();
     y = (int)e.gety();
     p3 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p3);
     paintinfo.addelement(cutflag);
     repaint();
     break;

   case 5: //矩形
     x = (int)e.getx();
     y = (int)e.gety();
     p3 = new point(x, y, c, toolflag, con);
     paintinfo.addelement(p3);
     paintinfo.addelement(cutflag);
     repaint();
     break;

   default:
  }
 }

 public void mouseentered(mouseevent e){}

 public void mouseexited(mouseevent e){}

 public void mouseclicked(mouseevent e){}

 public void actionperformed(actionevent e)
 {
    
  if(e.getsource()==pen)//画笔
  { toolflag = 0; }

  if(e.getsource()==eraser)//橡皮
  { toolflag = 1; }
  
  if(e.getsource()==clear)//清除
  {
   toolflag = 2;
   paintinfo.removeallelements();
   repaint();
  }
  
  if(e.getsource()==drline)//画线
  { toolflag = 3; }

  if(e.getsource()==drcircle)//画圆
  { toolflag = 4; }

  if(e.getsource()==drrect)//画矩形
  { toolflag = 5; }

  if(e.getsource()==colchooser)//调色板
  {   
   color newcolor = jcolorchooser.showdialog(this,”调色板”,c);
   c = newcolor;   
  }

  if(e.getsource()==openpic)//打开图画
  {
   
   openpicture.setvisible(true);   
   
   if(openpicture.getfile()!=null)
   { 
    int tempflag;
    tempflag = toolflag;
    toolflag = 2 ;
    repaint();

    try{ 
      paintinfo.removeallelements();
      file filein = new file(openpicture.getdirectory(),openpicture.getfile());
      picin = new fileinputstream(filein);
      vin = new objectinputstream(picin);
      paintinfo = (vector)vin.readobject();
      vin.close();
      repaint();
      toolflag = tempflag;

     }

    catch(classnotfoundexception ioe2)
    {
     repaint();
     toolflag = tempflag;
     system.out.println(“can not read object”);
    }
    catch(ioexception ioe)
    {
     repaint();
     toolflag = tempflag;
     system.out.println(“can not read file”);
    }
   }

  }

  if(e.getsource()==savepic)//保存图画
  {
   savepicture.setvisible(true);
   try{
     file fileout = new file(savepicture.getdirectory(),savepicture.getfile());
     picout = new fileoutputstream(fileout);
     vout = new objectoutputstream(picout);
     vout.writeobject(paintinfo);
     vout.close();
    }
   catch(ioexception ioe)
    {
      system.out.println(“can not write object”);
    }

   
  }
 }
}//end paintboard

public class pb
{
 public static void main(string args[])
 { new paintboard(“画图程序”); }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » JAVA小画板(应用程序)-JSP教程,Java技巧及代码
分享到: 更多 (0)