使用JPanel绘制x,y坐标系并绘制方程图

时间:2014-09-13 16:26:50

标签: java swing jpanel paintcomponent

我的任务是使用JPanel绘制坐标系。然后,我需要使用线段绘制给定方程的图形。要求为:0.0< = x< = 1.0 - y轴上有10个哈希标记和4个哈希标记 - -0.1,-0.5,0.5,1.0

这是我第一次使用Java编程,而且我在使用draw line方法时遇到了问题。我想以一种对图形有帮助的方式绘制坐标系。我知道一旦我正确地绘制了图形,我将能够使用循环来绘制方程图。

这是我到目前为止所拥有的。

public class Curve extends JPanel
{

    // initialize variables 
    private int choice; // users choice which function to pick 
    private int lines; // number of lines for curve 

    public Curve(int choice, int lines)
    {
        this.choice = choice; // assign choice in instance variable choice
        //this.x = x; // assigns x instance variable x 
        this.lines = lines; // assigns lines in instance variable lines
    } // end Curve constructor 

    // method to draw x,y axis and ticks
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int x = getWidth()/2;
        int y = getHeight()/2;
        int tic = getWidth() / 100;
        g.translate(x, y);
        g.setColor (Color.black);
        //g.drawLine (200, 50, 50, 50);


        g.drawLine (0, 0, x, 0);
        g.drawLine (0, -y+50, 0, y-50);

        for (int k = -x/7; k <= x; k += 33)
            g.drawLine (k, tic, k, -tic);

        // draw the tic marks along the y axis
        for (int k = -y+20; k <= y-20; k += 50)
          g.drawLine (-tic , k, tic, k);
    }



    // method to pick function based on user's choice
    public void draw()
    {
        switch ( choice ) // takes in user's choice and plugs into function
        // question 
        {
            case 1: // function 1: y = 1.0 - x: x = int1 
                break;
            case 2: 
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;

        } // end switch statement
    } // end method
} // end class Curve`

0 个答案:

没有答案