图片输出。(Java)

时间:2013-10-03 11:14:34

标签: java

我的代码有问题,你能说出它的错误吗?

以下是代码:

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel {

    private static final long serialVersionUID = 1L;

    private BufferedImage image;

    public static final int WIDTH = 600;
    public static final int HEIGHT = 500;

    public static void main(String avg[]) throws IOException {
        Game abc = new Game();
    }

    public Game() {
        try {
            JFrame frame = new JFrame();
            frame.setSize(WIDTH, HEIGHT);
            frame.setVisible(true);
            frame.getContentPane().setBackground(Color.BLACK);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            image = ImageIO.read(new File(
                    "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));
        } catch (IOException ex) {
            // handle exception...
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null); 
    }

}

我有一个窗口,但图片没有显示。如果你写的问题是什么,那就太棒了!如果你还有固定的 - 补充了代码,那就太棒了!

感谢您的关注。

UPD

谢谢大家。 更新了这样的代码,图像被带出,但背景不再是黑色!

public Game() {
        try {   
            image = ImageIO.read(new File(
                    "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));

            JFrame frame = new JFrame();
            frame.setSize(WIDTH, HEIGHT);
            frame.setVisible(true);
            frame.getContentPane().setBackground(Color.BLACK);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.add(this);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

如何给我一个黑色背景?)

UPD2

这是一个适用于我的代码,谢谢大家。

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel {

    private static final long serialVersionUID = 1L;

    private BufferedImage image;

    public static final int WIDTH = 600;
    public static final int HEIGHT = 500;

    public static void main(String avg[]) throws IOException {
        Game abc = new Game();

    }

    public Game() {
        try {
            JFrame frame = new JFrame();
            image = ImageIO.read(new File(
                    "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));

            frame.setSize(WIDTH, HEIGHT);
            frame.setVisible(true);
            frame.getContentPane().add(this);
            this.setBackground(Color.BLACK);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null); // see javadoc for more info on the
                                        // parameters
    }

}

3 个答案:

答案 0 :(得分:2)

在添加组件后添加Game的实例并调用setVisible,以便JPanel使窗口包含组件,框架可以正确地绘制添加的组件

frame.add(this);
frame.setVisible(true);

答案 1 :(得分:0)

  

你能说出它的错误吗

这显然是错误的:

    } catch (IOException ex) {
        // handle exception...
    }

这至少应该是:

    } catch (IOException ex) {
        ex.printStackTrace();
    }

能够看出你是否有错误......

您的图片未显示的原因可能是您没有指定将游戏组件添加到框架的ContentPane中:

frame.getContentPane().add(this);

答案 2 :(得分:0)

setVisible设为TRUE

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;

    public class ImageInFrame {
        public static void main(String[] args) throws IOException {
            String path = "Image1.jpg";
            File file = new File(path);
            BufferedImage image = ImageIO.read(file);
            JLabel label = new JLabel(new ImageIcon(image));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(label);
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }