【发布时间】:2014-01-17 10:49:41
【问题描述】:
我写了一个程序,希望通过 getWidth() 和 getHeight() 方法显示在屏幕中间,但它却出现在左上角。所以我的问题是 getWidth() 和 getHeight() 方法不是应该获取整个屏幕的宽度和高度吗?
/*
* This program displays Pascal's Triangle for 8 rows.
*/
import acm.graphics.*;
import acm.program.*;
public class combination extends GraphicsProgram {
public void run() {
for(int i = 0; i < NROWS; i++) {
for (int n = 0; n <= i; n++) {
add(new GLabel(Combination(i,n), x(i,n), y(i))); //the coordination and the amount of labels are related to i;
}
}
}
//method that adds labels on the screen.//
private String Combination(int i, int n) { //this method returns a string which would be added to the screen.//
return (String.valueOf(i) +","+ String.valueOf(n));
}
//method that set the y coordination for the label//
private double y(int i) {
double Height = getHeight()/NROWS;
return i * Height + 9;
}
//method that set the x coordination for the label//
private double x(int i, int n) {
double Orgnx = getWidth() / 2;
double HalfBase = getHeight() / NROWS / Math.sqrt(3);
double Base = HalfBase * 2;
return Orgnx - i * HalfBase + n * Base;
}
/*The program displays 8 rows of pairs of number.*/
private static final int NROWS = 8;
【问题讨论】:
-
什么是
GraphicsProgram、getHeight、getWidth?这些不是标准的 Java 类/方法...
标签: java