gpt4 book ai didi

java - fillRect 格式不正确

转载 作者:行者123 更新时间:2023-11-29 05:05:13 30 4
gpt4 key购买 nike

我想为我创建的游戏制作背景。我在使用 fillRect 时遇到了一些问题。getHeight 和 getWidth 是否必须按特定顺序排列,还是应该使用 getX/Y/Height/Width?

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Graphical loop start
Rectangle rect = new Rectangle(0,0,1440,900) ;
g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
//Graphical loop end
}

最佳答案

首先,您使用的是 Graphics 对象而不是 Graphics2D。您需要先设置油漆颜色,然后使用填充方法。

g2d.setPaint(Color.BLUE);
g2d.fill(new Rectangle2D.Double(0, 0, screenWidth, screenHeight));

Rectangle2D 构造函数参数是:左上角的 x 坐标、左上角的 y 坐标、宽度、高度

最好使用 Graphics2D.fill() 方法,该方法将接受任何实现 Shape 接口(interface)的对象。如果您决定这样做,这样可以更轻松地将形状更改为不同的形状。

关于java - fillRect 格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592983/

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