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

📄 machine.java

📁 Ideal Flow Machine For Data Plot
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;

public class machine extends Frame {

    Panel flowtype,numbers,controls,baseline,buttonbar;    //GUI declarations
    Drawing drawing;
    Legend legend;
    Choice options;
    TextField strength,angle;
    Label label2,label3,xD,yD;
    Button newflow,mapbut,databut;
    mapping map;

    dataPort out;

    machine() {
        super("The Ideal Flow Machine 4.0");

        setBackground(Color.white);

        GridBagLayout gridbag=new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        numbers=new Panel();
        numbers.setLayout(gridbag);
        c.weightx=1.0;

        options= new Choice();
        options.setFont(new Font("Dialog",Font.PLAIN,10));
        options.addItem("Free stream");
        options.addItem("Source");
        options.addItem("Vortex");
        options.addItem("Doublet");
        options.addItem("Source panel");
        options.addItem("Vortex panel");
        options.addItem("Circle");
        options.addItem("Circle with K.c.");
        options.addItem("Draw Streamline");

        gridbag.setConstraints(options,c);
        numbers.add(options);

        //c.anchor=GridBagConstraints.EAST;
        label2=new Label("Strength =");
        label2.setFont(new Font("Dialog",Font.PLAIN,10));
        gridbag.setConstraints(label2,c);
        numbers.add(label2);

        strength=new TextField(5);
        strength.setFont(new Font("Dialog",Font.PLAIN,10));
        gridbag.setConstraints(strength,c);
        numbers.add(strength);

        label3=new Label("Angle =");
        label3.setFont(new Font("Dialog",Font.PLAIN,10));
        gridbag.setConstraints(label3,c);
        numbers.add(label3);

        angle=new TextField(5);
        angle.setFont(new Font("Dialog",Font.PLAIN,10));
        gridbag.setConstraints(angle,c);
        numbers.add(angle);

        //c.anchor=GridBagConstraints.WEST;
        xD=new Label("x =  0.00");
        xD.setFont(new Font("Dialog",Font.PLAIN,10));
        gridbag.setConstraints(xD,c);
        numbers.add(xD);

        c.gridwidth = GridBagConstraints.REMAINDER;
        yD=new Label("y =  0.00");
        yD.setFont(new Font("Dialog",Font.PLAIN,10));
        gridbag.setConstraints(yD,c);
        numbers.add(yD);

        buttonbar=new Panel();
        buttonbar.setLayout(new GridLayout(1,3,10,5));

        newflow=new Button("New flow");
        newflow.setFont(new Font("Dialog",Font.PLAIN,10));
        buttonbar.add(newflow);
        databut=new Button("Show data");
        databut.setFont(new Font("Dialog",Font.PLAIN,10));
        buttonbar.add(databut);
        mapbut=new Button("Show mapping");
        mapbut.setFont(new Font("Dialog",Font.PLAIN,10));
        buttonbar.add(mapbut);


        legend=new Legend(this);
        baseline=new Panel();
        baseline.setLayout(new BorderLayout());
        baseline.add("East",buttonbar);
        baseline.add("Center",legend);

        drawing=new Drawing(this);

        setLayout(new BorderLayout());
        add("North",numbers);
        add("South",baseline);
        add("Center",drawing);

        strength.setText("1.0");
        angle.setText("0.0");
        setBackground(Color.white);

        drawing.repaint();
        legend.repaint();

        out=new dataPort();

    }

    public Insets insets() {
        return new Insets(30,20,30,20);
    }

    public void paint (Graphics g){
        legend.repaint();
    }

    public boolean handleEvent(Event event) {
    	if (event.id == Event.WINDOW_DESTROY) {
    	    hide();
    	    return true;
    	}
        else
        if (event.id == Event.ACTION_EVENT && event.target == newflow) {
                clickedNewflow();
                return true;
        }
        else
        if (event.id == Event.ACTION_EVENT && event.target == databut) {
                clickeddatabut();
                return true;
        }
        else
        if (event.id == Event.ACTION_EVENT && event.target == mapbut) {
                clickedMapbut();
                return true;
        }
    	return super.handleEvent(event);
    }

    public void clickedMapbut() {
        if(map==null) {
            map=new mapping(this);
            map.resize(640,450);
            map.show();
        }
        else if(!map.isShowing()) {
            map.show();
        }
    }

    public void clickedNewflow() {
        drawing.nf=0;drawing.nst=0;drawing.freeStreamU=0.0;drawing.freeStreamV=0.0;
        drawing.circleOn=false;drawing.kuttaOn=false;
        drawing.repaint();
        if(map!=null) {
            if(map.isShowing()) {
                map.drawing.repaint();
            }
        }

    }

    public void clickeddatabut() {
        if(drawing.nst!=0) {
            out.resize(512,385);
            out.show();
            drawing.record=true;
            drawing.drawStreamline(drawing.xstream[drawing.nst-1],drawing.ystream[drawing.nst-1]);
            drawing.record=false;
        }
    }


}

class Drawing extends Canvas {

    machine root;
    int w,h,xImin,xImax,yImin,yImax,x0,y0,xO=0,yO=0,rO=0;
    double x,y,x1,y1;
    Sing f[]=new Sing[1000]; //f is an array containing all the singularities
    int nf=0,nst=0;  //nf is a counter for the singularities, nst is a counter for the streamlines
    double xstream[]=new double[1000];
    double ystream[]=new double[1000];
    double freeStreamU=0.0,freeStreamV=0.0;
    double circlex,circley,circler,kuttat;
    boolean circleOn=false,kuttaOn=false,record=false;

    Drawing(machine target) {
        root=target;
        setBackground(Color.white);

    }

    public void paint (Graphics g){
        int xI,yI;

        w=size().width;h=size().height;
        xImin=(int)(w*0.10);xImax=xImin+((int)(w*0.85)/50)*50;
        yImin=(int)(h*.05);yImax=yImin+((int)(h*0.9)/50)*50;

        g.setColor(Color.gray);       //Draw grid
        for(int i=xImin;i<=xImax;i+=50) {
            for(int j=yImin;j<=yImax;j+=50) {
                g.drawLine(i-3,j,i+3,j);
                g.drawLine(i,j-3,i,j+3);
            }
        }

        x0=(xImax-xImin)/50/2;
        y0=(yImax-yImin+99)/50/2;

        g.drawLine(x0*50-6+xImin,yImin+y0*50,x0*50+6+xImin,yImin+y0*50);
        g.drawLine(x0*50+xImin,yImin+y0*50-6,x0*50+xImin,yImin+y0*50+6);

                                            //Draw critical points
        if(root.map!=null) {
            if(root.map.isShowing()) {
                for(int i=0;i<root.map.ncrit;i++) {
                    xI=(int)Math.round((root.map.critx[i]+x0)*50.0)+xImin;
                    yI=(int)Math.round((y0-root.map.crity[i])*50.0)+yImin;
                    if(xI>=xImin && xI<=xImax && yI>=yImin && yI<=yImax) {
                    g.setColor(Color.yellow);
                    g.fillOval(xI-2,yI-2,4,4);
                    g.setColor(Color.black);
                    g.drawOval(xI-2,yI-2,4,4);
                    }
                }
            }
        }

        drawFreeStream(g,freeStreamU,freeStreamV);
        if(circleOn) drawCircle(g);
        for(int i=0;i<nf;i++) drawFlow(g,f[i]);
        for(int i=0;i<nst;i++) drawStreamline(xstream[i],ystream[i]);

    }

    void drawCircle(Graphics g) {
        int i,j,i1,j1;
        g.setColor(Color.green);

        for(double th=0.;th<359.;th+=3.) {
            i=(int)Math.round((circlex+circler*Math.cos(th*Math.PI/180.)+x0)*50.)+xImin;
            j=(int)Math.round((y0-circley-circler*Math.sin(th*Math.PI/180.))*50.)+yImin;
            i1=(int)Math.round((circlex+circler*Math.cos((th+3.)*Math.PI/180.)+x0)*50.)+xImin;
            j1=(int)Math.round((y0-circley-circler*Math.sin((th+3.)*Math.PI/180.))*50.)+yImin;
            if((i+i1)/2>=xImin && (i+i1)/2<=xImax && (j+j1)/2>=yImin && (j+j1)/2<=yImax) g.drawLine(i,j,i1,j1);
        }
        if(kuttaOn) {
            i=(int)Math.round((circlex+circler*Math.cos(kuttat)*.95+x0)*50.)+xImin;
            j=(int)Math.round((y0-circley-circler*Math.sin(kuttat)*.95)*50.)+yImin;
            i1=(int)Math.round((circlex+circler*Math.cos(kuttat)*1.05+x0)*50.)+xImin;
            j1=(int)Math.round((y0-circley-circler*Math.sin(kuttat)*1.05)*50.)+yImin;
            if((i+i1)/2>=xImin && (i+i1)/2<=xImax && (j+j1)/2>=yImin && (j+j1)/2<=yImax) g.drawLine(i,j,i1,j1);
        }
    }

    void drawFreeStream(Graphics g,double u, double v) {
        double mag;

        mag=Math.sqrt(u*u+v*v);
        g.setColor(Color.black);
        if(mag>0.0) drawArrow(20,7,4,xImin/2,yImin+(yImax-yImin)/2,Math.atan2(v,u),g);
    }

    void drawArrow(int shaftLength, int headLength, int headWidth, int xPos, int yPos, double angle, Graphics ga) {
        int xa,ya,xb,yb,xc,yc,xd,yd;

        xa=xPos-(int)Math.round(shaftLength/2.0*Math.cos(angle));
        ya=yPos+(int)Math.round(shaftLength/2.0*Math.sin(angle));
        xb=xa+(int)Math.round(shaftLength*Math.cos(angle));
        yb=ya-(int)Math.round(shaftLength*Math.sin(angle));
        xc=xa+(int)Math.round((shaftLength-headLength)*Math.cos(angle)-headWidth/2.0*Math.sin(angle));
        yc=ya-(int)Math.round((shaftLength-headLength)*Math.sin(angle)+headWidth/2.0*Math.cos(angle));
        xd=xa+(int)Math.round((shaftLength-headLength)*Math.cos(angle)+headWidth/2.0*Math.sin(angle));
        yd=ya-(int)Math.round((shaftLength-headLength)*Math.sin(angle)-headWidth/2.0*Math.cos(angle));

        ga.drawLine(xa,ya,xb,yb);
        ga.drawLine(xb,yb,xc,yc);
        ga.drawLine(xb,yb,xd,yd);
    }

    void drawFlow(Graphics g, Sing f) {
        int size=6,size1=6,size2=4;   // scale of source, vortex and doublet symbols
        int xI=(int)Math.round((f.x+x0)*50.0)+xImin;
        int yI=(int)Math.round((y0-f.y)*50.0)+yImin;
        int xI1=(int)Math.round((f.x1+x0)*50.0)+xImin;
        int yI1=(int)Math.round((y0-f.y1)*50.0)+yImin;


        switch (f.id) {
          case 1:   //Source & Sink
          g.setColor(Color.white);
          g.fillOval(xI-size/2,yI-size/2,size,size);
          g.setColor(Color.blue);
          g.drawOval(xI-size/2,yI-size/2,size,size);
          g.drawLine(xI-size/2,yI,xI+size/2,yI);
          if(f.s>0.0) g.drawLine(xI,yI-size/2,xI,yI+size/2);
          break;
          case 2:   //Vortex
          g.setColor(Color.red);
          g.fillOval(xI-2,yI-2,4,4);
          if(f.s>0.0) {
            g.drawArc(xI-size1,yI-size1,size1*2,size1*2,-90,270);
            g.drawLine(xI-size1,yI,xI-size1+3,yI-2);
            g.drawLine(xI-size1,yI,xI-size1-2,yI-3);
          }
          else {
            g.drawArc(xI-size1,yI-size1,size1*2,size1*2,-90,-270);
            g.drawLine(xI+size1,yI,xI+size1+2,yI-3);
            g.drawLine(xI+size1,yI,xI+size1-3,yI-2);
          }
          break;
          case 3:   //Doublet
          g.setColor(Color.black);
          drawArrow(14,3,2,xI,yI,f.a,g);
          g.setColor(Color.white);
          g.fillOval(xI-(int)Math.round(size2*Math.sin(f.a)*.75)-size2/2,yI-(int)Math.round(size2*Math.cos(f.a)*.75)-size2/2,size2,size2);
          g.fillOval(xI+(int)Math.round(size2*Math.sin(f.a)*.75)-size2/2,yI+(int)Math.round(size2*Math.cos(f.a)*.75)-size2/2,size2,size2);
          g.setColor(Color.black);
          g.drawOval(xI-(int)Math.round(size2*Math.sin(f.a)*.75)-size2/2,yI-(int)Math.round(size2*Math.cos(f.a)*.75)-size2/2,size2,size2);
          g.drawOval(xI+(int)Math.round(size2*Math.sin(f.a)*.75)-size2/2,yI+(int)Math.round(size2*Math.cos(f.a)*.75)-size2/2,size2,size2);
          break;
          case 4:   //Source Panel
          g.setColor(Color.blue);
          g.drawLine(xI,yI,xI1,yI1);
          break;
          case 5:   //Vortex Panel
          g.setColor(Color.red);
          g.drawLine(xI,yI,xI1,yI1);
          break;
          case 6:   //Add dye
        }
    }

    public boolean handleEvent(Event event) {
        if (event.id == Event.MOUSE_UP && event.target == this) {
                mouseUpThis(event);
                return true;
        }
        else
        if (event.id == Event.MOUSE_MOVE && event.target == this) {
                mouseMoveThis(event);
                return true;
        }
        else
        if (event.id == Event.MOUSE_DRAG && event.target == this) {
                mouseDragThis(event);
                return true;
        }

        return super.handleEvent(event);
    }


    public void mouseMoveThis(Event ev) {
        int xIndex,yIndex;

        xIndex=ev.x;yIndex=ev.y;
        if(xIndex>=xImin && xIndex<=xImax && yIndex>=yImin && yIndex<=yImax) {
            x=(double)(xIndex-xImin)/50.0-x0;
            y=y0-(double)(yIndex-yImin)/50.0;
            root.xD.setText("x = "+format(x,3));
            root.yD.setText("y = "+format(y,3));
        }

    }

    public void mouseDragThis(Event ev) {
        int xIndex,yIndex;
        xIndex=ev.x;yIndex=ev.y;
        Graphics gs;

        int xI,yI,rI;

        gs=this.getGraphics();

        if(xIndex>=xImin && xIndex<=xImax && yIndex>=yImin && yIndex<=yImax) {
            x1=(double)(xIndex-xImin)/50.0-x0;
            y1=y0-(double)(yIndex-yImin)/50.0;
            root.xD.setText("x = "+format(x1,3));
            root.yD.setText("y = "+format(y1,3));
            xI=(int)Math.round((x+x0)*50.0)+xImin;
            yI=(int)Math.round((y0-y)*50.0)+yImin;


            switch(root.options.getSelectedIndex()) {     //Animate drawing of panels
                case 4:   //Source Panel
                if(xO!=0) {
                    gs.setColor(Color.white);
                    gs.drawLine(xI,yI,xO,yO);
                }
                gs.setColor(Color.blue);
                gs.drawLine(xI,yI,xIndex,yIndex);
                xO=xIndex;yO=yIndex;
                break;
                case 5:   //Vortex Panel
                if(xO!=0) {
                    gs.setColor(Color.white);
                    gs.drawLine(xI,yI,xO,yO);
                }
                gs.setColor(Color.red);
                gs.drawLine(xI,yI,xIndex,yIndex);
                xO=xIndex;yO=yIndex;
                break;
                case 6:    //Circle
                case 7:    //Circle with Kutta Condition
                if(rO!=0) {
                    gs.setColor(Color.white);
                    gs.drawOval(xI-rO,yI-rO,rO*2,rO*2);
                }

⌨️ 快捷键说明

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