java graphics颜色_在Java Graphics对象中设置背景色

在Java Graphics中,使用setColor()方法只能改变对象边框颜色,若要填充整个对象,需使用fillRect()方法。drawRect()仅绘制矩形边框,而fillRect()则填充指定矩形区域,确保颜色填满内部。了解这些区别有助于正确地为图形对象设置背景颜色。
摘要由CSDN通过智能技术生成

美好的一天,

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

void draw(Graphics g)

{

g.setColor(color);

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

}

好心提醒.

解决方法:

使用fillRect()方法.

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

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()将颜色设置为border.

标签:background,graphics,colors,border,java

来源: https://codeday.me/bug/20191028/1948747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值