如何“真的”在Java应用程序中绘制图像 [英] How to "really" draw images in a Java app

查看:92
本文介绍了如何“真的”在Java应用程序中绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm experimenting with graphics in Java, creating a domino game. However, I have run into another mysterious little "challenge"... I think I really ticked off the IDE this time

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

Anyway here is my code:

// 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.
        }
    }
}

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

Anyway, when I run it, I get this:

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部分)。然后我试图删除try / catch的东西,看看
的异常是什么(我99%肯定会有一个,哈哈)。好吧,我的IDE(NetBeans)以
的方式回应,并且基本上强迫我使用一个。然后我进入项目文件夹
并检查以确保文件位于正确的位置(它是)。所以只是为了它的b
,我把它复制到项目中的每个其他文件夹中,看看是否有帮助。它是
没有。

So I did some research. First, I went to the Java/Oracle website and found their tutorial on drawing images (and in fact my code very closely based on theirs, at least the try/catch part). Then I tried to remove the try/catch thing to see what the exception would be (I was 99% sure there would be one, lol). Well my IDE (NetBeans) responded by bugging out and basically forcing me to use one. So then I went into the project folder and checked to make sure the file was in the right place (it was). So just for the heck of it, I copied it into every other folder in the project to see if that would help. It didn't.

所以很明显我做错了什么,无论出于什么原因,Java都不能或者
赢了' t读取文件,即使它存在且位置正确(现在每个
位置),但我不知道该怎么办。

So it's painfully obvious I've done something wrong, and for whatever reason Java can't or won't read the file, even though it exists and is in correct location (and now every location), but I have no idea what else to do about it.

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

So, as always, I'm open to any ideas, suggestions or other information. Thanks in advance. : )

推荐答案

如果将图像/部分从路径中取出,并确保文件相同,会发生什么情况目录作为你的exe?所以,而不是

What happens if you take the images/ part out of the path, and make sure the file is in the same directory as your exe? So, instead of

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

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

That should check the same directory as where you are running your program from.

这篇关于如何“真的”在Java应用程序中绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆