0

所以我无法将框架居中以使我的整个图表都在屏幕上。目前,当我运行我的程序时,坐标系上的 x 值是正确的范围,但我几乎看不到我的图形,因为框架位于图形绘制位置上方数百像素的中心。这是我的代码:

public static int startx = 1;
public static int endx = 500;
static Scanner input = new Scanner(System.in);

int WIDTH = 0;  
int HEIGHT = 0;


public GraphSigma(int WIDTH, int HEIGHT) {
    this.WIDTH = WIDTH;
    this.HEIGHT = HEIGHT;
}

// Draw your spiral here!!!
// You'll need to use a FOR loop to calculate the
// points on the spiral


public void paintSigma(Graphics g){

    int prevX=startx;
    int prevY=Sigma.Sigma(startx);
     Graphics2D g2 = (Graphics2D) g;
    g2.translate(0, Sigma.Sigma(endx));
    g2.scale(1,-1);
    if (endx-startx < 50)
        for (int i = startx; i <= endx; i++) {
            int x = i;
            int y = Sigma.Sigma(i);
            g.drawLine(0+prevX, 0+prevY, x, y);
            prevX=x;
            prevY=y;

            };
    if (51 < endx-startx && endx-startx < 300)
        for (int i = startx; i <= endx; i+=2) {
        int x = i;
        int y = Sigma.Sigma(i);
        g.drawLine(0+prevX, 0+prevY, x, y);
        prevX=x;
        prevY=y;

        };
        if (301 < endx-startx  && endx-startx < 1000)
        for (int i = startx; i <= endx; i+=4) {
            int x = i;
            int y = Sigma.Sigma(i);
            g.drawLine(0+prevX, 0+prevY, x, y);
            prevX=x;
            prevY=y;

        };
        if (1000<endx-startx)
            for (int i = startx; i <= endx; i+=10) {
                int x = i;
                int y = Sigma.Sigma(i);
                g.drawLine(0+prevX, 0+prevY, x, y);
                prevX=x;
                prevY=y;

                };

}

// This special method is automatically called when the scene needs to be drawn.
public void paintComponent(Graphics g) {

    paintSigma(g);
}

public static void main(String[] args) {
    // Graphics window size

    int WINDOW_WIDTH = endx-startx+20;
    int WINDOW_HEIGHT = endx;

    JFrame frame = new JFrame();

    frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    frame.setTitle("Sigma Function");

    GraphSigma d = new GraphSigma(endx-startx+20, Sigma.Sigma(endx));
    frame.add(d);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

我调用的 sigma 方法在不同的类中,这里是它的代码: public class Sigma {

static Scanner input = new Scanner(System.in);

public static int Sigma(int s){
    int a = 0;
    for(int i=1;i<=s;i++){
        if(s%i==0)
            a = a + i;

    }
    return a;

}
public static void main(String[] args) {
    System.out.println("Please enter the number you want to perform the sigma function on");
    int s = input.nextInt();
    System.out.print(Sigma.Sigma(s) +" is the sum of all the divisors of your input" ); 
    }

}

老实说,我不知道如何正确地将其居中,我是否缺少方法?我一直在弄乱变量,它们似乎并没有改变框架的居中方式。非常感谢!

4

1 回答 1

0

首先,让我们清理你的代码,然后我会告诉你哪里出错了。

西格玛班

这是我的 Sigma 课程版本。

1)您没有将方法命名为与类名相同的名称。我将您的方法名称更改为 calculateSigma。

2)当您打开扫描仪时,完成后将其关闭。

3)永远不要像你那样在类中定义和打开扫描仪变量。尤其是调用 calculateSigma 方法的次数。

import java.util.Scanner;

public class Sigma {

    public static int calculateSigma(int s) {
        int a = 0;
        for (int i = 1; i <= s; i++) {
            if (s % i == 0)
                a = a + i;
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner input   = new Scanner(System.in);
        System.out.println("Please enter the number you want " +
                "to perform the sigma function on");
        int s = input.nextInt();
        System.out.print(Sigma.calculateSigma(s)
                + " is the sum of all the divisors of your input");
        input.close();
    }

}

GraphSigma 类

这是我的 GraphSigma 类版本。

您总是在事件调度线程 (EDT) 上调用 Swing 应用程序。这可以防止出现问题。

一开始看不到绘图的问题,所以我为drawLine方法添加了一个调试显示方法。这向我表明您正在绘制 (1,1) 和 (497,576) 之间的线。

有了这些信息,我专注于绘图代码中的 translate 和 scale 方法。

translate 方法移动图形的原点。

scale 方法通过将计算的坐标乘以一个因子来将计算的坐标转换为图形坐标,以获得图形坐标。

我为解决这个问题所做的第一件事是更改我用来调用 translate 方法的数字,以将图形的原点移动到 JPanel 的左下角。

我为解决这个问题所做的第二件事是找出比例因子,以便图形适合 JPanel。调试信息让我确信我的比例因子是正确的。

我将调试代码留在了类中,以便您(和其他人)可以看到一种调试 Java 类的方法并自己找出问题所在。

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GraphSigma extends JPanel {

    public static boolean   DEBUG   = false;

    public static int       startx  = 1;
    public static int       endx    = 500;
    static Scanner          input   = new Scanner(System.in);

    int                     WIDTH   = 0;
    int                     HEIGHT  = 0;

    public GraphSigma(int WIDTH, int HEIGHT) {
        this.WIDTH = WIDTH;
        this.HEIGHT = HEIGHT;
    }

    // Draw your spiral here!!!
    // You'll need to use a FOR loop to calculate the
    // points on the spiral

    public void paintSigma(Graphics g) {

        int prevX = startx;
        int prevY = Sigma.calculateSigma(startx);
        Graphics2D g2 = (Graphics2D) g;
        if (DEBUG) {
            System.out.println("Sigma of endx: " 
                    + Sigma.calculateSigma(endx));
        }
        g2.translate(0, this.getHeight());
        g2.scale(1, -((double) this.getHeight()) / 
                Sigma.calculateSigma(endx));
        if (endx - startx < 50)
            for (int i = startx; i <= endx; i++) {
                int x = i;
                int y = Sigma.calculateSigma(i);
                if (DEBUG) {
                    debugOutput(0 + prevX, 0 + prevY, x, y);
                }
                g.drawLine(0 + prevX, 0 + prevY, x, y);
                prevX = x;
                prevY = y;

            }
        ;
        if (51 < endx - startx && endx - startx < 300)
            for (int i = startx; i <= endx; i += 2) {
                int x = i;
                int y = Sigma.calculateSigma(i);
                if (DEBUG) {
                    debugOutput(0 + prevX, 0 + prevY, x, y);
                }
                g.drawLine(0 + prevX, 0 + prevY, x, y);
                prevX = x;
                prevY = y;

            }
        ;
        if (301 < endx - startx && endx - startx < 1000)
            for (int i = startx; i <= endx; i += 4) {
                int x = i;
                int y = Sigma.calculateSigma(i);
                if (DEBUG) {
                    debugOutput(0 + prevX, 0 + prevY, x, y);
                }
                g.drawLine(0 + prevX, 0 + prevY, x, y);
                prevX = x;
                prevY = y;

            }
        ;
        if (1000 < endx - startx)
            for (int i = startx; i <= endx; i += 10) {
                int x = i;
                int y = Sigma.calculateSigma(i);
                if (DEBUG) {
                    debugOutput(0 + prevX, 0 + prevY, x, y);
                }
                g.drawLine(0 + prevX, 0 + prevY, x, y);
                prevX = x;
                prevY = y;

            }
        ;

    }

    private void debugOutput(int x1, int y1, int x2, int y2) {
        System.out.println("Line drawn from (" + x1 + "," + y1 + ") to (" + x2
                + "," + y2 + ").");
    }

    // This special method is automatically called when the scene needs to be
    // drawn.
    @Override
    public void paintComponent(Graphics g) {
        paintSigma(g);
    }

    public static void main(String[] args) {
        // Graphics window size
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createGUI();
            }
        });
    }

    private static void createGUI() {
        int WINDOW_WIDTH = endx - startx + 20;
        int WINDOW_HEIGHT = endx;

        JFrame frame = new JFrame();

        frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        frame.setTitle("Sigma Function");

        GraphSigma d = new GraphSigma(endx - startx + 20,
                Sigma.calculateSigma(endx));
        frame.add(d);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
于 2013-04-08T16:34:39.363 回答