无法在Java中显示图像

时间:2014-02-27 01:51:05

标签: java swing paint

我正在做一个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();
    }

}

}

1 个答案:

答案 0 :(得分:3)

这很容易被忽视,只是因为它太奇怪了。你有一个方法ImagePanel()。我认为可以安全地假设你从ImagePanel得到的代码是类,而ImagePanel()是构造函数,你只是添加了void,因为你得到一个错误说方法需要返回类型。

您应该做的是将public ImagePanel()变为public Robot(),以便您的Robot类具有构造函数。目前您实例化Robot但图片永远不会初始化,因为永远不会调用方法 ImagePanel()

  1. 可能需要在what is a Constructor

  2. 上查看一些基本内容
  3. public void ImagePanel()更改为public Robot()

  4. 不要从File个对象加载图像。使用Robot.class.getResource("path/to/image.png")通过类路径加载它们。请参阅Embedded Resources

  5. 上的信息
  6. 您应该在super.paintComponent方法

    中致电paintComponent
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
    
  7. 绘画时,您应该覆盖getPreferredSize()的{​​{1}},以便该组件具有首选尺寸,然后您只需JComponent您的框架。

    pack()
  8. 从事件调度线程运行Swing应用程序,但使用@Override public Dimension getPreferredSize() { return new Dimension(width, height); } 。见Initial Thread

    SwingUtilities.invokLater

  9. 见下面的例子

    <强>更新

    enter image description here

    public 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);
            }
        }
    }