文本编辑器源文件(java语言)

类别:Java 点击:0 评论:0 推荐:
/版权所有helj
import java.awt.*;
import java.io.*;
import java.util.*;
//W_color;C_str;C_file;C_back;W_find;W_fond

interface I_Method
{
    public void setQuit();
    public void setfont(Font f);
    public void setcolor(Color c);
    public void Find(String find,int dire);
}



class D_color extends Dialog
{
    private Button OK,Cancel;
    private I_Method mp;
    private Rectangle test;

    private Color current= Color.black;
    private Color []colors;
    private Color []colors1;
    private Color []colors2;
    private int strlen= 250;

    public D_color(Frame fr,I_Method mp,int x,int y)
    {
        super(fr,"调色板",true);
        this.mp= mp;
        colors= new Color[strlen];
        colors1= new Color[strlen];
        colors2= new Color[strlen];
        for (int i = 0; i < strlen; i++)
        {
            float h = ((float)i)/((float)strlen);
colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f));
        }
        for (int i = 0; i < strlen; i++)
        {
            float h = ((float)i)/((float)strlen);
colors1[i] = new Color(Color.HSBtoRGB(1.0f,h,1.0f));
        }
        for (int i = 0; i < strlen; i++)
        {
            float h = ((float)i)/((float)strlen);
colors2[i] = new Color(Color.HSBtoRGB(1.0f,1.0f,h));
        }
       
        setLayout(null);
        OK= new Button("确定");
        Cancel= new Button("取消");
        OK.reshape(320,100,80,30);
        add(OK);
        Cancel.reshape(320,150,80,30);
        add(Cancel);        
        test= new Rectangle(0,0,300,250);
        reshape(x/2-210,y/2-140,420,280);
        show();
    }

    public void update(Graphics g)
    {
        int y= 0;
        for (int i = 0; i < strlen; i++)
        {
            g.setColor(colors[i]);
            g.fillRect(0,y,100,1);
            y+=1;
        }
        y= 0;
        for (int i = 0; i < strlen; i++)
        {
            g.setColor(colors1[i]);
            g.fillRect(100,y,100,1);
            y+=1;
        }
        y= 0;
        for (int i = 0; i < strlen; i++)
        {
            g.setColor(colors2[i]);
            g.fillRect(200,y,100,1);
            y+=1;
        }
        g.setColor(current);
        g.fillRect(330,30,60,60);
    }
   
    public void paint(Graphics g)
    {
        update(g);
    }

    public boolean mouseMove(Event evt,int x,int y)
    {
        if(test.inside(x,y))
        {
            Graphics g1= getGraphics();
            //g1.drawImage(back,oldx,oldy);
            //g1.clipRect(x-3,y-3,6,6);
            g1.setColor(Color.white);
            g1.drawArc(x-3,y-3,6,6,60,360);
            g1.drawLine(x-3,y,x+3,y);
            g1.drawLine(x,y-3,x,y+3);
            repaint();
            //oldx= x-3;
            //oldy= y-3;
            return true;
        }
        return false;
    }


    public boolean mouseDown(Event evt,int x,int y)
    {
        if((x>=0)&&(x<=100))
        {
            current= colors[y];
            System.out.println(y);
            getGraphics().setColor(current);
        getGraphics().fillRect(330,30,60,60);
            return true;
        }
        if((x>100)&&(x<=200))
        {
            current= colors1[y];
            getGraphics().setColor(current);
            getGraphics().fillRect(330,30,60,60);
            return true;
        }
        if((x>200)&&(x<=300))
        {
            current= colors2[y];
            getGraphics().setColor(current);
            getGraphics().fillRect(330,30,60,60);
            return true;
        }
        return false;
    }

    public boolean action(Event evt,Object arg)
    {
        if(evt.target instanceof Button)
        {
            if(evt.target== OK)
            {
                mp.setcolor(current);
                dispose();
                return true;
            }
            if(evt.target== Cancel)
            {
                dispose();
                return true;
            }
        }
        return super.action(evt,arg);
    }
}

class W_quick extends Window
{
    MainF fr;
    private Dimension sz;
    private int w,h;
    private int fw,fh;
    private FontMetrics fm;
    private int index=0;

    public W_quick(MainF fr,I_Method qm,int x,int y)
    {
        super(fr);
        this.fr= fr;
        setBackground(Color.lightGray);
       
    }

    public void show(int x,int y)
    {
        repaint();
        reshape(x,y,100,160);
        show();
    }

    private void draw_uplines(Graphics g,int x,int y,int w1,int h1)
{
    g.setColor(Color.white);
        g.drawLine(x,y,x+w1-1,x);
        g.drawLine(x,y,x,y+h1-1);

        g.setColor(Color.black);
        g.drawLine(x+w1-1,y-1,x+w1-1,y+h1-1);
        g.drawLine(x+w1-2,y+1,x+w1-2,y+h1-2);
        g.drawLine(x+1,y+h-2,x+w1-2,y+h1-2);
    //    g.drawLine(x,y+h1-1,x+w1-1,y+h1-1);
    //    g.setColor(Color.black);
    //    g.drawLine(x,y+h1-1,x+w1-1,y+h1-1);
       
}
   
    public void paint(Graphics g)
    {
        sz= size();
        w= sz.width;
        h= sz.height;   
    //    g.setFont(Ffont);
        fm= g.getFontMetrics();
        fh=fm.getHeight();
        fw= fm.charWidth('C');
        g.setColor(Color.gray);
        g.drawRect(10,(index-1)*fh+6,w-20,fh);
    //    g.setColor(Color.lightGray);
    //    g.drawRect(0,0,w,h);
        draw_uplines(g,1,1,w-1,h-1);
        g.setColor(Color.black);
        g.drawString("Cut",13,fh);
        g.drawString("Copy",13,2*fh);
        g.drawString("Paste",13,3*fh);
        g.drawString("Delete",13,4*fh);
    }

    public boolean mouseMove(Event evt,int x,int y)
    {
        for(int i=1;i<=4;i++)
            if((y>(i-1)*fh)&&(y<(i*fh)))
        {
            index= i;
            repaint();
        }
        return super.mouseMove(evt,x,y);
    }

    public boolean mouseDown(Event evt,int x,int y)
    {
        if(y>0&&y<fh)
        {
            fr.current_t.dels();
            fr.eitem.enable(false);
            fr.eitem1.enable(false);
            fr.eitem3.enable(false);
            fr.eitem2.enable(true);
            hide();
            return true;
        }
        if(y>fh&&(y<(2*fh)))
        {
            fr.eitem.enable(false);
            fr.eitem1.enable(false);
            fr.eitem3.enable(false);
            fr.eitem2.enable(true);
            hide();
            return true;
        }
        if((y>(2*fh))&&(y<(3*fh)))
        {
            fr.current_t.adds();
            fr.eitem2.enable(false);
            fr.eitem.enable(false);
            fr.eitem1.enable(false);
            fr.eitem3.enable(false);
            hide();
            return true;
        }
        if((y>(3*fh))&&(y<(4*fh)))
        {
            fr.current_t.dels();
            fr.eitem3.enable(false);
            fr.eitem.enable(false);
            fr.eitem1.enable(false);
            fr.eitem2.enable(false);
            hide();
            return true;
        }
        return super.mouseDown(evt,x,y);
    }
}

class ptest extends Panel
{
    String test= "ABCDabcd";
    D_font ff;

    public ptest(D_font ff)
    {
        this.ff= ff;
        repaint();
    }

    private void draw_downlines(Graphics g,int x,int y,int w,int h)
{
        g.setColor(Color.white);
        g.drawLine(x+1,y+h-1,x+w-1,y+h-1);
        g.drawLine(x+w-1,y+h-1,x+w-1,y+1);
       
        g.setColor(Color.black);
        g.drawLine(x,y,x,y+h);
        g.drawLine(x+1,y+1,x+1,y+h-1);
        g.drawLine(x,y,x+w,y);
        g.drawLine(x+1,y+1,x+w-1,y+1);
}

    public void rep()
    {
        repaint();
    }
   
    public void paint(Graphics g)
    {
        Dimension sz= size();
        Point pt= location();
g.setColor(Color.darkGray);
g.drawRect(0,0,sz.width-2,sz.height-2);

g.setColor(Color.white);
g.drawLine(1,1,1,sz.height-3);
g.drawLine(1,1,sz.width-3,1);

g.drawLine(0,sz.height-1,sz.width-1,sz.height-1);
g.drawLine(sz.width-1,0,sz.width-1,sz.height-1);

        draw_downlines(g,15,10,sz.width-30,sz.height-20);
        g.clipRect(16,11,sz.width-31,sz.height-21);
        g.setColor(Color.black);
        g.setFont(ff.bf);
        g.drawString(test,20,sz.height-21);
    }
}



class D_font extends Dialog
{
    public Font bf;
    private Button OK,Cancel;
    private Label l1,l2,l3;
    private String items1[]={"Helvetica","Courier","TimesRoman","Dialog"};
    private String items2[]={"PLAIN","BOLD","PLAIN+BOLD","ITALIC","BOLD+ITALIC"};
    private int[] style= new int[5];
    private String items3[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
    private TextField f1,f2,f3;
    private List ll1,ll2,ll3;
    private ptest test;
    private I_Method im;

    public void init()
    {
        style[0]= Font.PLAIN;
        style[1]= Font.BOLD;
        style[2]= Font.PLAIN+Font.BOLD;
        style[3]= Font.ITALIC;
        style[4]= Font.BOLD+Font.ITALIC;
    }

    public D_font(Frame fr,I_Method qm,int x,int y)
    {
        super(fr,"字体",true);
        init();
        setFont(new Font("TimesRoman",Font.PLAIN,14));
        im= qm;
       
        GridBagLayout gridBag= new GridBagLayout();
        GridBagConstraints c= new GridBagConstraints();
        setLayout(gridBag);
        c.insets.left= 4;
        c.insets.right= 4;
        c.insets.bottom= 1;
        c.insets.top= 1;
       
        l1= new Label("字体:",Label.LEFT);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=0;
        c.gridy=0;
        c.gridwidth=2;
        c.gridheight =1;
        gridBag.setConstraints(l1,c);
        add(l1);

        l2= new Label("字体样式:",Label.LEFT);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=2;
        c.gridy=0;
        c.gridwidth=1;
        c.gridheight =1;
        gridBag.setConstraints(l2,c);
        add(l2);

        l3= new Label("大小:",Label.LEFT);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=3;
        c.gridy=0;
        c.gridwidth=1;
        c.gridheight =1;
        gridBag.setConstraints(l3,c);
        add(l3);

        OK= new Button("确定");
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=4;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight =1;
        gridBag.setConstraints(OK,c);
        add(OK);

        f1= new TextField();
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=0;
        c.gridy=1;
        c.gridwidth=2;
        c.gridheight =1;
        gridBag.setConstraints(f1,c);
        add(f1);

        f2= new TextField();
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=2;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight =1;
        gridBag.setConstraints(f2,c);
        add(f2);

        f3= new TextField();
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=3;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight =1;
        gridBag.setConstraints(f3,c);
        add(f3);

        Cancel= new Button("取消");
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=4;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight =1;
        gridBag.setConstraints(Cancel,c);
        add(Cancel);

        ll1= new List();
        for(int i=0;i<items1.length;i++)
            ll1.addItem(items1[i]);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 1.0;
        c.weighty= 1.0;
        c.gridx=0;
        c.gridy=2;
        c.gridwidth=2;
        c.gridheight =7;
        gridBag.setConstraints(ll1,c);
        add(ll1);

        ll2= new List();
        for(int i1=0;i1<items2.length;i1++)
            ll2.addItem(items2[i1]);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=2;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight =7;
        gridBag.setConstraints(ll2,c);
        add(ll2);

        ll3= new List();
        for(int i2=0;i2<items3.length;i2++)
            ll3.addItem(items3[i2]);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 0.0;
        c.weighty= 0.0;
        c.gridx=3;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight =7;
        gridBag.setConstraints(ll3,c);
        add(ll3);

        test= new ptest(this);
        c.gridwidth= GridBagConstraints.REMAINDER;
        c.fill= GridBagConstraints.BOTH;
        c.weightx= 1.0;
        c.weighty= 1.0;
        c.gridx=0;
        c.gridy=9;
        c.gridwidth=5;
        c.gridheight =4;
        gridBag.setConstraints(test,c);
        add(test);

        reshape(x/2-210,y/2-140,420,280);
        show();
    }

    public boolean action(Event evt,Object arg)
    {
        if(evt.target instanceof Button)
        {
            if(evt.target== OK)
            {
                if(ll1.getSelectedIndex()==-1)
                    ll1.select(0);
                if(ll3.getSelectedIndex()==-1)
                    ll3.select(0);
                if(ll2.getSelectedIndex()==-1)
                    ll2.select(0);
                int size= Integer.parseInt(items3[ll3.getSelectedIndex()]);
                bf=new Font(items1[ll1.getSelectedIndex()],style[ll2.getSelectedIndex()],size);
                im.setfont(bf);
                dispose();
                return true;
            }
            if(evt.target== Cancel)
            {
                dispose();
                return true;
            }
        }

        if(evt.target instanceof List)
        {
            if(ll1.getSelectedIndex()==-1)
                    ll1.select(0);
            if(ll3.getSelectedIndex()==-1)
                    ll3.select(0);
            if(ll2.getSelectedIndex()==-1)
                    ll2.select(0);
            f1.setText(ll1.getSelectedItem());
            f2.setText(ll2.getSelectedItem());
            f3.setText(ll3.getSelectedItem());
           
            int size= Integer.parseInt(items3[ll3.getSelectedIndex()]);
            bf=new Font(items1[ll1.getSelectedIndex()],style[ll2.getSelectedIndex()],size);
            test.rep();
            return true;
        }
        return super.action(evt,arg);
    }
}


class W_quit extends Dialog
{
    Button OK,Cancel;
    I_Method im;

    public W_quit(Frame fr,I_Method qm)
    {
        super(fr,"Java文本编译器");
        //setBackground(Color.lightGray);
        setFont(new Font("TimesRoman",Font.BOLD,16));
        im= qm;
        OK= new Button("确定");
        Cancel= new Button("取消");

        Panel pp= new Panel();
        pp.add(OK);
        pp.add(Cancel);
        add("South",pp);
        add("Center",new Label("Ready to Quit",1));
        reshape(100,100,200,100);
    }

    public boolean action(Event evt,Object arg)
    {
        if(evt.target== OK)
        {
            im.setQuit();
            dispose();
            return true;
        }else
            if(evt.target== Cancel)
        {
            dispose();
            return true;
        }else
            return super.action(evt,arg);
    }
}

class screen
{
    public int[] length;
    public int left,right,up,down;
    public int offsetx=0,offsety=0;
    public int w=0,h=0;
    public int maxline;

    public screen()
    {
        length= new int[300];
        for(int i=0;i<300;i++)
        {
            length[i]= 0;
        }
        left= 0;
        up= 0;
    }

    public void setmax(int x,int y)
    {
        down= y;
        right= x;
    }
}

class cusor
{
    public int cusorx,cusory;//当前光标的坐标
    public int index;//当前光标对应的偏移
    public int linex,liney;//行列号首地址为1,1linex
    public int oldx,oldy;
    public int w,h;

    public cusor(int y)
    {
        index= 0;
        cusorx= 3;
        cusory= y;
        linex= liney= 0;
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        g.drawLine(oldx,oldy,oldx,oldy-h+2);        
        g.setColor(Color.black);
        g.drawLine(cusorx,cusory,cusorx,cusory-h+2);
        oldx= cusorx;
        oldy= cusory;
    }
}

class select
{
    public char[] sels;
    public int count;// 选中字符的个数
    public int index;//当前光标对应的偏移
    public int linex,liney;

    public select()
    {
        linex= liney= count= 0;
        sels= new char[1000];
    }
}

//横坐标起始为3
class P_area extends Canvas
{
    public screen sc;
    public cusor cus;
    public select sel;
    private MainF fr;
    public String filename,filedir;
    private Font Ffont= null;
    private Color Bcolor= Color.black;
    private FontMetrics fm;
    private Dimension sz;
   
    private int fh,fw;
    private int paint_x,paint_y;//用于写字符   
    public int totle= 0;//字符总数
    private int lcount=0;

    public boolean border= true;//光标是否在边界
//    private boolean scroll= false;
    public boolean select= false;
    public boolean selold= false;//选中后重画屏幕
    public boolean bfind= false;

    private String findtext= null;   
    private int[] findindex;
    private int findcount=0;
    private char[] text;
   
    public P_area(MainF fr,Font cuf,Color cuc,String fn,String fd)
    {
        this.fr= fr;
        setBackground(Color.white);
        sz= size();        
        Ffont= cuf;
        setFont(cuf);
        cus= new cusor(sz.height);
        sc= new screen();
        sel= new select();
        Bcolor= cuc;
        filename= fn;
        filedir= fd;
        text= new char[5000];
        findindex= new int[100];
        repaint();
    }

    private void draw_downlines(Graphics g,int x,int y,int w,int h)
{
        g.setColor(Color.white);
        g.drawLine(x+1,y+h-1,x+w-1,y+h-1);
        g.drawLine(x+w-1,y+h-1,x+w-1,y+1);
       
        g.setColor(Color.black);
        g.drawLine(x,y,x,y+h);
        g.drawLine(x+1,y+1,x+1,y+h-1);
        g.drawLine(x,y,x+w,y);
        g.drawLine(x+1,y+1,x+w-1,y+1);
}

    //打开已存在的文件
    public void setname(String fn,String fd)
    {
        filename= fn;
        filedir= fd;
    }

    public void open(String line)
    {
        //findtext= line;
        line.getChars(0,line.length()-1,text,totle);
        totle+= line.length();
    //    text[totle]= '\n';
    //    totle++;
    //    sc.length[lcount]= line.length()+1;
    //    lcount++;
        repaint();
    }

    public String getText()
    {
        char[] ret= new char[totle];
        for(int i=0;i<totle;i++)
            ret[i]= text[i];
        return (new String(ret));
    }

    public void movecusor(int x,int y)
    {
        cus.linex= (int)((x-3)/cus.w);
        cus.liney= (int)((y-3*fh)/cus.h)+1;
        int i=0;
        cus.index=0;
        for(;i< cus.liney+sc.offsety-1;i++)
            cus.index+= sc.length[i];
        cus.index+=cus.linex-1+sc.offsetx;
        cus.cusorx= (cus.linex-1)*cus.w+3;
        cus.cusory= cus.liney*cus.h;
        cus.paint(getGraphics());
    }
   
    public void keycusor(int x,int y)
    {
        cus.linex+= x;
        cus.liney+= y;
        cus.cusorx+= x*cus.w;
        cus.cusory+= y*cus.h;
        int i=0;
        cus.index=0;
        for(;i<cus.liney+sc.offsety-1;i++)
            cus.index+= sc.length[i];
        cus.index+=cus.linex-1+sc.offsetx;
        cus.paint(getGraphics());
    }

    public void add(char ch)
    {
        if(totle==0)
            totle= 1;
        for(int i= totle;i>=cus.index;i--)
            text[i]= text[i-1];
        text[cus.index]= ch;
        totle++;
        sc.length[cus.liney]+= 1;
       
        repaint();
        if(ch=='\n')
        {
            cus.linex= 1;
            cus.liney+= 1;
            cus.cusorx= 3;
            cus.cusory= cus.liney*cus.h;
            cus.index++;
        }else
            keycusor(1,0);
    }

    public void adds()
    {
        totle+= sel.count;
        int i;
        for(i= totle-1;i>(cus.index+sel.count);i--)
            text[i]= text[i-sel.count];
        for(int j= sel.count;j>0;j--,i--)
            text[i]= sel.sels[j-1];
        sel.count= 0;
        repaint();
    }
   
    public void del(int de)
    {
        if(de<0)
        {
            for(int i= cus.index-1;i<totle;i++)
                text[i]= text[i+1];
            keycusor(-1,0);
        }
        if(de>0)
            for(int i= cus.index;i<totle;i++)
            text[i]= text[i+1];
        totle--;
        repaint();
    }

    public void dels()
    {
        if(sel.count<0)
        {
            int end= -sel.count;
            for(int i= sel.index;i<totle;i++)
                text[i]= text[i+end+2];
        }
        if(sel.count>0)
        {
            int end= sel.index+1;
            for(int i= end;i<totle;i++)
                text[i-sel.count]= text[i];
        }
        totle-= sel.count;
        //sel.count= 0;
        repaint();
    }

    public void selrep()
    {
        draw(getGraphics());
    }

    public void drag(int x,int y)
    {
        sel.linex= (int)((x-3)/cus.w);
        sel.liney= (int)((y-3*fh)/cus.h)+1;
        int i=0;
        sel.index=0;
        for(;i< sel.liney+sc.offsety-1;i++)
            sel.index+= sc.length[i];
        sel.index+=sel.linex-1+sc.offsetx;
        sel.count= sel.index- cus.index+1;
        if(sel.index!= cus.index)
        {
            select= true;
            selold= true;
            repaint();
        }
    }

    private void drawselect(Graphics g)
    {
        int ji= 0;
        paint_x= 3-(int)(sc.offsetx*cus.w);
        paint_y= fh-(int)(sc.offsety*fh);
        for(int i=0;i< totle;i++)
        {
            if(text[i]=='\n')
            {
                paint_x = 3-(int)(sc.offsetx*cus.w);
                paint_y+=cus.h;
                i++;
            }
            fw= fm.charWidth(text[i]);
            if(sel.index>cus.index)
            {
                if((i< totle)&&(i<=sel.index)&&(i>=cus.index))
                {
                    sel.sels[ji]= text[i];
                    ji++;
                    g.setColor(Color.blue);
                    g.fillRect(paint_x,paint_y-fh,fw+1,fh);
                    g.setColor(Color.white);
                    g.drawChars(text,i,1,paint_x,paint_y);
                }
            }else
                if((i< totle)&&(i>=sel.index)&&(i<=cus.index))
                {
                sel.sels[ji]= text[i];
                ji++;
                g.setColor(Color.blue);
                    g.fillRect(paint_x,paint_y-fh,fw+1,fh);
                    g.setColor(Color.white);
                    g.drawChars(text,i,1,paint_x,paint_y);
                }

            paint_x+= fw;
        }
    }

    public void home()
    {
        cus.linex= 1;
        int i=0;
        cus.index=0;
        for(;i< cus.liney+sc.offsety-1;i++)
            cus.index+= sc.length[i];
        cus.index+=cus.linex-1+sc.offsetx;
        cus.cusorx= 3;
        cus.paint(getGraphics());
    }

    public void end()
    {
        cus.linex= sc.length[cus.liney-1]-1;
        int i=0;
        cus.index=0;
        for(;i< cus.liney+sc.offsety-1;i++)
            cus.index+= sc.length[i];
        cus.index+=cus.linex-1+sc.offsetx;
        cus.cusorx= (cus.linex-1)*cus.w+3;
        cus.paint(getGraphics());
    }

    public void pagedown()
    {
        if(sc.maxline>2*sc.h)
        {
            sc.offsety+= sc.h;
            fr.vert.setValue(sc.offsety);
            draw(getGraphics());
            fr.vert.setValue(sc.offsety);
        }
    }

    public void pageup()
    {
        if((sc.offsety-sc.h)>=0)
        {
            sc.offsety-=sc.h;
            draw(getGraphics());
            fr.vert.setValue(sc.offsety);
        }
    }

    public void scrollpaint(int x1,int x2)
    {
        sc.offsetx= x1;
        sc.offsety= x2;
    //    getGraphics().translate(-x1,-x2);
        draw(getGraphics());
    }
    public void setFont(Font cuf)
    {
        Ffont= cuf;
        //paint= true;
        repaint();
    }

    public void setColor(Color col)
    {
        Bcolor= col;
    //    paint= true;
        repaint();
    }

    public void rep()
    {
        repaint();
    }
   
    private void draw(Graphics g)
    {
        findtext= new String(text,0,totle);
        int findi= findcount;
        int aa= 0;//用于记录starti
        int c1= 0;//记录行的数目
        int c2= 0;//记录每行的字节数
        int c3= 0; //最大列数
        g.setFont(Ffont);
        g.setColor(Color.white);
        g.fillRect(3,2,sz.width,sz.height);
        g.setColor(Bcolor);
       
        paint_x= 3-(int)(sc.offsetx*cus.w);
        paint_y= fh-(int)(sc.offsety*fh);
        for(int i=0;i< totle;i++)
        {
            if(text[i]=='\n')
            {
                paint_x = 3-(int)(sc.offsetx*cus.w);
                paint_y+=cus.h;
                //i++;
                sc.length[c1]= c2+1;
                c1++;
                if(c2>c3)
                    c3= c2;
                c2= 0;
            }else            
            if(i< totle)
            {
                fw= fm.charWidth(text[i]);
                g.setColor(Bcolor);
                g.drawChars(text,i,1,paint_x,paint_y);
                if(i== findindex[findi]&&bfind)
                {
                    if(findi>0)
                        findi--;
                    g.setColor(Color.blue);        
                    g.fillRect(paint_x,paint_y-fh+2,fw+1,fh);
                    g.setColor(Color.white);
                    g.drawChars(text,i,1,paint_x,paint_y);
               
                }
               
                paint_x+= fw;
                c2++;
            }
        }
        c1+= 1;
        sc.maxline= c1;
        cus.paint(g);
        if(c3> sc.w)
            fr.horz.setValues(0,sc.w,0,c3-sc.w);
        if(c1> sc.h)
            fr.vert.setValues(0,sc.h,0,c1-sc.h);
    }

    public void update(Graphics g)
    {
        sz= size();
        g.setColor(Bcolor);
        g.setFont(Ffont);
        fm= g.getFontMetrics();
        cus.h= fh= fm.getHeight();
        cus.w= fw= fm.charWidth('A');
        sc.h= (int)((sz.height-3)/fh)+1;
        sc.w= (int)((sz.width-3)/fw)+1;

        draw_downlines(g,0,0,sz.width,sz.height);
        //g.clipRect(3,2,sz.width,sz.height);
        if(select)
        {
            drawselect(g);
            select= false;
        }else
            draw(g);
    }

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

    public boolean mouseEnter(Event evt,int x,int y)
    {
        fr.setCursor(2);
        return true;
    }

    public boolean mouseExit(Event evt,int x,int y)
    {
        fr.setCursor(0);
        return true;
    }

    public void find(String find,int dire)
    {
        bfind= true;
        findcount =0;
        String sss= findtext;
        for(int i=0;i<100;i++)
            findindex[i]= 0;
        int j=0;
        while(true)
        {
            if(sss.indexOf(find)==-1)
                break;
            findindex[j]= sss.lastIndexOf(find);
            sss= sss.substring(0,findindex[j]);
            j++;
            findcount++;
        }
        repaint();
        selold= true;
    }

}


public class MainF extends Frame implements I_Method
{
private W_quick Lshow;
   
    private FileDialog openDialog;
private FileDialog saveDialog;

    public Scrollbar horz,vert;
    private MenuBar menuBar;
    private Panel File_panel,Back_p;
    private Panel Back_panels[];
   
    private int screenw,screenh;
    private Color Fcolor= Color.black;
    private Font Ffont= new Font("TimesRoman",Font.PLAIN,14);
    private int Fwidth,Fheight;
    private int[] bookmarks= new int[20];
    //**************
    public P_area current_t;
    private int i1;
    private int iii=0;
    private boolean insert= false;
    boolean begin= false;//用于标志是否开始选中
    boolean end= false;
    private int sel1= 0;
    private int sel2= 0;

    MenuItem eitem;
    MenuItem eitem1;
    MenuItem eitem2;
    public MenuItem eitem3;

MainF(String title)
{
super(title);

Font font= new Font("TimesRoman",Font.BOLD,20);
setFont(font);
        setBackground(Color.lightGray);
        horz= new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,0);
        vert= new Scrollbar(Scrollbar.VERTICAL,0,0,0,0);

    //    show();
        addMenu();
        init();
        Lshow= new W_quick(this,this,0,0);
       
        openDialog= new FileDialog(this,"Open File...",FileDialog.LOAD);
saveDialog= new FileDialog(this,"Save File...",FileDialog.SAVE);
    }

    private void init()
    {
        setLayout(new BorderLayout());
        File_panel= new Panel();
        File_panel.setBackground(Color.lightGray);
        Panel pp= new Panel();
        pp.setLayout(new BorderLayout(2,2));
        current_t= new P_area(this,Ffont,Fcolor,"aa","aa");
        pp.add("Center",current_t);
        pp.add("South",horz);
        pp.add("East",vert);

        add("South",File_panel);
        add("Center",pp);

        Dimension screen_size= Toolkit.getDefaultToolkit().getScreenSize();
        screenw=screen_size.width;
        screenh= screen_size.height-16;
        reshape(16,16,screen_size.width-32,screen_size.height-32);
        show();
    }

    private void addMenu()
    {
        menuBar= new MenuBar();
setMenuBar(menuBar);

Menu menu= new Menu("File");
Menu menu1= new Menu("Edit");
        Menu menu2= new Menu("Option");
menuBar.add(menu);
menuBar.add(menu1);
        menuBar.add(menu2);

MenuItem fitem= new MenuItem("New");
menu.add(fitem);
MenuItem fitem1= new MenuItem("Open");
menu.add(fitem1);
MenuItem fitem2= new MenuItem("Close");
menu.add(fitem2);
        menu.addSeparator();
        MenuItem fitem3= new MenuItem("Save");
        menu.add(fitem3);
        MenuItem fitem4= new MenuItem("Save As");
        menu.add(fitem4);
        menu.addSeparator();
        MenuItem fitem5= new MenuItem("Quit");
        menu.add(fitem5);

       
   
        menu1.add(new MenuItem("Undo"));
        menu1.add(new MenuItem("Redo"));
        menu1.addSeparator();
        eitem= new MenuItem("Cut");
menu1.add(eitem);
        eitem.enable(false);
eitem1= new MenuItem("Copy");
eitem1.enable(false);
        menu1.add(eitem1);
eitem2= new MenuItem("Paste");
menu1.add(eitem2);
        eitem2.enable(false);
        menu1.addSeparator();
        eitem3= new MenuItem("Delete");
        menu1.add(eitem3);
        eitem3.enable(false);
        MenuItem eitem6= new MenuItem("Bookmark");
        menu1.add(eitem6);
        menu1.addSeparator();
        MenuItem eitem4= new MenuItem("Find");
        menu1.add(eitem4);        
        MenuItem eitem5= new MenuItem("Repace");
        menu1.add(eitem5);

        MenuItem oitem= new MenuItem("Color");
menu2.add(oitem);
MenuItem oitem1= new MenuItem("Font");
menu2.add(oitem1);

    }

public void setQuit()
    {
        this.dispose();
        System.exit(0);
    }

    public void setfont(Font f)
    {
        Ffont= f;
        current_t.setFont(f);
        repaint();
    }

    public void setcolor(Color c)
    {
        Fcolor= c;
        current_t.setColor(c);
        repaint();
    }
   
    public void paint(Graphics g)
{
        if(Ffont!= null)
            g.setFont(Ffont);

}

    public void read(String fn,String fd)
{
current_t.setname(fn,fd);
        String line= null;
        String input= null;
File s= new File(fd,fn);
        FileInputStream in= null;
        DataInputStream datain= null;

        try
{
in= new FileInputStream(s);
datain = new DataInputStream(in);
}catch(Exception e)
{
System.out.println("Unable to open file");
return;
}

        try{
while((line=datain.readLine())!= null)
{
                line+= "\n";
                if(input== null)
                    input= line;
                else
                    input+= line;
        }
        }catch(IOException e){
            System.out.println("Error in reading file");
        }
        current_t.open(input);

try{
in.close();
}catch(IOException e){
System.out.println("e");
}
}   

public void write(String fn,String fd)
{
FileOutputStream out= null;
        File s= new File(fd,fn);

try{
            out= new FileOutputStream(s);
        }catch(Exception e)
        {
            System.out.println("Unable to open file");
            return;
        }
PrintStream psOut=new PrintStream(out);
psOut.print(current_t.getText());///
try{
out.close();
}catch(IOException e){
System.out.println("e");
}
}

public boolean keyDown(Event evt,int key)
{
        System.out.println(key);
        switch(key)
{
        case 1004://up
            current_t.keycusor(0,-1);
            return true;
        case 1005://down
            current_t.keycusor(0,1);
            return true;
        case 1006:
            current_t.keycusor(-1,0);
            return true;
        case 1007:
            current_t.keycusor(1,0);
            return true;    
        case 8:
            current_t.del(-1);
            return true;
        case 127:
            current_t.del(1);
            return true;
        case 1001://end
            current_t.end();
            return true;

        case 1000://home
            current_t.home();
            return true;
        case 1002:
            current_t.pageup();
            return true;
        case 1003:
            current_t.pagedown();
            return true;
        }
    //    if(!current_t.border)
    //         current_t.addchar((char)evt.key);
    //    else
            current_t.add((char)evt.key);
        return true;
    }

    public boolean mouseDown(Event evt,int x,int y)
    {
        //****单击右键*******************不完整
        eitem.enable(false);
        eitem1.enable(false);
        eitem3.enable(false);
        current_t.bfind= false;
       
        if((evt.modifiers&Event.META_MASK)>0)
        {
            Lshow.show(evt.x,evt.y);
            return false;
        }else
        {
            Lshow.hide();
            begin= true;
            current_t.movecusor(evt.x,evt.y);
            if(current_t.selold)
            {
                current_t.selold= false;
                current_t.selrep();
            }
        }
        if(end)
        {
        //    current_t.rep();
        //    current_t.paint =true;
            end= false;
        }
        return false;
    }

    public boolean mouseDrag(Event evt,int x,int y)
    {
        eitem.enable(true);
        eitem1.enable(true);
        eitem3.enable(true);
        current_t.drag(evt.x,evt.y);
        //}
        return true;
    }
   
    public boolean handleEvent(Event evt)
    {
        if(evt.id== Event.WINDOW_DESTROY)
{
            W_quit aa= new W_quit(this,this);
            aa.show();
            return true;
        }
        switch(evt.id)
{
case 601:
            case 602:
            case 603:
            case 604:
            case 605:
                //current_t.offsety= (vert.getValue());
            //     current_t.offsetx= (horz.getValue());
            //     current_t.paint= true;
            //     current_t.rep();
                current_t.scrollpaint(horz.getValue(),vert.getValue());
               
                vert.setValue(current_t.sc.offsety);
                horz.setValue(current_t.sc.offsetx);
                return true;
        }


        return super.handleEvent(evt);
    }

    public boolean action(Event evt,Object arg)
{
String filename;
        if(evt.target instanceof MenuItem)
        {
            if(evt.arg.equals("New"))
            {
                return true;
            }
            if(evt.arg.equals("Open"))
            {
                openDialog.show();
                String fn= openDialog.getFile();
                String filedir= openDialog.getDirectory();
                read(fn,filedir);
                return true;
            }
            if(evt.arg.equals("Save"))
            {
                write(current_t.filename,current_t.filedir);
                return true;
            }
            if(evt.arg.equals("Save As"))
            {
                saveDialog.show();
                filename= saveDialog.getFile();
                String filedir= openDialog.getDirectory();
                write(filename,filedir);
                return true;
            }
            if(evt.arg.equals("Quit"))
            {
                System.exit(0);
            }
            if(evt.arg.equals("Cut"))
            {
                current_t.dels();
                eitem.enable(false);
                eitem1.enable(false);
                eitem3.enable(false);
                eitem2.enable(true);
                return true;
            }
            if(evt.arg.equals("Paste"))
            {
                current_t.adds();
                eitem2.enable(false);
                eitem.enable(false);
                eitem1.enable(false);
                eitem3.enable(false);
                return true;
            }
            if(evt.arg.equals("Copy"))
            {
                eitem.enable(false);
                eitem1.enable(false);
                eitem3.enable(false);
                eitem2.enable(true);
                return true;
            }
            if(evt.arg.equals("Delete"))
            {
                current_t.dels();
                eitem3.enable(false);
                eitem.enable(false);
                eitem1.enable(false);
                eitem2.enable(false);
                return true;
            }
            if(evt.arg.equals("Close"))
            {
                current_t.totle= 0;
                current_t.rep();
                return true;
            }
            if(evt.arg.equals("Find"))
            {
                new outPanel(this);
                return true;
            }
if(evt.arg.equals("Font"))
{
new D_font(this,this,screenw,screenh);
return true;
}
if(evt.arg.equals("Color"))
{
new D_color(this,this,screenw,screenh);
return true;
}
}
/*else if(evt.target instanceof Button)
{
} */
return false;
}

    public void Find(String find,int dire)
    {
        current_t.find(find,dire);
        System.out.println(find);
    }

    public static void main(String[] args)
    {
        MainF ff= new MainF("文本编译器");
    }
}

class outPanel extends Dialog
{
    MainF fr;
    Label lbname; //name label
    TextField tfname; //entry field
    outlinePanel psex; //two outlined panels]
    CheckboxGroup cbsex;
    Checkbox up,down,boxc; //2 radios
    Button OK, Cancel; //two pushbuttons
    GridBagConstraints gbc;
    GridBagLayout gbl;
    int dire= 1;

    public outPanel(MainF fr)
    {
        super(fr,"查找",true);
        this.fr= fr;
        setFont(new Font("Helvetica", Font.PLAIN, 12));
        setBackground(Color.lightGray);
        gbl =new GridBagLayout();
        gbc = new GridBagConstraints();
        setLayout(gbl);
        gbc.ipadx = 5; //make buttons wider
        gbc.ipady = 3; //make buttons higher
        gbc.insets.left =6;
        gbc.insets.right =6;
        gbc.insets.bottom =4;
        gbc.insets.top =4;
        gbc.weightx =1;
        gbc.weighty =1;
       
        lbname = new Label("查找目标:"); //label
        add_component(lbname, 0,0,1,1);
        tfname = new TextField(20); //text entry field
        add_component(tfname, 1,0,3,1);
        //panel with two buttons for sex
        boxc= new Checkbox("区分大小写");
        add_component(boxc, 0,2,1,1);
        psex = new outlinePanel(); //first panel
        add_component(psex, 1,1,3,2); //insert in layout
        //put two radio buttons in panel
        up = psex.addCheckbox("向上", false);
        down = psex.addCheckbox("向下", true);
        psex.resize(148,53);
        //add two command buttons to bottom
        OK = new Button("找下一个"); //buttons at bottom
        Cancel = new Button("取消");
        add_component(OK,4,0,1,1);
        add_component(Cancel,4,2,1,1);
        reshape(100,100, 380,146);
        show();
}
//--------------------------------------
private void add_component(Component c, int x, int y, int w, int h)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight =h;
add(c);
gbl.setConstraints(c, gbc);
}
//--------------------------------------
public boolean action(Event evt, Object arg)
{
    if(evt.target== OK)
    {
        String ss= tfname.getText();
        if(boxc.getState())
        {
            ss.toLowerCase();
            fr.Find(ss,dire);
        }else
            fr.Find(ss,dire);
    }
    if (evt.target == Cancel)
    {
        dispose();
        return true;
    }
    if(evt.target== up)
    {
        dire=-1;        
    }
    if(evt.target== down)
    {
        dire= 1;
    }
    if(evt.target== boxc)
    {
    }
    return super.action(evt, arg);
}
}
//*///=======================================
class outlinePanel extends Panel
{
CheckboxGroup cbg;
int yposn;
Vector cblist;
//--------------------------------------
public outlinePanel()
{
super();
cbg = new CheckboxGroup(); //for radio buttons
setLayout(null);
yposn = 3;
cblist = new Vector();
}
//--------------------------------------
public void reshape(int x, int y, int w, int h)
{
super.reshape(x,y,w,h);
for (int i=0; i< cblist.size(); i++)
{
Component c = (Component)cblist.elementAt(i);
c.reshape(c.location().x, c.location().y, w-6, c.size().height);
}
}
//--------------------------------------
public Component add(Component c)
{
super.add(c);
return c;
}
//--------------------------------------
public Checkbox addCheckbox(String s, boolean checked)
{
Checkbox cb = new Checkbox(s, cbg, checked);
add(cb);
cblist.addElement(cb);
cb.reshape(3, yposn, 50, 12);
yposn += 15;
return cb;
}
//--------------------------------------
public void paint(Graphics g)
{
Dimension sz = size();
Point pt = location();
g.setColor(Color.darkGray); // graw rectangle
g.drawRect(0, 0, sz.width-2, sz.height-2);
//outline gray rectangle with 2 white inner lines
g.setColor(Color.white);
g.drawLine(1, 1, sz.width-3, 1);
g.drawLine(1, 1, 1, sz.height-3);
//and two white outer lines
g.drawLine(0,sz.height-1,sz.width-1,sz.height-1);
g.drawLine(sz.width-1, 0, sz.width-1, sz.height-1);
}
//--------------------------------------
}

本文地址:http://com.8s8s.com/it/it18783.htm