gpt4 book ai didi

java - acm.program.GraphicsProgram,无法获得适当大小的 Canvas

转载 作者:行者123 更新时间:2023-11-29 08:09:42 25 4
gpt4 key购买 nike

这不是作业题。我只是复习斯坦福大学的一门免费类(class)。我正在使用带有 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;

}


}

最佳答案

我正在在线学习同一门斯坦福类(class),但遇到了同样的问题。 setSize 方法会调整显示大小,但不会调整 getWidth 和 getHeight 返回的值。

您可以通过转到“项目”>“属性”>“运行/调试设置”>“编辑”>“参数”选项卡来更改宽度和高度。

我假设有更直接或基于代码的方法,但这是一个简单的解决方案。

关于java - acm.program.GraphicsProgram,无法获得适当大小的 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com