gpt4 book ai didi

java - 在 JFrame 中绘制 Graphics2D

转载 作者:行者123 更新时间:2023-12-03 02:26:55 26 4
gpt4 key购买 nike

我正在制作一个 2d 垂直射击游戏,其中除了图形之外的所有内容都已编码(并且可以工作)。我以前没有使用过 Graphics 类,所以这对我来说是全新的。以下是我用来将所有内容绘制到 JFrame 的代码:

public void paintAll()
{
Graphics h = new Graphics2D();
for(Bullet j : GameState.getEnBullets()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
for(Enemy j : GameState.getEnemies()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
for(Bullet j : GameState.getPlayBullets()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
this.paint(h);
}

第一行“Graphics h = new Graphics2D();”产生错误,因为 Graphics2d 是抽象的,但我不知道从这里去哪里。

我需要代码来获取我拥有的所有图像并将它们绘制到 JFrame 中的点。我提醒您,我以前从未这样做过,所以请告诉我这是否是错误的方法。

最佳答案

覆盖paintComponent()反而;它将提供Graphics上下文。您可以cast将其转换为 Graphics2D

Graphics2D g2d = (Graphics2D) g;

附录:这假设您正在覆盖 paintComponent()JComponent ,然后将其添加到 JFrame 中。

关于java - 在 JFrame 中绘制 Graphics2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6260436/

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