java - 绘制图表

标签 java swing graph jfreechart jgrapht

您好,我对 java 很陌生,但是我需要在 JApplet 中绘制图表。我已经成功地声明了所有变量和类,方程正在工作(当它自己编译时)。但我得到的只是一条直线!谁能告诉我我做错了什么?! 在此小程序中,将要求用户插入 abcd 的值以及 x 轴的最小值和最大值

这是我的代码......任何帮助将不胜感激:)

package CubicEquationSolver;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public class graphApplet extends JApplet implements ActionListener {

    private static class g {
        public g() {
        }
    }

    int a;
    int b;
    int c;
    int d;

    int minimumX;
    int maximumX;
    int minimumY;
    int maximumY;

    int xCoOrdinates[];
    int yCoOrdinates[];

    int y;

    // Setting labels
    JLabel labelA = new JLabel("Enter value of A");
    JLabel labelB = new JLabel("Enter value of B");
    JLabel labelC = new JLabel("Enter value of C");
    JLabel labelD = new JLabel("Enter value of D");
    JLabel labelMinX = new JLabel("Minimum X value");
    JLabel labelMaxX = new JLabel("Maximum X value");
    JLabel message = new JLabel("Please insert your values");

    // Values will be entered here using JTextField
    JTextField textA = new JTextField();
    JTextField textB = new JTextField();
    JTextField textC = new JTextField();
    JTextField textD = new JTextField();
    JTextField minX = new JTextField();
    JTextField maxX = new JTextField();
    JTextField ref = new JTextField("Enter value 0-1");

    // declaring the layout for layout manager
    JPanel north = new JPanel();
    JPanel south = new JPanel();
    JPanel west = new JPanel();
    JPanel east = new JPanel();
    JPanel center = new JPanel();

    // declaring buttons using JButtons
    JButton calculate = new JButton("Calculate");
    JButton delete = new JButton("Delete");
    JButton refine = new JButton("Refine");

    // Calling from equation class
    // equation eq = new equation();

    private JPanel panel;
    private int width = center.getWidth();
    private int height = center.getHeight();

    @Override
    public void init() {
        // setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container c = this.getContentPane();
        this.setSize(900, 480);
        this.setVisible(true);

        // listener to buttons
        calculate.addActionListener(this);
        delete.addActionListener(this);
        refine.addActionListener(this);

        // listeer to user's input
        textA.addActionListener(this);
        textB.addActionListener(this);
        textC.addActionListener(this);
        textD.addActionListener(this);
        minX.addActionListener(this);
        maxX.addActionListener(this);
        ref.addActionListener(this);

        // assigning colours to panels to be distinguished
        north.setBackground(Color.LIGHT_GRAY);
        south.setBackground(Color.LIGHT_GRAY);
        west.setBackground(Color.YELLOW);
        east.setBackground(Color.GREEN);
        center.setBackground(Color.GRAY);

        // Declaring border
        BorderLayout layoutBorder = new BorderLayout();
        // setting up the grid (x rows, y clumns, space, space)
        GridLayout layoutGrid = new GridLayout(2, 8, 4, 4);

        // layout grid
        north.setLayout(layoutGrid);
        // set labels
        north.add(labelA);
        north.add(labelB);
        north.add(labelC);
        north.add(labelD);
        north.add(labelMinX);
        north.add(labelMaxX);
        north.add(ref);

        // calculate button
        north.add(calculate);
        // text boxes
        north.add(textA);
        north.add(textB);
        north.add(textC);
        north.add(textD);
        north.add(minX);
        north.add(maxX);
        north.add(refine);
        // delete button
        north.add(delete);

        south.add(message);

        // border layout
        c.add(north, BorderLayout.NORTH);
        c.add(south, BorderLayout.SOUTH);
        c.add(center, BorderLayout.CENTER);
        // c .add(west, BorderLayout.WEST);
        // c .add(east, BorderLayout.EAST);

        // panel = new JPanel();
        // panel.setPreferredSize(new Dimension(width, height));
        // panel.setBackground(Color.GRAY);
        // center.add(panel);

    }

    @Override
    public void actionPerformed(ActionEvent e) // throws NumberFormatException
    {
        // dafault message will be <message> -- "Please insert values"
        message.setText(e.getActionCommand());
        // when button "Delete" is pressed all values in text firlds will turn
        // null
        if (e.getActionCommand().equals("Delete")) {
            message.setForeground(Color.DARK_GRAY);

            textA.setText(null);
            textB.setText(null);
            textC.setText(null);
            textD.setText(null);
            minX.setText(null);
            maxX.setText(null);

            repaint();
        } else if (e.getActionCommand().equals("Calculate"))
            // when "Calculate" button is pressed, values will be attached to
            // equation
            try {
                message.setForeground(Color.DARK_GRAY);

                // -------------------------------------------------
                a = Integer.parseInt(textA.getText());
                b = Integer.parseInt(textB.getText());
                c = Integer.parseInt(textC.getText());
                d = Integer.parseInt(textD.getText());
                minimumX = Integer.parseInt(minX.getText());
                maximumX = Integer.parseInt(maxX.getText());

                System.out.println("center.getWidth() " + center.getWidth());
                System.out.println("center.getHeight() " + center.getHeight());
                System.out.println("minimum " + minX.getText());
                System.out.println("maximum " + maxX.getText());
                System.out.println("a " + textA.getText());
                System.out.println("b " + textB.getText());
                System.out.println("c " + textC.getText());
                System.out.println("d " + textD.getText());
                // ------------------------------------------------------

                message.setText("This is the result for " + "A "
                        + textA.getText() + ", B " + textB.getText() + ", C "
                        + textC.getText() + ", D " + textD.getText());
                draw();
            }

            catch (NumberFormatException ex)
            // if user inputs other than numbers, a warning message in the south
            // panel will show
            {
                message.setText("Please insert numerical value in "
                        + ex.getMessage());
                message.setForeground(Color.red);
                message.setFont(new Font("Tahoma", Font.BOLD, 12));
            }

        else if (e.getActionCommand().equals("Refine")) {
            // for refine
        }

    }

    // ===================================================================================

    private void calculation() {
        xCoOrdinates = new int[(maximumX - minimumX) + 1];
        yCoOrdinates = new int[(maximumX - minimumX) + 1];

        for (int i = 0; i < xCoOrdinates.length; i++)
        // for(int j = 0; j < yCoOrdinates.length; j++)

        {
            // generating the x co-ordinates and storing them in arrays
            xCoOrdinates[i] = minimumX + i;
            // generating the y co-ordinates using the formula given
            y = ((a * (int) Math.pow(i, 3)) + (b * (int) Math.pow(i, 2))
                    + (c * i) + (d));
            // storing y co-ordinates
            yCoOrdinates[i] = y;
            // displaying results
            // System.out.println("X = " + i + "   Y = " + getY());
            System.out.println("These are the values of X = " + i);
        }

        // printing the y axes values
        for (int i = 0; i < yCoOrdinates.length; i++) {

            System.out.println("this is the extracted Y " + yCoOrdinates[i]);

        }

        maximumX = xCoOrdinates[0];
        maximumX = xCoOrdinates[0];
        for (int i = 1; i < yCoOrdinates.length; i++) {
            if (yCoOrdinates[i] > maximumX)
                maximumX = xCoOrdinates[i];
            else if (yCoOrdinates[i] < minimumX)
                minimumX = xCoOrdinates[i];
        }

        System.out.println("MAXX is " + maximumX);
        System.out.println("MINX is " + minimumX);

        maximumY = yCoOrdinates[0];
        minimumY = yCoOrdinates[0];
        for (int i = 1; i < yCoOrdinates.length; i++) {
            if (yCoOrdinates[i] > maximumY)
                maximumY = yCoOrdinates[i];
            else if (yCoOrdinates[i] < minimumY)
                minimumY = yCoOrdinates[i];
        }
        System.out.println("MAXY is " + maximumY);
        System.out.println("MINY is " + minimumY);

    }

    // =================================================================================================

    public void draw() {
        Graphics g = center.getGraphics();
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, center.getWidth(), center.getHeight());
        // g.fillRect(25,25, center.getWidth()-50, center.getHeight()-50);
        double x, y, nextX, nextY;
        int xPoint; // = 0;
        int yPoint; // = 0;
        int nextXpoint; // = 0;
        int nextYpoint; // = 0;

        g.setColor(Color.BLUE);

        for (xPoint = 0; xPoint <= (double) center.getWidth(); xPoint++) {
            x = scaleX(xPoint);
            y = equation(x);
            yPoint = scaleY(y);
            nextXpoint = xPoint + 1;
            nextX = scaleX(nextXpoint);
            nextY = equation(nextX);
            nextYpoint = scaleY(nextY);
            g.drawLine(xPoint, yPoint, nextXpoint, nextYpoint);
            // System.out.println("equation  --->" + eq.getY());
        }

    }

    private double equation(double x) {
        return y;
        // return a*x*x*x + b*x*x + c*x + d;
    }

    private double scaleX(int xPoint) {
        int minXstart = minimumX;
        int maxXend = maximumX;
        double xScale = (double) center.getWidth() / (maxXend - minXstart);
        return (xPoint - (center.getWidth() / 2)) / xScale;
    }

    private int scaleY(double y) {
        int minYstart = minimumY;
        int maxYend = maximumY;
        int yCoord;
        double yScale = (double) center.getHeight() / (maxYend - minYstart);
        yCoord = (int) (-y * yScale) + (int) (center.getHeight() / 2);
        return yCoord;
    }
}

最佳答案

有第三方库可以为您完成此类工作。查看:

JFreeChart

Charts4J

关于java - 绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493357/

相关文章:

java - 如何填充有关布局管理器的剩余空间?

Java JButton和swing问题

jquery - 如何突出显示 highstock 中的某个范围?

algorithm - TSP优游

ios - 带有值和日期图表 iOS 的折线图

Java、MySQL : how to connect dependent combobox based on another one

java - 找不到模块 'react-transform-hmr/lib/index.js'

java - 尝试使用 Gmail API 发送电子邮件时出现 NoClassDefFoundError

Java - 从文件中读取

java - 如何将 JRadioButton 组与模型一起使用