Ich erstelle einen Differentialrechner, in dem der Benutzer den Grad ihres Polynoms eingibt und dann die Koeffizienten jedes Terms eingibt. Der Rechner zeigt die resultierende Ableitung in einem Applet-Fenster sowie ein Diagramm der ursprünglichen Funktion an.Java - JApplet zeichnet kein Diagramm - Kann mit Pixeln und Skalierung zu tun haben
Hier ist die Grafikklasse.
package beta;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.List;
import javax.swing.JApplet;
public class GraphingCalc extends JApplet
{
public void drawAxes(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Line2D.Double yaxis = new Line2D.Double(200, 400, 200, 0);
Line2D.Double xaxis = new Line2D.Double(0, 200, 400, 200);
g2.draw(yaxis);
g2.draw(xaxis);
for (int i = 0; i<=20; i++)
{
Line2D.Double ytick = new Line2D.Double(197, 400 - i * 20, 203, 400 - i * 20);
Line2D.Double xtick = new Line2D.Double(400 - i * 20, 203, 400 - i * 20, 197);
g2.draw(ytick);
g2.draw(xtick);
}
}
public void drawFunction(Graphics g, List<Double> l)
{
Graphics2D g2 = (Graphics2D)g;
double x1 = 0;
double y1 = 0;
double x2 = 0;
double y2 = 0;
int size = l.size();
for (double x = -10; x <= 10; x += 0.2)
{
x1 = x;
for (int d = size-1; d>=0; d--)
{
y1 += l.get(d) * Math.pow(x1, d);
}
Point2D.Double first = new Point2D.Double(20 * x1 + 200, -20 * y1 + 200);
x2 = x1 + 0.2;
for (int d = size-1; d>=0; d--)
{
y2 += l.get(d) * Math.pow(x2, d);
}
Point2D.Double second = new Point2D.Double(20 * x2 + 200, -20 * y2 + 200);
Line2D.Double line = new Line2D.Double(first, second);
g2.draw(line);
}
}
}
Hier ist die abgeleitete Berechnungsklasse. Die einzige relevante Teil ist, wenn ich die Liste der Koeffizienten von dem Benutzer coeffList
package beta;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class DerivativeCalculator
{
public DerivativeCalculator()
{
String d = JOptionPane.showInputDialog("Enter the degree of your polynomial: ");
String v = JOptionPane.showInputDialog("Enter the x value "
+ "at which you want to take the derivative: ");
degree = Integer.parseInt(d);
value = Double.parseDouble(v);
coeffList = new ArrayList<Double>();
for (int i = 0; i <= degree; i++)
{
String console = JOptionPane.showInputDialog("Enter the coefficient of the "
+ "x^" + i + " term.");
Double coeff = Double.parseDouble(console);
coeffList.add(coeff);
}
}
public double calc()
{
double dx = 0.00001;
double x1 = value;
double y1 = 0;
for (int d = degree; d >= 0; d--)
{
y1 += coeffList.get(d) * Math.pow(x1, d);
}
double x2 = x1 + dx;
double y2 = 0;
for (int d = degree; d >= 0; d--)
{
y2 += coeffList.get(d) * Math.pow(x2, d);
}
double slope = (y2 - y1)/ (x2 - x1);
DecimalFormat round = new DecimalFormat("##.##");
round.setRoundingMode(RoundingMode.DOWN);
return Double.valueOf(round.format(slope));
}
public String getEquation()
{
String equation = "";
for (int d = degree; d >= 1; d--)
{
equation = equation + String.valueOf(coeffList.get(d)) + "x^" + String.valueOf(d) + " + ";
}
equation = equation + String.valueOf(coeffList.get(0)) + "x^" + String.valueOf(0);
return equation;
}
public String getValue()
{
return String.valueOf(value);
}
public List<Double> getCoeff()
{
return coeffList;
}
private int degree;
private double value;
private List<Double> coeffList;
}
Schließlich bekommen, ist dies die Testklasse ist, die die beiden vorherigen Klassen enthält.
Das Applet wird ausgeführt, zeigt alle abgeleiteten Informationen korrekt an, aber das Funktionsdiagramm wird nicht korrekt gezeichnet. Zum Beispiel, wenn ich x + 5 eingeben, zeichnet das Applet eine Reihe von einzelnen geraden Linien, aber sie sind in der Form einer Parabel geclustert.
Ich vermute sofort, dass es mit der Art und Weise, wie ich den Graphen zeichne, zu tun hat. Ich mache tatsächlich eine Reihe kurzer Linien mit der Länge 0.2.
for (double x = -10; x <= 10; x += 0.2)
{
x1 = x;
for (int d = size-1; d>=0; d--)
{
y1 += l.get(d) * Math.pow(x1, d);
}
Point2D.Double first = new Point2D.Double(20 * x1 + 200, -20 * y1 + 200);
x2 = x1 + 0.2;
for (int d = size-1; d>=0; d--)
{
y2 += l.get(d) * Math.pow(x2, d);
}
Point2D.Double second = new Point2D.Double(20 * x2 + 200, -20 * y2 + 200);
Line2D.Double line = new Line2D.Double(first, second);
g2.draw(line);
}
Was ist das Problem? Irgendwelche Vorschläge?
1) Um eine bessere Hilfe zu erhalten, sollten Sie ein [MCVE] oder [Short, Self Contained, Correct Example] (http://www.sscce.org/) veröffentlichen. 2) Warum ein Applet codieren? Wenn es aufgrund der Angabe des Lehrers geschieht, verweisen Sie bitte auf [Warum CS-Lehrer ** aufhören sollten ** Java-Applets zu unterrichten] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop-teaching-Java-Applets /). 3) Siehe [Java Plugin-Unterstützung nicht mehr unterstützt] (http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) und [Wechsel zu einem Plugin-freien Web] (https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). –