绘图功能,JFrame错误的结果

时间:2015-01-22 14:05:59

标签: java swing function jframe drawing

我有一个问题,我试图构建一个可视化函数的代码。

package main;

import java.awt.*;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MyPanel extends JPanel{

    private static final long serialVersionUID = -6649271997955423098L;

    @Override
    public void paintComponent(Graphics g) 
    {
        //super.paintComponents(g); only show code how it works 
        //g.setColor(Color.cyan);
        //g.fillRect(10, 20, 35, 78);

        paintStuff(g);
        paintBackgroundComponents(g); // the navigation cross in the background 
        paintFunctions(g); //Both
        leftPaintLineFunction(g); //with test Functions 
        rightPaintLineFunction(g);
    }

    /**
     * Own Method for drawing lines. Needs Graphics g!
     * @param g
     */
    private void paintStuff(Graphics g) 
    {
        g.setColor(Color.black);
        g.drawString("Integral Veranschaulichung", (Main.length)-(Main.length/3),((Main.width) - (Main.width/12)));
    }

    private void paintBackgroundComponents(Graphics g)
    {
        g.drawLine((Main.length)/2, Main.width, (Main.length)/2, 0);
        g.drawLine(0, (Main.width)/2, Main.length, (Main.width)/2);  

        g.setColor(Color.red);
        g.drawString("X", (Main.length-(Main.length/8)) , ((Main.width/2) - (Main.width/80)));
        g.drawString("Y", ((Main.length/2)+(Main.length/80)) , ((Main.width) - (Main.width/8)));
    }

    private void paintFunctions(Graphics g)
    { 
         g.setColor(Color.blue);
         for(int x=(0 - Main.length); x < Main.length; x++) //fills all possible values (limited through length)
         {
            g.drawOval((x) + (Main.length/2-1), Functions.solveTestFunction(x)+(Main.width/2-1), 3, 3);
         }
    }

   //needs different methods for left and right Side    
    private void leftPaintLineFunction(Graphics g)
    {
        int [] pointOneX = new int [Main.length*2];
        int [] pointOneY = new int [Main.length*2];

        for(int x = 0; x < Main.length; x++)
        {
                pointOneX[x] = (((x)*(-1)) + (Main.length/2-1));
                pointOneY[x]= (Functions.solveTestFunction(x) + (Main.width/2-1));  
                System.out.print(pointOneX[x]+" ");
                System.out.print(pointOneY[x]);
        }
        g.drawPolyline(pointOneX, pointOneY, 100);
    }
    private void rightPaintLineFunction(Graphics g)
    {
        int [] pointOneX = new int [Main.length*2];
        int [] pointOneY = new int [Main.length*2];

        for(int x = 0; x < Main.length; x++)
        {
                pointOneX[x] = ((x) + (Main.length/2+2)); //no clue why 2? 
                pointOneY[x]= (Functions.solveTestFunction(x) + (Main.width/2));    
                System.out.print(pointOneX[x]+" ");
                System.out.print(pointOneY[x]);
        }
        g.drawPolyline(pointOneX, pointOneY, 100);
    }
}

我的另一课是:

package main;

public class Functions 
{

    int x = 0;

    public static int solveTestFunction(int x) //simpel test function 
    {
        int y=0;

        y = (x^2)*(-1);//-1 cause the gui uses changed sides 

        return y;
    }
}

我的主要方法就是这个:

package main;

import javax.swing.JFrame;

public class Main {
    static int length = 1000; // Playgrounds measures 
    static int width = 1000;

    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("My Drawings");
        MyPanel panel = new MyPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(length, width); //playground
        frame.add(panel);
        frame.setVisible(true);
    }
}

我左右分割方法rightpaintLineFUnctions()。因为这是我让他们工作的唯一方法。当我为我的testFunction尝试x * x时,一切工作都很好。但是,由于我用这种形式写了它^ ^ 2它没有正确的工作。一些帮助会非常好。

0 个答案:

没有答案