I wrote a program, expecting it to be displayed in the middle of the screen by using getWidth() and getHeight() methods, but, instead, it appears on the upper left corner. So my question is aren't the getWidth() and getHeight() methods supposed to get the width and height of the whole screen?
/*
* 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;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…