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

📄 context.java

📁 Java Design Pattern:Strategy 希望对大家有所帮助!
💻 JAVA
字号:
import java.awt.*;
import java.util.*;

public class Context {
    //this object selects one of the strategies
    //to be used for plotting
    private PlotStrategy plotStrategy;
    private float x[], y[];
//---------------------------------
    public Context() {
        setLinePlot();
    }
    public void setBarPlot() {
        plotStrategy = new BarPlotStrategy();
    }
    public void setLinePlot() {
        plotStrategy = new LinePlotStrategy();
    }
//---------------------------------
    public void plot() {
        plotStrategy.plot(x, y);
    }
//---------------------------------
    public void setPenColor(Color c) {
        plotStrategy.setPenColor(c);
    }
    //---------------------------------
    public void readData(String filename) {
        StringTokenizer tok;
        InputFile f = new InputFile(filename);
        Vector xv = new Vector();
        Vector yv = new Vector();
        String s ="";
        //read data into 2 Vectors
        while (s != null) {
            s =f.readLine();   //read a line at a time
            if (s != null) {
                tok = new StringTokenizer(s);  
                xv.addElement(tok.nextToken());   //x data
                yv.addElement(tok.nextToken());   //y data
            }
        }
        f.close();
        //copy data into two float arrays
        x = new float[xv.size()];
        y = new float[yv.size()];
        for (int i=0; i< xv.size(); i++) {
            x[i] = new Float((String)xv.elementAt(i)).floatValue();
            y[i] = new Float((String)yv.elementAt(i)).floatValue();
        }
    }
}

⌨️ 快捷键说明

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