gpt4 book ai didi

java - 无法让图像在 Java 中显示

转载 作者:行者123 更新时间:2023-12-01 23:10:44 25 4
gpt4 key购买 nike

我正在做一个 SwingBot,但不知道如何让图像出现。它编译得很好,但不可见。我究竟做错了什么? This is my image.它位于与我的代码 java 文件位于同一目录中的名为“images”的文件夹中。

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Scanner;



public class SwingBot
{
public static void main(String[] args)
{

// contruction of new JFrame object
JFrame frame = new JFrame();

// mutators
frame.setSize(400,400);
frame.setTitle("SwingBot");

// program ends when window closes
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Robot r = new Robot();

frame.add(r);

// voila!
frame.setVisible(true);


// your Scanner-based command loop goes here

int noend = 0;
System.out.println("Enter Commands");
while(noend == 0)
{

Scanner input = new Scanner(System.in);

String command = input.next();

if(command.equals("left"))
r.moveBot(10,0);

if(command.equals("right"))
r.moveBot(-10,0);

if(command.equals("down"))
r.moveBot(0,10);

if(command.equals("up"))
r.moveBot(0,-10);

// call methods on the Robot instance like w.moveBot(10,10) in response to
// user input



}

}

public static class Robot extends JComponent
{
private Rectangle rect = new Rectangle(10,10);
private BufferedImage image;

public void ImagePanel()
{
try
{
image = ImageIO.read(new File("images/flower.png"));
}
catch (IOException ex)
{
// handle exception...
}
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;


// set the color
//g2.setColor(Color.BLUE);




g.drawImage(image, 0, 0, null);


}


public void moveBot(int x, int y)
{
// move the rectangle
rect.translate(x,y);

// redraw the window
repaint();
}

}

}

最佳答案

这很容易被忽视,因为它太奇怪了。您有一个方法ImagePanel()。我认为可以安全地假设您从 ImagePanel 获得的代码是类,而 ImagePanel() 是构造函数,并且您刚刚添加了 void > 因为您收到一条错误消息,指出该方法需要返回类型。

您应该做的是将 public ImagePanel() 放入 public Robot() 中,这样您的 Robot 类就有一个构造函数。目前,您实例化了 Robot,但图像从未初始化,因为从未调用 方法 ImagePanel()

  1. 可能需要了解 what is a Constructor 的一些基础知识。

  2. public void ImagePanel() 更改为 public Robot()

  3. 不要从 File 对象加载图像。使用 Robot.class.getResource("path/to/image.png") 通过类路径加载它们。查看信息 Embedded Resources

  4. 您应该在 paintComponent 方法中调用 super.paintComponent

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    }
  5. 绘制时,您应该重写 JComponentgetPreferredSize() ,以便组件具有首选大小,然后您只需 pack() 你的框架。

    @Override
    public Dimension getPreferredSize() {
    return new Dimension(width, height);
    }
  6. 从事件调度线程运行 Swing 应用程序,但使用 SwingUtilities.invokLater。请参阅Initial Thread

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
    public void run() {

    }
    });
    }
<小时/>

参见下面的示例

更新

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class SwingBot {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JFrame frame = new JFrame();
frame.add(new Robot());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class Robot extends JComponent {
BufferedImage img;

public Robot() {
try {
img = ImageIO.read(Robot.class.getResource("/images/android.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, 300, 300, this);
}

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

文件结构

ProjectRoot
src
somepackage
SwingBot.java
images
android.jpg

关于java - 无法让图像在 Java 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057490/

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