2013-06-02 37 views
-5

試圖使用paintComponent創建一個公牛眼睛程序。我想要矩形在屏幕的中心。我怎麼做?中心在Java框架/面板中對齊矩形

+5

這是不是把一個地方你在那裏工作。請給出你嘗試過的一些代碼。 – ncm

+1

我已經問了幾天前類似這樣的問題有一個強大的解決方案回答http://stackoverflow.com/questions/16637633/aligning-shapes-center-to-jpanels-center – pinkpanther

回答

3

基本上,你畫你想圍繞組件的中心,在此基礎上什麼都的寬度和高度

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    int width = getWidth() - 1; 
    int height = getHeight() - 2; 
    g.drawLine(width/2, 0, width/2, height); 
    g.drawLine(0, height/2, width, height/2); 
} 

退房Performing Custom Painting2D Graphics的細節

+0

我想現在真正的問題是,爲什麼投票?因爲我沒有繪製矩形?不能爲所有人做所有事情,特別是當對問題 – MadProgrammer

+1

+1沒有付出努力甚至回答這樣的問題時。 :-) – The111