Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java g、 drawImage未成功,完全被难倒_Java_Swing_Graphics_Jframe_Drawimage - Fatal编程技术网

Java g、 drawImage未成功,完全被难倒

Java g、 drawImage未成功,完全被难倒,java,swing,graphics,jframe,drawimage,Java,Swing,Graphics,Jframe,Drawimage,我试图画一幅图像,但我不知道为什么它没有出现在屏幕上。图像加载良好 System.out.printf(“%n层1.img层播放器1为:”+Player层1.img层播放器1);表示图像加载正确(正确的图像w/h等) 尝试过各种图像观察者,从不同的类(即Player_1.drawSquare和MyPanel.painComponent)绘制图像 有来自控件类的常规repaint()请求,但我不认为这会阻止绘图 不会抛出任何错误 player_1.gif位于game_2包中,我已尝试在src和ro

我试图画一幅图像,但我不知道为什么它没有出现在屏幕上。图像加载良好

System.out.printf(“%n层1.img层播放器1为:”+Player层1.img层播放器1);表示图像加载正确(正确的图像w/h等)

尝试过各种图像观察者,从不同的类(即Player_1.drawSquare和MyPanel.painComponent)绘制图像

有来自控件类的常规repaint()请求,但我不认为这会阻止绘图

不会抛出任何错误

player_1.gif位于game_2包中,我已尝试在src和root文件夹中使用它运行代码

package test;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
public class Main{
 public static void main(String[] args) {
     GUI.main(args);
 }
}

class GUI {
static JFrame f = new JFrame("Swing Paint Demo");
    public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}

private static void createAndShowGUI() {
    System.out.printf("%nCreated GUI on EDT? "+
    SwingUtilities.isEventDispatchThread());

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.setSize(640,640);
    f.setVisible(true);


} 

}

class MyPanel extends JPanel {

public MyPanel() {

    //setBorder(BorderFactory.createLineBorder(Color.black));
}


public Dimension getPreferredSize() {
    return new Dimension(640,640);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);       
    System.out.printf("%nPaincomponent on GUI");

    Player_1.paintSquare(g);
    System.out.printf("%nPlayer_1.img_player1 is: "+Player_1.img_player1);

}

}

class Player_1{
public static Image img_player1;
public int int_x = 0;
public int int_y = 1;
public int int_w = 1;
public int int_h = 1;

public static void paintSquare(Graphics g){
     if (img_player1 == null)
         img_player1 = IOControl.getImage("player_1.gif");

    g.drawImage(img_player1, 32, 32, 32, 32, GUI.f);

}
}
class IOControl {

public static void main(String[] args) {

}

static BufferedImage getImage(String path)
{

    BufferedImage tempImage = null;
        try
        {

                URL imageURL = Main.class.getResource(path);
                tempImage = toBufferedImage(Toolkit.getDefaultToolkit().getImage(imageURL));
        }
        catch(Exception e)
        {
                System.out.printf("%nAn error occured -" + e.getMessage());
        }
        System.out.printf("%n"+path);
        System.out.printf("%nIOControl.path");

        System.out.printf("%n"+tempImage);
        System.out.printf("%nIOControl.tempImage");
        return tempImage;


}

public static BufferedImage toBufferedImage(Image img)
{
    if (img instanceof BufferedImage)
    {
        return (BufferedImage) img;
    }

    // Create a buffered image with transparency
    BufferedImage bimage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}
}
  • 不要调用
    main
    方法。这意味着JVM可以调用它作为程序的起点。另外,无论在哪个类中是启动类,都应该只有一个
    main
    方法

  • 不要在
    paintXxx
    方法中创建图像。您应该在构造函数中创建

  • 我在原始帖子的评论中提到的所有其他错误点

  • 看起来您的主要问题在于
    getImage
    方法。我为您简化了它,并且它是有效的(考虑到我修复了上面提到的其他点)

  • 下面是完整的运行代码。注意:图像位置正确,与
    Main
    类位于同一个包中

    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class Main {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new GUI().createAndShowGUI();
                }
            });
        }
    }
    
    class GUI {
    
        JFrame f = new JFrame("Swing Paint Demo");
    
        public void createAndShowGUI() {
            System.out.printf("%nCreated GUI on EDT? "
                    + SwingUtilities.isEventDispatchThread());
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MyPanel());
            f.setSize(640, 640);
            f.setVisible(true);
    
        }
    
    }
    
    class MyPanel extends JPanel {
    
        Player_1 player1;
    
        public MyPanel() {
            player1 = new Player_1(MyPanel.this);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(640, 640);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.printf("%nPaincomponent on GUI");
    
            player1.paintSquare(g);
    
        }
    
    }
    
    class Player_1 {
    
        public Image img_player1;
        public int int_x = 0;
        public int int_y = 1;
        public int int_w = 1;
        public int int_h = 1;
        JPanel panel;
    
        public Player_1(JPanel panel) {
            this.panel = panel;
            img_player1 = IOControl.getImage("player_1.png");
        }
    
        public void paintSquare(Graphics g) {
            g.drawImage(img_player1, 32, 32, 32, 32, panel);
        }
    }
    
    class IOControl {
    
        public static BufferedImage getImage(String path) {
            BufferedImage img = null;
            try {
                img = ImageIO.read(GUI.class.getResource(path));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return img;
        }
    }
    

    1) 选择使用Java命名约定的更好的类名和变量。变量和类名使用驼峰式大小写。此外,您还使用类型
    Player\u 1
    命名了一个变量
    Player\u 1
    。我不确定这是否让你感到困惑,但对我来说确实如此。2) 不要不必要地使用
    static
    ,这样做就太过分了。3) 不要使用将要绘制的方法创建图像
    paintSquare
    。4)显示您的<代码> IOrg.GeMimeG/<代码>方法,并提到“代码> > PraveRy1.GIF”<代码>位于您的文件结构中。5)考虑发布一个可编译的示例,我们可以测试。在它的当前状态下,由于缺少类,它是不可编译的。去掉不必要的代码并包含必要的类。确保它已编译,然后用示例编辑您的帖子。仍然无法编译。为此,只创建一个.java文件,不管主启动类是什么类。将所有类都放入该文件中。在除主类之外的类名中,省略
    public
    。如果你可以创建一个文件来编译,那么我们也应该能够测试它。我已经用player_1.gif位置和IOControl.getImage方法更新了帖子。我目前正在制作一个可编译的示例。
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class Main {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new GUI().createAndShowGUI();
                }
            });
        }
    }
    
    class GUI {
    
        JFrame f = new JFrame("Swing Paint Demo");
    
        public void createAndShowGUI() {
            System.out.printf("%nCreated GUI on EDT? "
                    + SwingUtilities.isEventDispatchThread());
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MyPanel());
            f.setSize(640, 640);
            f.setVisible(true);
    
        }
    
    }
    
    class MyPanel extends JPanel {
    
        Player_1 player1;
    
        public MyPanel() {
            player1 = new Player_1(MyPanel.this);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(640, 640);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.printf("%nPaincomponent on GUI");
    
            player1.paintSquare(g);
    
        }
    
    }
    
    class Player_1 {
    
        public Image img_player1;
        public int int_x = 0;
        public int int_y = 1;
        public int int_w = 1;
        public int int_h = 1;
        JPanel panel;
    
        public Player_1(JPanel panel) {
            this.panel = panel;
            img_player1 = IOControl.getImage("player_1.png");
        }
    
        public void paintSquare(Graphics g) {
            g.drawImage(img_player1, 32, 32, 32, 32, panel);
        }
    }
    
    class IOControl {
    
        public static BufferedImage getImage(String path) {
            BufferedImage img = null;
            try {
                img = ImageIO.read(GUI.class.getResource(path));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return img;
        }
    }