如何在 Java 中绘制一个正好位于小程序窗口中心的实心方框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2173055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a filled square box in Java that is exactly in the center of an applet window?
提问by Tom
How do you draw a filled square box in Java that is exactly in the center of an applet window? and when resizing the window, it is centered horizontally and vertically within the applet window? I want it to adapt to the vertical height of the screen but stay square even as the horizontal width edges. If the window is resized to be too narrow, then the sides might cut off?
如何在 Java 中绘制一个正好位于小程序窗口中心的实心方框?当调整窗口大小时,它在小程序窗口内水平和垂直居中?我希望它适应屏幕的垂直高度,但即使水平宽度边缘也保持方形。如果窗口调整得太窄,那么侧面可能会被切断?
回答by Kylar
Here's an example of a panel that will either make a 30px square in the middle, or resize with the panel. Perhaps it can give you enough to make progress.
这是一个面板示例,该面板将在中间制作一个 30 像素的正方形,或者随面板调整大小。也许它可以给你足够的进步。
private class MyPanel extends JPanel{
int height = 30;//30 pixels high.
int width = 30;//30 pixels wide.
boolean resize = true;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int verticalCenter = this.getHeight()/2;
int horizontalCenter = this.getWidth()/2;
if(!resize){
int topLeftSquareCornerY = verticalCenter - (height/2);
int topLeftSquareCornerX = horizontalCenter - (width/2);
g.setColor(Color.BLUE);
g.drawRect(topLeftSquareCornerX, topLeftSquareCornerY, width, height);
}else{
g.setColor(Color.GREEN);
g.drawRect(15,15,(this.getWidth()-30), this.getHeight()-30);
}
}
}
回答by Thorbj?rn Ravn Andersen
Sounds like your basic problem is figuring out how to place a given rectangle correctly. You need to have the middleof the screen in the middleof the rectangle.
听起来您的基本问题是弄清楚如何正确放置给定的矩形。你需要有中间的屏幕在中间的矩形。
The distance from the middle of the rectangle to its sides are half the height and length respectively.
长方形的中间到两边的距离分别是高和长的一半。
So x1, x2 = middle_x?± width/2, and y1, y2 = middle_y ± height/2.
所以 x1, x2 = middle_x?± width/2, y1, y2 = middle_y ± height/2。
回答by trashgod
I'm guessing you want to draw a square with a fixed size
that stays in the center
of the panel as the panel is resized. One approach to such problems is start from the end and work backward. You know about fillRect()
, so write down what you need to "fill on the blanks" required by that method. Call the center
coordinates x
and y
. The top corner would be half of size
up, and the left corner would be half of size
to the left; the square's width and height would be just size
:
我猜你想画一个正方形,当面板调整大小时size
,它会留在center
面板的 。解决此类问题的一种方法是从头开始并向后工作。您了解fillRect()
,因此请写下该方法所需的“填空”所需的内容。调用center
坐标x
和y
。顶角size
向上一半,左角向左一半size
;正方形的宽度和高度仅为size
:
g.fillRect(left, top, width, height);
g.fillRect(x - size/2, y - size/2, size, size);
Now go back and figure out that x
and y
are half the panel's width and height, respectively:
现在返回并计算出x
和y
分别是面板宽度和高度的一半:
int x = getWidth() / 2;
int y = getHeight() / 2;
Now put it all together in your paintComponent()
method.
现在把它们放在你的paintComponent()
方法中。