【发布时间】:2014-03-23 05:44:33
【问题描述】:
尝试绘制图像,但我不知道为什么它没有出现在屏幕上。图片加载正常。
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) 选择更好的类名和使用 Java 命名约定的变量。类名的变量使用驼峰式大小写。此外,您还使用类型
Player_1命名了变量Player_1。不确定它是否让你感到困惑,但对我来说肯定是。 2) 不要不必要地使用static,这样你就完蛋了。 3) 不要在将要绘制paintSquare的方法中创建图像。 4) 显示您的IOControl.getImage方法并提及"player_1.gif"在您的文件结构中的位置。 -
5) 考虑发布一个我们可以测试的可编译示例。在当前状态下,由于缺少类,它无法编译。去掉不必要的代码,加入是必要的类。确保它可以编译,然后使用示例编辑您的帖子。
-
仍然无法编译。执行此操作,只需创建一个 .java 文件,无论您的主要启动类是什么类。将您的所有类放入该文件中。在除主类之外的类名中省略
public。如果您可以创建一个可以编译的文件,那么我们也应该可以对其进行测试。 -
我已经用 player_1.gif 位置和 IOControl.getImage 方法更新了帖子。我目前正在制作一个可编译的示例。
标签: java swing graphics jframe drawimage