gpt4 book ai didi

java - 在java Graphics对象中设置背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:40 27 4
gpt4 key购买 nike

你好,

知道在Java Graphics 对象中,我们可以使用setColor() 方法来设置对象的颜色。但这仅适用于对象边框。无论如何要为整个对象设置颜色?我的意思是 Graphics 对象的背景。

void draw(Graphics g)
{
g.setColor(color);
g.drawRect(left, right, width, height);

}

请指教。

最佳答案

使用 fillRect() 方法。

 g.fillRect(left, right, width, height);

来自 javadoc


drawRect()

Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.

fillRect()

Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.


这仅适用于对象边框”,因为drawRect 仅绘制轮廓。“无论如何都要为整个对象设置颜色吗?“好吧,你误会了。和 setColor() 将颜色设置为您绘制的颜色 如果您绘制轮廓,那么您只能看到轮廓,这不是因为 setColor() 将颜色设置为边框

关于java - 在java Graphics对象中设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32100567/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com