FunktionsPlotter

Status
Nicht offen für weitere Antworten.

dFX

Mitglied
grüß euch alle zusammen !

kann mir jmd vll einen link geben, wo ich eine einleitung oder
beispielcode finde , der sich mit dem plotten einer beliebigen funktionen
zb cos(x) oder auch 14 * log ( 2sin(x) ) unter swing / awt befasst.

ich hab mich hier im forum schon totgesucht, finde
aber nichts zu "echten" kurven, nur linien, die aneinander
gehangen werden. hier ist ein perfektes beispiel:
www.jjam.de/Java/Applets/Mathematik/FunktionsPlotter.html


danke im voraus
dorian
 

Campino

Top Contributor
Du musst mit dem Parser für mathematische Ausdrücke (siehe FAQ) die Funktionsvorschrift zerlegen und dann für jede x-Koordinate einzeln den Wert berechnen.

eventuell verschiebst du mit Graphics.translate() den Ursprung des Koordinatensystems in die Mitte deiner Zeichenfläche(JPanel, Canvas oder Panel) und legst dann mit Graphics.scale(0, -1); die positiven Koordinaten an den hohen Arm (Wenn du verstehen was ich meinen :D )
 

dFX

Mitglied
ok, danke euch soweit.
ich hab mich jetzt belesen und amch mich mal ans werk.
werd dann, wenn ich zufrieden bin, hier mal meine ergebnisse reinsetzen.
cu
 

André Uhres

Top Contributor
Code:
/* 
*Funktionsplotter 0.98 - Created by  [url]www.jjam.de[/url]  2003 
* 
*Decompiled by DJ v3.8.8.85 Copyright 2005 Atanas Neshkov  Date: 05/12/2005 07:02:06 
*Home Page : [url]http://members.fortunecity.com/neshkov/dj.html[/url]  - Check often for new version! 
*Decompiler options: packimports(3) nonlb 
*Source File Name:   FunktionsPlotter.java 
* 
*/ 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 
public class FunktionsPlotter extends JFrame implements ActionListener{ 
    public FunktionsPlotter() { 
        super("FunktionsPlotter"); 
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
        setSize(620,570); 
        setResizable(false); 
        setLocationRelativeTo(null); 
        
        setFont(new Font("MonoSpaced", 0, 12)); 
        getContentPane().setLayout(new FlowLayout()); 
        mp = new MainPanel(); 
        mp.setBackground(new Color(255, 255, 255)); 
        MouseMotionListener mml = (MouseMotionListener)new MouseMotionAdapter(){ 
            public void mouseMoved(MouseEvent e) { 
                int x = e.getX(); 
                int y = e.getY(); 
                if(y < 401 && range > (double)0) { 
                    String xPos = Double.toString((double)(x - 300) / ((double)300 / range)); 
                    xPos = xPos.substring(0, xPos.indexOf(".") + 2); 
                    String yPos = Double.toString((double)(-(y - 200)) / ((double)300 / range)); 
                    yPos = yPos.substring(0, yPos.indexOf(".") + 2); 
                    status.setText(String.valueOf(String.valueOf((new StringBuffer(" ( " + 
                            "")).append(xPos).append(" , ").append(yPos).append(" )")))); 
                } else { 
                    status.setText(""); 
                } 
            } 
            public void mouseExited(MouseEvent e) { 
                status.setText(""); 
            } 
        }; 
        mp.addMouseMotionListener(mml); 
        MouseListener ml = (MouseListener)new MouseAdapter(){ 
            public void mouseExited(MouseEvent e) { 
                status.setText(""); 
            } 
            public void mousePressed(MouseEvent e) { 
                status.setText("Funktionsplotter 0.98 - Created by  [url]www.jjam.de[/url]  2003"); 
            } 
        }; 
        mp.addMouseListener(ml); 
        getContentPane().add(mp); 
        JPanel p = new JPanel(); 
        getContentPane().add(p); 
        p.setLayout(new FlowLayout(1, 20, 25)); 
        functionInput = new JTextField("", 39); 
        rangeInput = new JTextField("", 5); 
        JButton showFunctionButton = new JButton("  Start  "); 
        getRootPane().setDefaultButton(showFunctionButton); 
        showFunctionButton.addActionListener(this); 
        p.add(functionInput); 
        p.add(rangeInput); 
        p.add(showFunctionButton); 
        p.setVisible(true); 
        functionInput.setBackground(new Color(255, 255, 255)); 
        rangeInput.setBackground(new Color(255, 255, 255)); 
        showFunctionButton.setBackground(new Color(255, 255, 255)); 
        functionInput.setText("f(x) = 3*sin(x^2+3)/x"); 
        rangeInput.setText("3*pi"); 
        status = new JLabel(); 
        
        getContentPane().add(status); 
        JMenuBar menubar = new JMenuBar(); 
        JMenu fileMenu = new JMenu("File"); 
        JMenu helpMenu = new JMenu("Help"); 
        menubar.add(fileMenu); 
        menubar.add(helpMenu); 
        JMenuItem exitI = new JMenuItem("Exit"); 
        fileMenu.add(exitI); 
        JMenuItem helpI = new JMenuItem("Help"); 
        helpMenu.add(helpI); 
        setJMenuBar(menubar); 
        exitI.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                dispose(); 
            } 
        }); 
        helpI.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                JOptionPane.showMessageDialog(null, "Hinweise zur Benutzung:\n\n" 
                        
                        +"Der Funktionsplotter beherrscht die Operationen +, -, *," + 
                        " /, ^ und E (mal 10 hoch),\n" 
                        +"die Funktionen sin(x), cos(x), tan(x), ln(x), sqrt(x), " + 
                        "abs(x) und exp(x),\n" 
                        +"die Sprung-Funktionen sgn(x) (Signum-Funktion), H(x) " + 
                        "(Heaviside-Funktion) und int(x) (Gaußklammer-Funktion),\n" 
                        +"sowie die Konstanten pi und e.\n" 
                        +"\n" 
                        +"Im zweiten Feld wird die Länge der x-Achse ab ihrem " + 
                        "Ursprung bestimmt. Möglich sind hier auch Angaben wie 2*pi.\n" 
                        +"\n" 
                        +"Das Multiplikationszeichen * kann in den üblichen Fällen " + 
                        "auch weggelassen werden. (Zum Beispiel 2x statt 2*x u.s.w.)\n" 
                        +"\n" 
                        +"Fast alle Funktionen, die mit diesen Mitteln zu konstruieren " + 
                        "sind, sollten vom Funktionsplotter auch korrekt angezeigt werden. \n" + 
                        "Lediglich bei Funktionen mit Abschnitten sehr extremer " + 
                        "Steigung (fast senkrecht) kann es zu einer fehlerhaften " + 
                        "Darstellung kommen.\n" 
                        
                        ); 
            } 
        }); 
    } 
    public static void main(String[] args) { 
        new FunktionsPlotter().setVisible(true); 
    } 
    public void actionPerformed(ActionEvent e) { 
        mp.repaint(); 
    } 
    public String toLowerLetters(String s) { 
        if(s.indexOf("E") >= 0) { 
            int i; 
            for(i = 0; i < s.length() - 1; i++) 
                if(!s.substring(i, i + 1).equals("E") || s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X') 
                    s = replaceSubString(s, i, i, s.substring(i, i + 1).toLowerCase()); 
            
            s = replaceSubString(s, i, i, s.substring(i, i + 1).toLowerCase()); 
        } else { 
            s = s.toLowerCase(); 
        } 
        return s; 
    } 
    
    public String debugInput(String s) { 
        char c = ' '; 
        char c2 = ' '; 
        char c3 = ' '; 
        for(int i = 0; i < s.length() - 1; i++) { 
            if(s.charAt(i) != 'E') 
                continue; 
            if(i > 0) 
                c = s.charAt(i - 1); 
            if(i == 0 || c == '^' || c == '*' || c == '/' || c == '+' || c == '-' || c == '(') { 
                s = replaceSubString(s, i, i, "1E"); 
                continue; 
            } 
            if(c == 'x') 
                s = replaceSubString(s, i, i, "*1E"); 
        } 
        
        int i; 
        for(; s.indexOf("log") >= 0; s = replaceSubString(s, i + 1, i + 2, "n")) 
            i = s.indexOf("log"); 
        
        for(i = 0; i < s.length(); i++) { 
            if(i > 0 && s.charAt(i) == 'x') { 
                c = s.charAt(i - 1); 
                if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != '(' && c != 'e') 
                    s = replaceSubString(s, i, i, "*x"); 
            } 
            if(i >= s.length() - 1 || s.charAt(i) != 'x') 
                continue; 
            c = s.charAt(i + 1); 
            if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != ')' && c != 'p') 
                s = replaceSubString(s, i, i, "x*"); 
        } 
        
        if(s.indexOf("e") >= 0) { 
            for(i = 0; i < s.length() - 1; i++) { 
                if(s.charAt(i) != 'e') 
                    continue; 
                if(s.indexOf("expi") == i) { 
                    s = replaceSubString(s, i, i + 3, "e*x*pi"); 
                    continue; 
                } 
                if(s.indexOf("ex") == i && (i + 1 == s.length() - 1 || s.charAt(i + 2) != 'p')) { 
                    s = replaceSubString(s, i, i + 1, "e*x"); 
                    continue; 
                } 
                c = s.charAt(i + 1); 
                if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != ')' && c != 'x') 
                    s = replaceSubString(s, i, i, "e*"); 
            } 
            
        } 
        if(s.indexOf("pi") >= 0) { 
            for(i = 0; i < s.length() - 1; i++) { 
                if(i > 0 && s.charAt(i) == 'p' && s.charAt(i + 1) == 'i') { 
                    c = s.charAt(i - 1); 
                    if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != '(') 
                        s = replaceSubString(s, i, i + 1, "*pi"); 
                } 
                if(i >= s.length() - 2 || s.charAt(i) != 'p' || s.charAt(i + 1) != 'i') 
                    continue; 
                c = s.charAt(i + 2); 
                if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != ')') 
                    s = replaceSubString(s, i, i + 1, "pi*"); 
            } 
            
        } 
        for(i = 1; i < s.length() - 2; i++) { 
            if(s.charAt(i) != '(') 
                continue; 
            c = s.charAt(i - 1); 
            if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != '(' 
                    && c != 'n' && c != 's' && c != 'p' && c != 'g' && c != 't' && c != 'h' && c != 't') 
                s = replaceSubString(s, i, i, "*("); 
        } 
        
        for(i = 2; i < s.length() - 1; i++) { 
            if(s.charAt(i) != ')') 
                continue; 
            c = s.charAt(i + 1); 
            if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != ')') 
                s = replaceSubString(s, i, i, ")*"); 
        } 
        
        for(i = 1; i < s.length(); i++) { 
            c = s.charAt(i); 
            if(i < s.length() - 1) 
                c2 = s.charAt(i + 1); 
            if(i < s.length() - 2) 
                c3 = s.charAt(i + 2); 
            if((c != 's' || c2 != 'i' && c2 != 'g' && c2 != 'q') && c != 'c' 
                    && (c != 't' || c2 != 'a') && c != 'l' && c != 'e' 
                    && (c != 'a' || c2 != 'b') && c != 'h' 
                    && (c != 'i' || c2 != 'n' || c3 != 't')) 
                continue; 
            c = s.charAt(i - 1); 
            if(c != '^' && c != '/' && c != '*' && c != '-' && c != '+' && c != '(') 
                s = replaceSubString(s, i, i, "*".concat(String.valueOf(String.valueOf(s.substring(i, i + 1))))); 
        } 
        
        for(; s.indexOf(",") >= 0; s = replaceSubString(s, i, i, ".")) 
            i = s.indexOf(","); 
        
        return s; 
    } 
    
    public String insertArgumentValue(String s) { 
        int index; 
        for(; s.indexOf("x") >= 0; s = replaceSubString(s, index, index, x)) 
            index = s.indexOf("x"); 
        
        return replaceSigns(s); 
    } 
    
    public String insertConstants(String s) { 
        int i; 
        for(; s.indexOf("pi") >= 0; s = replaceSubString(s, i, i + 1, "3.141592654")) 
            i = s.indexOf("pi"); 
        
        if(s.indexOf("e") >= 0) { 
            for(i = s.indexOf("e"); i < s.length(); i++) 
                if(s.charAt(i) == 'e' && (i == s.length() - 1 || s.charAt(i + 1) != 'x')) 
                    s = replaceSubString(s, i, i, "2.718281828"); 
            
        } 
        return s; 
    } 
    
    public String replaceSigns(String s) { 
        String signsOut[] = { 
            "--", "++", "+-", "-+" 
        }; 
        String signsIn[] = { 
            "+", "+", "-", "-" 
        }; 
        for(int i = 0; i < 4; i++) { 
            int index; 
            for(; s.indexOf(signsOut[i]) >= 0; s = replaceSubString(s, index, index + 1, signsIn[i])) 
                index = s.indexOf(signsOut[i]); 
            
        } 
        
        return s; 
    } 
    
    public String doAllOperations(String s) { 
        String strOperator[] = { 
            "^", "/", "*", "-", "+" 
        }; 
        String strOperationTerm = ""; 
        int endIndex = 0; 
        for(; s.indexOf("(") >= 0; s = replaceSigns(s)) { 
            int beginIndex = s.indexOf("("); 
            int i; 
            for(i = beginIndex + 1; i < s.length(); i++) { 
                if(s.charAt(i) == '(') { 
                    beginIndex = i; 
                    continue; 
                } 
                if(s.charAt(i) != ')') 
                    continue; 
                endIndex = i; 
                strOperationTerm = s.substring(beginIndex + 1, endIndex); 
                for(int j = 0; j < 5; j++) 
                    strOperationTerm = innerOperation(strOperationTerm, strOperator[j]); 
                
                break; 
            } 
            
            if(i == s.length()) 
                return ""; 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("sin")) { 
                double dblMathResult = Math.sin(Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("cos")) { 
                double dblMathResult = Math.cos(Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("tan")) { 
                double dblMathResult = Math.tan(Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 2 >= 0 && s.substring(beginIndex - 2, beginIndex).equals("ln")) { 
                double dblMathResult = Math.log(Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 2, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 4 >= 0 && s.substring(beginIndex - 4, beginIndex).equals("sqrt")) { 
                double dblMathResult = Math.sqrt(Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 4, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("abs")) { 
                double dblMathResult = Math.abs(Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("exp")) { 
                double dblMathResult = Math.pow(2.7182818284590451D, Double.parseDouble(strOperationTerm)); 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("sgn")) { 
                double dblMathResult; 
                if(Double.parseDouble(strOperationTerm) > (double)0) { 
                    dblMathResult = 1.0D; 
                    if(sgnBefore == 0 || sgnBefore == -1) 
                        jump = true; 
                    sgnBefore = 1; 
                } else{ 
                    if(Double.parseDouble(strOperationTerm) < (double)0) { 
                        dblMathResult = -1D; 
                        if(sgnBefore == 0 || sgnBefore == 1) 
                            jump = true; 
                        sgnBefore = -1; 
                    } else { 
                        dblMathResult = 0.0D; 
                        jump = true; 
                        sgnBefore = 0; 
                    } 
                } 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 1 >= 0 && s.substring(beginIndex - 1, beginIndex).equals("h")) { 
                double dblMathResult; 
                if(Double.parseDouble(strOperationTerm) > (double)0) { 
                    dblMathResult = 1.0D; 
                    if(hBefore == 0) 
                        jump = true; 
                    hBefore = 1; 
                } else { 
                    dblMathResult = 0.0D; 
                    if(hBefore == 1) 
                        jump = true; 
                    hBefore = 0; 
                } 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 1, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(beginIndex - 3 >= 0 && s.substring(beginIndex - 3, beginIndex).equals("int")) { 
                double dblMathResult = Math.floor(Double.parseDouble(strOperationTerm)); 
                if(intBefore == dblMathResult - (double)1 || intBefore == dblMathResult + (double)1) 
                    jump = true; 
                intBefore = dblMathResult; 
                strOperationTerm = Double.toString(dblMathResult); 
                s = replaceSubString(s, beginIndex - 3, endIndex, String.valueOf(String.valueOf(( 
                        new StringBuffer("(")).append(strOperationTerm).append(")")))); 
                continue; 
            } 
            if(endIndex + 1 < s.length() && s.charAt(endIndex + 1) == '^' 
                    && Double.parseDouble(strOperationTerm) <= (double)0) { 
                int j = 0; 
                i = endIndex + 2; 
                do { 
                    if(i >= s.length()) 
                        break; 
                    if(s.charAt(i) == '(') 
                        j++; 
                    else 
                        if(s.charAt(i) == ')') 
                            j--; 
                    if(j == 0) { 
                        char c = s.charAt(i); 
                        if(c == '/' || c == '*' || (c == '-' || c == '+') 
                        && s.charAt(i - 1) != 'E' && i != endIndex + 2) 
                            break; 
                    } 
                    if(j < 0) 
                        break; 
                    i++; 
                } while(true); 
                if(j > 0) 
                    return ""; 
                if(s.substring(endIndex + 2, i).indexOf("(") >= 0 
                        || s.substring(endIndex + 2, i).indexOf("^") >= 0) { 
                    s = replaceSubString(s, endIndex + 2, i - 1, 
                            doAllOperations(s.substring(endIndex + 2, i))); 
                } else { 
                    double dblLeft = Double.parseDouble(strOperationTerm); 
                    double dblRight = Double.parseDouble(s.substring(endIndex + 2, i)); 
                    strOperationTerm = Double.toString(Math.pow(dblLeft, dblRight)); 
                    s = replaceSubString(s, beginIndex, i - 1, strOperationTerm); 
                } 
            } else { 
                s = replaceSubString(s, beginIndex, endIndex, strOperationTerm); 
            } 
        } 
        
        for(int j = 0; j < 5; j++) 
            s = innerOperation(s, strOperator[j]); 
        
        return s; 
    } 
    
    public String innerOperation(String strOperationTerm, String strOperator) { 
        int operatorIndex = 0; 
        double dblResult = 0.0D; 
        for(; strOperationTerm.substring(1).indexOf(strOperator) >= 0; 
        strOperationTerm = replaceSigns(strOperationTerm)) { 
            int i; 
            if(strOperator.equals("-") || strOperator.equals("+")) { 
                char c = strOperator.charAt(0); 
                for(operatorIndex = 1; operatorIndex < strOperationTerm.length() - 1 
                        && (strOperationTerm.charAt(operatorIndex) != c 
                        || strOperationTerm.charAt(operatorIndex - 1) == 'E'); operatorIndex++); 
                if(operatorIndex == strOperationTerm.length() - 1) 
                    break; 
            } else 
                if(strOperator.equals("^")){ 
                
                for(i = strOperationTerm.indexOf("^"); i < strOperationTerm.length(); i++) 
                    if(strOperationTerm.charAt(i) == '^') 
                        operatorIndex = i; 
                } else 
                    operatorIndex = strOperationTerm.indexOf(strOperator); 
            i = operatorIndex - 2; 
            do { 
                if(i < 0) 
                    break; 
                char c = strOperationTerm.charAt(i); 
                if(c == '^' || c == '/' || c == '*' || (c == '-' || c == '+') 
                && (i != 0 && strOperationTerm.charAt(i - 1) != 'E' || i == 0 && strOperator.equals("^"))) 
                    break; 
                i--; 
            } while(true); 
            String strLeft = strOperationTerm.substring(i + 1, operatorIndex); 
            strLeft = insertArgumentValue(strLeft); 
            double dblLeft = Double.parseDouble(strLeft); 
            int beginIndex = i + 1; 
            i = operatorIndex + 2; 
            do { 
                if(i >= strOperationTerm.length()) 
                    break; 
                char c = strOperationTerm.charAt(i); 
                if(c == '^' || c == '/' || c == '*' || (c == '-' || c == '+') 
                && strOperationTerm.charAt(i - 1) != 'E') 
                    break; 
                i++; 
            } while(true); 
            String strRight = strOperationTerm.substring(operatorIndex + 1, i); 
            strRight = insertArgumentValue(strRight); 
            double dblRight = Double.parseDouble(strRight); 
            int endIndex = i - 1; 
            if(strOperator.equals("^")) 
                dblResult = Math.pow(dblLeft, dblRight); 
            else 
                if(strOperator.equals("*")) 
                    dblResult = dblLeft * dblRight; 
                else 
                    if(strOperator.equals("/")) 
                        dblResult = dblLeft / dblRight; 
                    else 
                        if(strOperator.equals("-")) 
                            dblResult = dblLeft - dblRight; 
                        else 
                            if(strOperator.equals("+")) 
                                dblResult = dblLeft + dblRight; 
            strOperationTerm = replaceSubString(strOperationTerm, beginIndex, 
                    endIndex, Double.toString(dblResult)); 
        } 
        
        if(strOperationTerm.indexOf("x") >= 0) 
            strOperationTerm = insertArgumentValue(strOperationTerm); 
        return strOperationTerm; 
    } 
    
    public String replaceSubString(String s, int beginIndex, int endIndex, String middle) { 
        String left = s.substring(0, beginIndex); 
        String right = s.substring(endIndex + 1); 
        return String.valueOf(String.valueOf(( 
                new StringBuffer(String.valueOf(String.valueOf(left)))).append(middle).append(right))); 
    } 
    
    public void getPaintData(Graphics g2) { 
        String fx = functionInput.getText(); 
        int index = -1; 
        for(; fx.indexOf(" ") >= 0; fx = replaceSubString(fx, index, index, "")) 
            index = fx.indexOf(" "); 
        
        fx = fx.substring(fx.indexOf("=") + 1); 
        fx = toLowerLetters(fx); 
        fx = debugInput(fx); 
        fx = replaceSigns(fx); 
        functionInput.setText("f(x) = ".concat(String.valueOf(String.valueOf(fx)))); 
        functionInput.select(100, 100); 
        fx = insertConstants(fx); 
        try { 
            if(rangeInput.getText().indexOf("x") >= 0) { 
                range = 0.0D; 
            } else { 
                String r = debugInput(toLowerLetters(rangeInput.getText())); 
                range = Double.parseDouble(doAllOperations(insertConstants(r))); 
                if(range > (double)0) { 
                    rangeInput.setText(r); 
                    rangeInput.select(100, 100); 
                } 
            } 
        } catch(Exception e) { 
            range = 0.0D; 
        } 
        paintCurve(g2, fx, range); 
    } 
    
    private void paintCurve(Graphics g2, String fx, double range) { 
        g2.setColor(new Color(0, 0, 200)); 
        double zoom = (double)300 / range; 
        int xStartPoint = -1; 
        int yStartPoint = -1; 
        int xFinalPoint = -1; 
        int yFinalPoint = -1; 
        int xCoord[] = new int[603]; 
        int yCoord[] = new int[603]; 
        boolean yValue[] = new boolean[603]; 
        boolean yInfinity[] = new boolean[603]; 
        boolean jump2 = false; 
        label0: 
            for(int i = 0; i < 603; i++) { 
                x = Double.toString((double)(i - 1 - 300) / zoom); 
                jump = false; 
                try { 
                    double y = Double.parseDouble(doAllOperations(fx)); 
                    xCoord[i] = i - 1; 
                    yCoord[i] = (int)(-y * zoom + (double)200); 
                    yValue[i] = true; 
                    if(i > 2 && yValue[i - 1] && yValue[i - 2] && yInfinity[i - 3]) { 
                        int grad = getGradientCase(yCoord[i], yCoord[i - 1], 
                                yCoord[i - 2]); 
                        if(grad == 1 || grad == 3 || grad == 5) 
                            g2.drawLine(xCoord[i - 2], yCoord[i - 2], 
                                    xCoord[i - 2] - 1, 1401); 
                        else 
                            if(grad == 2 || grad == 4 || grad == 6) 
                                g2.drawLine(xCoord[i - 2], yCoord[i - 2], 
                                        xCoord[i - 2] - 1, -1000); 
                    } 
                    if(i > 0 && yValue[i - 1]) { 
                        if(yCoord[i - 1] >= 0 && yCoord[i - 1] <= 400 && yCoord[i] >= 0 
                                && yCoord[i] <= 400) { 
                            if(i > 1 && yValue[i - 2] && (yCoord[i - 2] < 0 
                                    || yCoord[i - 2] > 400)) { 
                                if(!jump2) { 
                                    int grad = getGradientCase(yCoord[i], 
                                            yCoord[i - 1], yCoord[i - 2]); 
                                    if(grad == 5) 
                                        g2.drawLine(xCoord[i - 1], yCoord[i - 1], 
                                                xCoord[i - 2], -1000); 
                                    else 
                                        if(grad == 6) 
                                            g2.drawLine(xCoord[i - 1], yCoord[i - 1], 
                                                    xCoord[i - 2], 1400); 
                                        else 
                                            g2.drawLine(xCoord[i - 1], yCoord[i - 1], 
                                                    xCoord[i - 2], yCoord[i - 2]); 
                                } else { 
                                    jump2 = false; 
                                } 
                                xStartPoint = xCoord[i - 1]; 
                                yStartPoint = yCoord[i - 1]; 
                            } 
                            if(jump) { 
                                g2.drawLine(xStartPoint, yStartPoint, 
                                        xCoord[i - 1], yCoord[i - 1]); 
                                xStartPoint = xCoord[i]; 
                                yStartPoint = yCoord[i]; 
                                continue; 
                            } 
                            if(yCoord[i - 1] != yCoord[i] && i < 602) { 
                                int j = i - 2; 
                                do { 
                                    if(j < 0) 
                                        continue label0; 
                                    if(yCoord[j] != yCoord[i - 1]) { 
                                        xFinalPoint = xCoord[j + (i - 1 - j) / 2 + 1]; 
                                        yFinalPoint = yCoord[i - 1]; 
                                        g2.drawLine(xStartPoint, yStartPoint, 
                                                xFinalPoint, yFinalPoint); 
                                        xStartPoint = xFinalPoint; 
                                        yStartPoint = yFinalPoint; 
                                        continue label0; 
                                    } 
                                    j--; 
                                } while(true); 
                            } 
                            if(i == 602) 
                                g2.drawLine(xStartPoint, yStartPoint, xCoord[i], yCoord[i]); 
                            continue; 
                        } 
                        if(yCoord[i - 1] >= 0 && yCoord[i - 1] <= 400) { 
                            if(xCoord[i - 1] - xStartPoint == 1) 
                                g2.drawLine(xStartPoint, yStartPoint, xCoord[i - 1], yCoord[i - 1]); 
                            else 
                                g2.drawLine(xCoord[i - 1], -1, xCoord[i - 1], 401); 
                            if(jump) { 
                                jump = false; 
                                continue; 
                            } 
                            if(i <= 1 || !yValue[i - 2] || yCoord[i - 2] < 0 || yCoord[i - 2] > 400) 
                                continue; 
                            int grad = getGradientCase(yCoord[i - 2], yCoord[i - 1], yCoord[i]); 
                            if(grad == 5) { 
                                g2.drawLine(xCoord[i - 1], yCoord[i - 1], xCoord[i], -1000); 
                                continue; 
                            } 
                            if(grad == 6) 
                                g2.drawLine(xCoord[i - 1], yCoord[i - 1], xCoord[i], 1400); 
                            else 
                                g2.drawLine(xCoord[i - 1], yCoord[i - 1], xCoord[i], yCoord[i]); 
                            continue; 
                        } 
                        if(yCoord[i] >= 0 && yCoord[i] <= 400 && jump) 
                            jump2 = true; 
                    } else { 
                        xStartPoint = xCoord[i]; 
                        yStartPoint = yCoord[i]; 
                    } 
                    continue; 
                } catch(Exception e) { 
                    yValue[i] = false; 
                    if(i > 0 && yValue[i - 1] && yCoord[i - 1] >= 0 && yCoord[i - 1] <= 400) 
                        g2.drawLine(xStartPoint, yStartPoint, xCoord[i - 1], yCoord[i - 1]); 
                    String inf = e.getMessage(); 
                    if(!inf.equals("Infinity") && !inf.equals("+Infinity") && !inf.equals("-Infinity")) 
                        continue; 
                } 
                if(i > 2 && yValue[i - 3] && yValue[i - 2] && yValue[i - 1]) { 
                    int grad = getGradientCase(yCoord[i - 3], yCoord[i - 2], yCoord[i - 1]); 
                    if(grad == 1 || grad == 3 || grad == 5) 
                        g2.drawLine(xCoord[i - 1], yCoord[i - 1], xCoord[i - 1] + 1, 1401); 
                    else 
                        if(grad == 2 || grad == 4 || grad == 6) 
                            g2.drawLine(xCoord[i - 1], yCoord[i - 1], xCoord[i - 1] + 1, -1000); 
                } 
                yInfinity[i] = true; 
            } 
            
    } 
    
    public int getGradientCase(int a, int b, int c) { 
        if(a < b && b < c) { 
            if(b - a < c - b) 
                return 1; 
            if(b - a > c - b) 
                return 2; 
        } else 
            if(a > b && b > c) { 
            if(b - c < a - b) 
                return 3; 
            if(b - c > a - b) 
                return 4; 
            } else { 
            if(a > b && b < c) 
                return 5; 
            if(a < b && b > c) 
                return 6; 
            } 
        return 0; 
    } 
    private MainPanel mp ; 
    private JLabel status; 
    private JTextField functionInput; 
    private JTextField rangeInput; 
    private String x; 
    private boolean jump; 
    private int sgnBefore; 
    private int hBefore; 
    private double intBefore; 
    private double range; 
    class MainPanel extends JPanel{ 
        public MainPanel(){ 
            setBorder(new LineBorder(Color.black)); 
            setPreferredSize(new Dimension(600,400)); 
        } 
        public void paintComponent(Graphics g) { 
            super.paintComponent(g); 
            g.setColor(new Color(210, 210, 200)); 
            g.drawLine(2, 200, 598, 200); 
            g.drawLine(300, 2, 300, 398); 
            Graphics2D g2 = (Graphics2D)g; 
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setColor(new Color(190, 190, 180)); 
            g2.drawLine(298, 9, 300, 2); 
            g2.drawLine(300, 2, 302, 9); 
            g2.drawLine(591, 198, 598, 200); 
            g2.drawLine(598, 200, 591, 202); 
            getPaintData(g2); 
        } 
    } 
}
 

dFX

Mitglied
wo hast du denn den quellcode her?
ich hab den nicht auf der seite gefunden !?
aber umso besser ^^

also compilieren geht, aber
beim aufruf kriege ich folgende meldung:


C:\>java -classpath . FunktionsPlotter
Exception in thread "main" java.lang.Error: Do not use FunktionsPlotter.setLayout() use FunktionsPlo
tter.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(Unknown Source)
at javax.swing.JFrame.setLayout(Unknown Source)
at FunktionsPlotter.<init>(FunktionsPlotter.java:15)
at FunktionsPlotter.main(FunktionsPlotter.java:112)



was ist zu tun ?

[edit]

zeile 23: getContentPane().setLayout(new FlowLayout());
zeile 55: getContentPane().add(mp);
zeile 57: getContentPane().add(mp);
zeile 75: getContentPane().add(mp);


Ikarus hat gesagt.:
Vielleicht hat er's gedownloaded und decompiliert. Das ist ja bloß eine Klasse

jo hab ich auch gemerkt, als ich den quellcode gelesen habe =)
 

André Uhres

Top Contributor
Stringvergleich mit == ist nicht risikofrei !!!
Danke für den Hinweis. Der Code wurde korrigiert.

Erklärung:
Der Code setzte einen optimierenden Compiler voraus.
Da jedoch die Optimierung bei implizit erzeugten String-Objekten
nicht zwingend vorgeschrieben ist, muss zum Vergleich des Inhalts
zweier String-Objekte die Methode equals() der Klasse String
verwendet werden.

wo hast du denn den quellcode her?
Das steht am Anfang drin:
Code:
*Funktionsplotter 0.98 - Created by  [url]www.jjam.de[/url]  2003 
* 
*Decompiled by DJ ...

Do not use FunktionsPlotter.setLayout() use FunktionsPlotter.getContentPane().setLayout() instead
Den Code habe ich mit 1.5 getestet. Ich habe aber deine Änderungen jetzt eingefügt.
Dann läuft's auch mit 1.4.
 

André Uhres

Top Contributor
Wenn ein String implizit erzeugt wird (d.h. ohne expliziten Aufruf von new),
dann wird dabei im Heap nicht immer ein neues String-Objekt angelegt.
Das geschieht aber nur bei optimierenden Compilern.
Im folgenden Beispiel werden zwei Referenzvariablen name und gleicherName
vom Typ String angelegt und mit "Anja" initialisiert:
Code:
String name = "Anja";
String gleicherName = "Anja";
Der optimierende Compiler erzeugt für beide Referenzvariablen dieselbe Referenz.
Diese Referenz wird vom Laufzeitsystem umgesetzt in eine Referenz auf das vom Laufzeitsystem
im Heap angelegte String-Objekt. Damit zeigen beide Referenzvariablen auf dasselbe String-Objekt.
Folgender Code ist in dem Fall korrekt:
Code:
if(name == gleicherName){
   //beide Namen sind gleich
}
Das Risiko dabei: es kann nicht unbedingt sichergestellt werden, daß der Compiler die
implizit erzeugten String-Objekte in dieser Weise optimiert.
"if(name == gleicherName)" ist aber nur "true" wenn beide Referenzvariablen auf dasselbe String-Objekt zeigen.
Wenn es zwei verschiedene String-Objekte sind, dann ist "if(name == gleicherName)" immer "false",
egal ob der Inhalt der beiden String-Objekte gleich ist oder nicht.
Risikofrei ist in diesem Fall nur der Gebrauch der Methode equals() der Klasse String:
Code:
if(name.equals(gleicherName)){
   //beide Namen sind gleich
}
PS: nicht nur der Compiler kann Optimierungen durchführen, auch der Interpreter hat die Möglichkeit,
eine ähnliche Speicherplatzoptimierung durchzuführen: existiert schon ein String-Objekt mit gleichem
Inhalt, wird kein neues Objekt angelegt. Es ist aber gleichfalls nicht unbedingt sicher,
ob der Interpreter diese Optimierung durchführt.
 

dFX

Mitglied
vielen dank. du bist der beste !
hab ich mich schon für das decompilieren bedankt ?
falls nein: DAAAANKE SCHÖN !


grüße
dorian
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben