gpt4 book ai didi

java - 将图像加载到 Java 中

转载 作者:行者123 更新时间:2023-12-01 18:37:22 25 4
gpt4 key购买 nike

我刚刚开始用java编程。然而,最近我无法将图像加载到我的游戏中。

我尝试将它们加载到单独的资源包中并阅读/观看了无数教程,但似乎根本不起作用!

有人可以告诉我代码有什么问题或提供任何建议吗?任何帮助将非常感激。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Main extends JFrame{

Image dbImage;
Graphics dbg;

//Variables for screen size
int
GWIDTH = 800,
GHEIGHT = 500;

//Dimension of gwidth and gheight
Dimension screenSize = new Dimension (GWIDTH, GHEIGHT);

public Main (){
//constructor to spawn window
this.setTitle ("Pond");
this.setSize(screenSize);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.cyan);
this.addKeyListener(new AL ());
ImageIcon img = new ImageIcon (getClass().getResource("/resources/ninja-png.gif"));

}

public static void main (String[] args){

Main m = new Main ();
}

//Double Buffering
@Override

public void paint (Graphics g){

dbImage = createImage (getWidth(), getHeight());
dbg = dbImage.getGraphics ();
draw (dbg);
g.drawImage(dbImage, 0, 0, this);

}

public void draw (Graphics g){
}
public void paintComponent (Graphics g){
}
//Event Listener Class
public class AL extends KeyAdapter {
@Override
public void keyPressed (KeyEvent e){
}
@Override
public void keyReleased (KeyEvent e){
}
}

}

最佳答案

"Can someone tell me what's wrong with the code or provide any suggestions?"

  1. 不要在 JFrame 等顶级容器上绘制。

  2. 改为使用 JPanel 并重写其 paintComponent 方法并调用 super.paintComponent

  3. @Override getPreferredSize()JPanel 上绘制时,因此面板将具有首选大小。

  4. 调用 frame.pack() 而不是设置大小,并且将遵循 JPanel 的首选大小。

  5. 将该自定义绘画面板添加到 JFrame

  6. 您从未初始化您尝试绘制的图像。改为执行此操作。

    dbImage = new ImageIcon(getClass().getResource("/resources/stackoverflow5.png")).getImage();

    声明一个您从未使用过的新ImageIcon

  7. Event Dispatch Thread 运行 Swing 应用程序像这样

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
    @Override
    public void run() {
    Main m = new Main();
    }
    });
    }
  8. 确保您有直接在 src 中的资源包

    Project
    src
    resources
<小时/>

enter image description here

使用上面提到的所有内容完整重构源代码。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

Image dbImage;

int GWIDTH = 800;
int GHEIGHT = 500;

public Main() {
dbImage = new ImageIcon(getClass().getResource("/resources/stackoverflow5.png")).getImage();
this.setTitle("Pond");
add(new ImagePanel());
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.cyan);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
Main m = new Main();
}
});
}

public class ImagePanel extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(dbImage, 0, 0, GWIDTH, GHEIGHT, this);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(GWIDTH, GHEIGHT);
}
}
}

关于java - 将图像加载到 Java 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21364253/

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