如何“真正”在Java应用程序中绘制图像

时间:2013-07-05 07:05:14

标签: java image swing jpanel paintcomponent

我正在尝试用Java创建图形,创建一个多米诺骨牌游戏。但是,我遇到了另一个神秘的小“挑战”......我想这次我真的已经退出了IDE

无论如何,这是我的代码:

// In the main class
import java.awt.Color;
import javax.swing.JFrame;
public class GameBoard extends JFrame {
    public static void main(String[] args) {
        JFrame game = new JFrame();
        game.setTitle("Domino");
        game.setSize(800, 600);
        game.setDefaultCloseOperation(EXIT_ON_CLOSE);
        game.setBackground(Color.GREEN);
        Domino double6 = new Domino("images/double_6.png",16,16,'H',6,6);
        game.add(double6);
        // Create pieces
        game.setVisible(true);
    }
}

// The game piece class
package domino;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Domino extends JPanel {
    // Instance variables for each piece
    public int n1, n2, x, y;
    public char position;
    // n1 is the first number, n2 is the second number, x and y are coordinates, position refers to horizontal or vertical (ideally would be a different image for each direction)
    public BufferedImage img = null;
    public String fileName;

    // Constructor
    public Domino(String fileName, int x, int y, char position, int n1, int n2) {
        this.fileName = fileName;
        this.x = x;
        this.y = y;
        this.n1 = n1;
        this.n2 = n2;
        this.position = position;
        repaint();
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.position == 'H') {
        try {
            // Here's where the trouble appears to rear its ugly head...
            img = ImageIO.read(new File(fileName));
            g.drawImage(img, this.x, this.y, null);
        } catch (IOException ex) {
            // This code was generated by the system - it wouldn't even let me do the drawImage code about without a try/catch.
            Logger.getLogger(Domino.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
            // If all else fails I can at least draw the game pieces manually.
            g.setColor(Color.WHITE);
            g.fillRect(this.x, this.y, 32, 64);
            // etc.
        }
    }
}

无论如何,当我运行它时,我得到了这个:

run:
Jul 04, 2013 11:33:13 PM domino.Domino paintComponent
SEVERE: null
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at domino.Domino.paintComponent(Domino.java:43)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3877)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Jul 04, 2013 11:33:14 PM domino.Domino paintComponent
SEVERE: null
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at domino.Domino.paintComponent(Domino.java:43)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3877)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

BUILD SUCCESSFUL (total time: 6 seconds)

所以我做了一些研究。首先,我访问了Java / Oracle网站并找到了他们的教程 在绘制图像(实际上我的代码非常接近他们的,至少是 尝试/捕捉部分)。然后我尝试删除try / catch事物以查看异常 会(我99%肯定会有一个,哈哈)。我的IDE(NetBeans)回应了 烦恼,基本上强迫我使用一个。然后我进入了项目文件夹 并检查以确保文件位于正确的位置(它是)。所以只是为了哎呀 它,我把它复制到项目中的每个其他文件夹,看看是否会有所帮助。它 没有。

所以很明显我做错了什么,无论出于何种原因,Java都无法做到 将不会读取该文件,即使它存在并且位于正确的位置(现在每个 位置),但我不知道该怎么办。

因此,与往常一样,我对任何想法,建议或其他信息持开放态度。提前致谢。 :)

3 个答案:

答案 0 :(得分:4)

检查您当前的工作目录:Getting the current working directory in Java。它应该是包含images文件夹的文件夹,但可能不是,因此是例外。

答案 1 :(得分:3)

错误消息是

javax.imageio.IIOException: Can't read input file!

根据堆栈跟踪,当您在Domino.java的第43行调用ImageIO.read时会发生这种情况。

确保您要加载的文件存在,可读并且相对路径(images / double_6.png)与您执行该程序的目录相关。

答案 2 :(得分:1)

如果将图像/部分从路径中取出,并确保文件与exe在同一目录中会发生什么?所以,而不是

Domino double6 = new Domino("images/double_6.png",16,16,'H',6,6);

尝试

Domino double6 = new Domino("double_6.png",16,16,'H',6,6);

这应该检查您运行程序的目录。