java 画四周阴影,如何在底图中为单个多边形添加点画或阴影?

如果 shape 是 .shp 文件的形状,则可以将其提供给 matplotlib.patches.Polygon 并使用 hatch 参数添加一些剖面线 .

p= Polygon(np.array(shape), fill=False, hatch="X")

plt.gca().add_artist(p)

一个完整的例子:

from mpl_toolkits.basemap import Basemap

import matplotlib.pyplot as plt

from matplotlib.patches import Polygon

import numpy as np

m = Basemap(llcrnrlon=-10,llcrnrlat=35,urcrnrlon=35,urcrnrlat=60.,

resolution='i', projection='tmerc', lat_0 = 48.9, lon_0 = 15.3)

m.drawcoastlines()

# shape file from

# http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/

fn = r"ne_10m_admin_0_countries\ne_10m_admin_0_countries"

m.readshapefile(fn, 'shf', drawbounds = False)

# here, 'shf' is the name we later use to access the shapes.

#Madrid

x,y = m([-3.703889],[40.4125])

m.plot(x,y, marker="o", color="k", label="Madrid", ls="")

hatches = ["\\\\","++", "XX"]

countries = ['Spain', 'Ireland', "Belgium"]

hatchdic = dict(zip(countries, hatches))

shapes = {}

for info, shape in zip(m.shf_info, m.shf):

if info['NAME'] in countries:

p= Polygon(np.array(shape), fill=False, hatch=hatchdic[info['NAME']])

shapes.update({info['NAME'] : p})

for country in countries:

plt.gca().add_artist(shapes[country])

handles, labels = plt.gca().get_legend_handles_labels()

handles.extend([shapes[c] for c in countries])

labels.extend(countries)

plt.legend(handles=handles, labels=labels, handleheight=3, handlelength=3, framealpha=1. )

plt.show()

dc0e5d2d-8658-4843-a10c-58feb5de0be1.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以通过使用Graphics2D类的阴影效果来添加阴影。具体步骤如下: 1. 创建一个BufferedImage对象,该对象的宽度和高度应该比要添加阴影的图形对象大一些。 2. 获取BufferedImage对象的Graphics2D对象,并将该对象的颜色设置为阴影的颜色。 3. 使用Graphics2D对象的translate()方法将图形对象移动到BufferedImage对象的底部和右侧。 4. 绘制图形对象到BufferedImage对象上。 5. 使用Graphics2D对象的drawImage()方法将BufferedImage对象绘制到屏幕上,并将BufferedImage对象的位置设置为原始图形对象的位置。 以下是一个示例代码,用于在Java添加阴影: ``` import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; public class ShadowPanel extends JPanel { private Color shadowColor = new Color(0, 0, 0, 50); private int shadowSize = 5; @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); int width = getWidth() - shadowSize * 2; int height = getHeight() - shadowSize * 2; BufferedImage shadow = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D shadowG2d = shadow.createGraphics(); shadowG2d.setColor(shadowColor); shadowG2d.translate(-shadowSize, -shadowSize); shadowG2d.fillRect(0, 0, width + shadowSize, height + shadowSize); g2d.drawImage(shadow, null, 0, 0); g2d.setColor(Color.WHITE); g2d.fillRect(shadowSize, shadowSize, width, height); g2d.dispose(); } } ``` 在这个示例代码中,我们创建了一个自定义的JPanel类ShadowPanel,并覆盖了它的paintComponent()方法。在这个方法中,我们使用了Graphics2D对象来添加阴影。我们首先创建了一个BufferedImage对象,该对象的大小比JPanel对象小一些,然后将其颜色设置为阴影颜色。接着,我们使用translate()方法将JPanel对象移动到BufferedImage对象的底部和右侧,然后使用fillRect()方法绘制一个填充矩形,从而创建了阴影。最后,我们将BufferedImage对象绘制到屏幕上,并将其位置设置为JPanel对象的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值