2013-06-02 36 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