2014-05-07 32 views
0

我有一個擴展jpanel的主類,和一個使用圖形參數的內部類Brick在屏幕上繪製矩形,我試圖使這些矩形在面板大小更改時可調整大小 這些Bricks在paintComponent方法中繪製()的寬度和高度也分配在 相同的方法中,我調用paintComponent()每30millis 磚的寬度是面板寬度的百分比,所以我試圖將舊的磚寬度保存在一個變量,並增加x磚的座標由舊磚和新磚寬度之間的差異,但它不會工作如何調整重繪的JComponent?

我有以下代碼:

int width,height,brickHeight,brickWidth,tempBWidth,diffrence; 
ArrayList<Brick> listOfBricks = new ArrayList<Panel.Brick>(); 
Timer timer = new Timer(30,this); 
boolean bricksFilled,resized; 

public Panel() { 
    setBackground(Color.BLACK); 
    setVisible(true); 
    timer.start(); 
} //end constructor Panel(). 

protected void paintComponent(Graphics g) { 
    width = getWidth(); height = getHeight(); 
    setSize(width,height); 
    tempBWidth = brickWidth; 
    brickHeight = height/20; brickWidth = width/10; 
    if(width != 0 && tempBWidth != brickWidth) { diffrence = brickWidth - tempBWidth; } 
    super.paintComponent(g); 
    if(bricksFilled == false) { fillListOfBricks(); } 
    drawBricks(listOfBricks, g); 
    diffrence = 0; 
} //end method paintComponent(). 

public void actionPerformed(ActionEvent event) { 
    repaint(); 
} //end method actionPerformed(). 

void fillListOfBricks() { 
    for (int row = 0;row < height/2;row += brickHeight+1) { 
     for (int column = 0;column < width;column += brickWidth+1) { 
      if(brickWidth != 0){ 
       listOfBricks.add(new Brick(column,row,true,brickWidth,brickHeight)); 
       bricksFilled = true; 
      } //end if. 
     } //end inner loop. 
    } //end outer loop. 
} //end method fillListOfBricks(). 

void drawBricks(ArrayList<Brick> listOfBricks,Graphics g) { 
    for (Brick brick:listOfBricks) { 
     if(listOfBricks.isEmpty() == false) { 
      if (brick.visible) { 
        brick.draw(g); 
      } //end inner if. 
     } //end outer if. 
    } //end loop. 
} //end method drawBricks(). 

class Brick { 
    int x,y; 
    int width,height,xDiff,yDiff; 
    boolean visible; 
    Color randomColor; 
    Brick(int x,int y,boolean visible,int width,int height) { 
     this.x = x; this.y = y; this.visible = visible; 
     this.width = width; this.height = height; 
     this.randomColor = getRandomColor(); 
    } //end constructor Brick(). 

    void draw(Graphics g) { 
     if(this.visible) { 
      System.out.println(diffrence); 
      g.setColor(this.randomColor); 
      System.out.println(x+"    "+diffrence); 
      g.fillRect(x+diffrence, y, brickWidth,brickHeight); 
      g.setColor(Color.WHITE); 
      g.drawRect(x-1, y-1, brickWidth+1, brickHeight+1); 
     } // end if block. 
    } //end method draw(). 

    Color getRandomColor() { 
     int R = (int)(255*(Math.random())); 
     int G = (int)(255*(Math.random())); 
     int B = (int)(255*(Math.random())); 
     return new Color(R,G,B); 
    } //end Method getRandomColor. 
     } 

}

感謝您的幫助

+1

'的setSize(寬度,高度)樣本代碼;'在'paintComponent'方法 - 從未設置paint方法 –

+1

此外內部組件狀態,您的要求是不清楚。請嘗試添加更具描述性/可理解的問題描述,同時指出問題出現的位置 –

+0

希望它現在更好 – user3612007

回答

0

「我試圖讓這些矩形調整大小時,面板尺寸變化」

傳遞面板作爲參數傳遞給Brick構造函數,然後使用面板的getWidth()getHeight()方法繪製磚塊。例如

class Brick { 
    private JComponent panel; 

    public Brick(JComponent panel, ....) { 
     this.panel = panel; 
    } 

    public void draw(Graphics g) { 
     int height = (int)(panel.getHeight()/BRICK_ROWS); 
     int width = (int)(panel.getWidth()/BRICK_COLS); 
     g.fillRect(x, y, width, height); 
    } 
} 

根據電流大小的面板getWidth()getHeight()意志大小的磚。您可以通過將面板大小除以每列/每行的磚塊數量來確定大小。你可能不得不重構你的代碼。現在,這是假設所有的磚塊在狀結構

網格也不要設置組件狀態paintComponent方法內。如果你想(某種奇怪的原因)想要調整面板的大小,請使用定時器方法。

+0

感謝您的回答,非常感謝 – user3612007

+0

我也想知道我的方法有什麼問題,爲什麼它不起作用? – user3612007

+0

我也想改變每個磚塊的x座標 – user3612007

0

將其移交給佈局管理器以管理組件的位置和大小。請勿手動設置。

GridLayout

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Maze { 
    private JFrame frame = null; 

    private Color[] colors = new Color[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }; 

    public Maze(int length) { 

     frame = new JFrame(); 

     JPanel panel = new JPanel(new GridLayout(length, length, 5, 5));   

     for (int i = 0; i < length; i++) { 
      for (int j = 0; j < length; j++) { 
       JPanel p2 = new JPanel(); 
       p2.setBackground(colors[(int) (Math.random() * colors.length)]); 

       panel.add(p2); 
      } 
     } 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Maze Game"); 
     frame.setContentPane(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

} 

enter image description here

相關問題