服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java小画板(应用程序)

 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://画直线