java - g.drawImage不成功,完全难住了

标签 java swing graphics jframe drawimage

尝试绘制图像,但我不知道为什么它没有出现在屏幕上。图像加载正常。

System.out.printf("%nPlayer_1.img_player1 为:"+Player_1.img_player1);表示图像已正确加载(正确的图像w/h等)。

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

有来自 Control 类的常规 repaint() 请求,但我不认为这会阻止绘制。

没有抛出任何错误。

player_1.gif 位于 game_2 包中,我尝试在 src 和根文件夹中运行代码。

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

最佳答案

  1. 不要调用 main 方法。它的目的是让 JVM 作为程序的起点进行调用。另外,您应该只拥有一个 main 方法,无论哪个类是启动类。

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

  3. 我在原帖评论中提到的所有其他错误点。

  4. 看来您的主要问题出在 getImage 方法中。我为您简化了它并且它有效(假设我修复了上面提到的其他点)。

    public static BufferedImage getImage(String path) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(GUI.class.getResource(path));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return img;
    }
    

这是完整的运行代码。注意:您的图像位置正确,它与 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;
    }
}

关于java - g.drawImage不成功,完全难住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22587537/

相关文章:

java - 根据 xml 文件中属性的更改更新 java 类中的数据

java - 停止线程并使其在单击按钮时运行;

java - 如何将两个 View 堆叠在一起?

java - JApplet不使用paintComponent(Graphics g)方法绘制圆形

c++ - 无窗口栏 SDL C++ 应用程序

ios - 为什么我的 PhoneGap 应用程序图形仅在 iOS 上损坏?

java - 在这种情况下,java 堆会抖动到磁盘吗?

java - 指定属性和日志文件的位置时出错

java - 如何使用 javax.swing.text.html 从 HREF 标记获取完整/绝对链接?

java - 如何检测用户是否使用ipad或机器访问网站?