You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java Swing中为fillRect或fillOval添加模糊效果?

给矩形/椭圆添加外侧模糊效果的实现方案

下面分几种常见场景给出具体实现方法:

Web前端(CSS)

用filter: blur()配合伪元素是最简便的方式,能快速实现图形外侧的扩散模糊:

<div class="blur-oval"></div>
/* 矩形的话去掉border-radius: 50%即可 */
.blur-oval {
  position: relative;
  width: 200px;
  height: 120px;
  background: #4285f4;
  border-radius: 50%; /* 椭圆/圆形设置,矩形可省略或设为固定值 */
}

.blur-oval::before {
  content: "";
  position: absolute;
  top: -12px;
  left: -12px;
  right: -12px;
  bottom: -12px;
  background: inherit;
  border-radius: inherit;
  filter: blur(18px);
  z-index: -1;
}

调整blur()的数值控制模糊强度,修改伪元素的top/left/right/bottom值可以调整模糊的扩散范围。

Python批量处理(PIL/Pillow)

如果需要批量生成带外侧模糊的图形,用PIL库可以实现:

from PIL import Image, ImageDraw, ImageFilter

# 画布尺寸
canvas_w, canvas_h = 350, 250
# 目标图形(矩形):左、上、右、下坐标
shape_coords = (60, 50, 290, 200)
blur_radius = 16
expand = blur_radius * 2

# 创建透明画布
base_img = Image.new("RGBA", (canvas_w, canvas_h), (255, 255, 255, 0))
draw = ImageDraw.Draw(base_img)

# 绘制模糊底层(放大版图形)
blur_layer = Image.new("RGBA", (canvas_w, canvas_h), (255, 255, 255, 0))
blur_draw = ImageDraw.Draw(blur_layer)
# 椭圆的话把rounded_rectangle换成ellipse
blur_draw.rounded_rectangle(
    (shape_coords[0]-expand, shape_coords[1]-expand,
     shape_coords[2]+expand, shape_coords[3]+expand),
    radius=25,
    fill=(66, 133, 244, 255)
)
blur_layer = blur_layer.filter(ImageFilter.GaussianBlur(blur_radius))

# 绘制清晰的上层图形
draw.rounded_rectangle(shape_coords, radius=25, fill=(66, 133, 244, 255))

# 合并图层
final_img = Image.alpha_composite(blur_layer, base_img)
final_img.save("blur_rect.png")

Photoshop可视化设计

如果是做设计图,用Photoshop的操作步骤如下:

  • 绘制目标矩形/椭圆,选中该图层,按Ctrl+J复制图层
  • 将复制的图层拖到原图层下方
  • 选中下方的模糊图层,执行「滤镜 > 模糊 > 高斯模糊」,调整半径参数
  • 按Ctrl+T启动自由变换,适当放大模糊图层,让模糊效果覆盖原图形外侧
  • 若需要柔化边缘,可给模糊图层添加图层蒙版,用渐变工具拉取黑白渐变弱化边缘

内容的提问来源于stack exchange,提问作者ShizamDaGeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 16:32:35