acm.program.GraphicsProgram,无法获得合适尺寸的画布

时间:2012-01-16 07:38:36

标签: java

这不是作业问题。我只是从斯坦福大学免费参加课程。我在Eclipse中使用Ubuntu Linux。

问题和疑问: 我通过在acm.program.GraphicsProgram对象上调用add()来绘制矩形。我正在绘制一定数量的具有一定固定宽度的矩形。但是我发现我的矩形正在流出可见区域。我已经尝试为GraphicsProgram对象和GCanvas对象设置足够大的宽度和高度,但我的矩形仍然从可见区域掉落。无论我设置什么高度,我总是为GraphicsProgram对象获得相同的高度。关于我做错了什么的指示?

    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;

    public class Pyramid extends GraphicsProgram {

    /** Width of each brick in pixels */
            private static final int BRICK_WIDTH = 30;

    /** Width of each brick in pixels */
            private static final int BRICK_HEIGHT = 12;

    /** Number of bricks in the base of the pyramid */
            private static final int BRICKS_IN_BASE = 14;

            public void run() {

                    setWindowSize();
                    this.createPyramid();
            }

            private void createPyramid()
            {
                    int centerX = findCenter();
                    int startingX = centerX - (BRICKS_IN_BASE / 2) * BRICK_WIDTH;
                    int startingY = BRICK_HEIGHT;

                    for(int numBricks = BRICKS_IN_BASE; numBricks>= 1; numBricks--)
                    {
                            this.layBricks(startingX,startingY , numBricks);
                            startingX = startingX + BRICK_WIDTH / 2;
                            startingY = (BRICKS_IN_BASE - numBricks + 2) * BRICK_HEIGHT;
                    }
            }

            private void layBricks(int x, int y, int numOfBricks)
            {
                    for(int i = 0; i < numOfBricks; i++)
                    {
                            add(new GRect(x,y,this.BRICK_WIDTH, this.BRICK_HEIGHT));
                            x+=this.BRICK_WIDTH;
                    }

            }
            private void setWindowSize()
            {
                    int width = BRICK_WIDTH * BRICKS_IN_BASE * 2;
                    int height = BRICKS_IN_BASE * BRICK_HEIGHT * 2;

                    this.setSize(width, height);
                    //this.setForeground(Color.GREEN);
                    //this.setBackground(Color.BLUE);
                    //this.getGCanvas().setBounds(0, 0, width, height);
                    //this.getGCanvas().add(new GRect(0,0,300,30));
                    //this.getGCanvas().setBackground(Color.WHITE);
                    System.out.println(this.getHeight());
                    System.out.println(this.getWidth());
                    System.out.println(this.getGCanvas().getHeight());
                    System.out.println(this.getGCanvas().getWidth());
            }


            private int findCenter()
            {

                    return this.getWidth() / 2;

            }


    }

3 个答案:

答案 0 :(得分:4)

我正在网上完成相同的斯坦福课程并遇到同样的问题。 setSize方法将调整显示大小,但不会调整getWidth和getHeight返回的值。

您可以转到Project&gt;更改宽度和高度。属性&gt;运行/调试设置&gt;编辑&gt;参数选项卡。

我认为有一些更直接或基于代码的东西,但这是一个简单的解决方案。

答案 1 :(得分:1)

首先不要硬编码像这样的砖块

        int brick_width = (getWidth() / BRICKS_IN_BASE) - (getWidth() / 50);
        int brick_height = (brick_width / 3);

这样,无论屏幕尺寸如何,您的金字塔都会被绘制出来。 它也应该居中,一旦您的砖块大小相对于窗口大小,保证砖块始终居中变得更容易。我发现的唯一扳手很奇怪 窗口大小,如50 x 500,但这种情况不会经常发生。

以下是我的解决方案

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class Pyramid1 extends GraphicsProgram {



 public void run(){

    int brick_width = (getWidth() / BRICKS_IN_BASE) - (getWidth() / 50);
    int brick_height = (brick_width / 3);
    for(int n = 0; n < BRICKS_IN_BASE; n++ ){
        //make a row at level n of bricks that is BRICKS_IN_BASE - n bricks wide.
        int bricks_in_level = BRICKS_IN_BASE - n;
        int x = ((getWidth() / 2) - ((bricks_in_level * brick_width) / 2)); //find the center then offset to farthest left.
        int y = (((getHeight() / 2) + ((BRICKS_IN_BASE / 2) * brick_height )) - ((n + 1) * brick_height)); //start at the 1/2 and move down half max stack height and move up a brick each round.
        GRect brick = new GRect(x, y, brick_width, brick_height);
        brick.setFilled(true);
        brick.setFillColor(Color.RED);
        add(brick);

        if(bricks_in_level > 1){ //If there are 2 or more bricks needed in this level
            for(int needed_bricks = bricks_in_level - 1; needed_bricks > 0; needed_bricks -= 1){
                x += brick_width;
                GRect needed_brick = new GRect(x, y, brick_width, brick_height);
                needed_brick.setFilled(true);
                needed_brick.setFillColor(Color.RED);
                add(needed_brick);
            }
        }
    }

 }
private static final int BRICKS_IN_BASE = 12;
}

答案 2 :(得分:0)

主要问题是我使用的是open-java-jdk而不是sun-java-jdk。更改jre后,我的Applet以更可预测的方式运行。