我正在做一个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()
,因此图像从未初始化。
您可能需要仔细阅读what is a Constructor的一些基本知识
将public void ImagePanel()
更改为public Robot()
不要从File
对象加载图像。使用Robot.class.getResource("path/to/image.png")
通过类路径加载它们。在Embedded Resources上查看信息
您应该在super.paintComponent
方法中调用paintComponent
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
绘画时,应覆盖
getPreferredSize()
的JComponent
,以便组件具有首选的大小,然后只需pack()
框架即可。@Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
从事件调度线程运行Swing应用程序,但使用
SwingUtilities.invokLater
。见Initial Threadpublic static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
}
});
}
见下面的例子
更新
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