2012-01-16 26 views
1

這不是一個家庭作業問題。我只是從斯坦福大學免費提供的課程。我正在使用Ubuntu Linux和Eclipse。acm.program.GraphicsProgram,無法獲得適當尺寸的畫布

問題和問題: 我通過調用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; 

      } 


    } 

回答

0

主要問題是我使用open-java-jdk而不是sun-java-jdk。在更改jre後,我的Applet以更可預測的方式運行。

4

我正在通過相同的斯坦福大學在線課程並遇到同樣的問題。 setSize方法將調整顯示大小,但不會調整getWidth和getHeight返回的值。

您可以通過轉到項目>屬性>運行/調試設置>編輯>參數選項卡來更改寬度和高度。

我假設有更直接或基於代碼的東西,但這是一個簡單的解決方案。

+0

這工作..但有一種方法來設置默認? – RubyDev 2012-09-22 05:30:14

1

開始通過不硬編碼的磚尺寸這樣

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

這樣,你的金字塔總是會內任何屏幕尺寸恰好是繪製。 它也應該居中,一旦你的磚塊尺寸與窗戶大小相關,保證磚塊始終居中變得更容易。我發現的唯一扳手是很奇怪的,例如50×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; 
}