Java - JApplet不绘制图形 - 可能与像素和缩放

时间:2016-05-08 06:20:27

标签: java swing graphics applet japplet

我正在创建一个导数计算器,用户输入多项式的次数,然后输入每个项的系数。计算器在小程序窗口中显示结果导数以及原始函数的图形。

这是图表类。

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);
        }
    }
}

这是衍生计算类。唯一相关的部分是当我从用户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;

}

最后,这是包含前两个类的测试类。

package beta;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class DerivativeCalculatorTest extends JApplet
{
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        setSize(800,800);

        DerivativeCalculator myDerivCalc = new DerivativeCalculator();
        g2.drawString(String.valueOf(myDerivCalc.calc()), 10, 100);
        g2.drawString(myDerivCalc.getEquation(), 10, 40);
        g2.drawString(myDerivCalc.getValue(), 10, 70);

        GraphingCalc myGrapher = new GraphingCalc();
        myGrapher.drawAxes(g2);
        myGrapher.drawFunction(g2, myDerivCalc.getCoeff());
    }

}

小程序运行,正确显示所有衍生信息,但功能图无法正确绘制。例如,当我输入x + 5时,applet会绘制一堆单独的直线,但它们以抛物线的形状聚类。

我立即怀疑它与绘制图形的方式有关。我实际上制作了一堆长度为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);
            }

有什么问题?有什么建议吗?

1 个答案:

答案 0 :(得分:4)

一些要点:

您的GraphingCalc课程不应extends JApplet以及绘图线,您不需要创建一些Line2D对象,然后绘制它们。您只需拨打drawLine方法:

即可
package beta;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;

public class GraphingCalc
{
    public void drawAxes(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;

        g2.drawLine(200, 400, 200, 0);
        g2.drawLine(0, 200, 400, 200);

        for (int i = 0; i<=20; i++)
        {
            g2.drawLine(197, 400 - i * 20, 203, 400 - i * 20);
            g2.drawLine(400 - i * 20, 203, 400 - i * 20, 197);
        }
    }

    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);
            }

            x2 = x1 + 0.2;
            for (int d = size-1; d>=0; d--)
            {
                y2 += l.get(d) * Math.pow(x2, d);
            }

            g2.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
        }
    }
}

这样您的程序就会显示轴和刻度。同样对于1度的多项式,它绘制一条线,但不知怎的,我可以告诉它没有正确绘制多项式。也许你想纠正一些计算。正如您所看到的,具有这些系数的一阶简单多项式不应该如下所示:

enter image description here 祝你好运。