Lol,我刚才完全失败了。删除某些内容后上传了代码。
不管怎样,我使用ImageIcons加载了背景,但我试图使用BufferedImage,因为我被告知必须这样做才能在上面绘制更多图像。
BufferedImage不会返回任何错误,但不会绘制任何内容。
代码如下:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Client {
static JFrame client = new JFrame();
public static void drawBackground() throws IOException {
BufferedImage background = ImageIO.read(new File("Resources/Images/Background.png"));
client.setTitle("Keldagrim: The lost Empire");
client.setSize(1280, 720);
client.setDefaultCloseOperation(client.EXIT_ON_CLOSE);
Container pane = client.getContentPane();
BackgroundPanel backgroundPanel = new BackgroundPanel(background);
pane.add(backgroundPanel);
client.pack();
client.setVisible(true);
}
public static void drawLogin() {
ImagePanel loginBox = new ImagePanel(new ImageIcon("Resources/Images/LoginBox.png").getImage());
client.add(loginBox);
client.repaint();
System.out.println("Painted");
}
public static void main(String[] args) {
try {
drawBackground();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
drawLogin();
}
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
class BackgroundPanel extends JPanel {
BufferedImage backgroundImage;
public BackgroundPanel(BufferedImage image){
backgroundImage = image;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(backgroundImage, null, 50,50);
}
}
有很多事情不适合你。。。
- 您没有在任何位置实际调用
drawClient
,因此从未创建帧。我实际上会去掉这个方法,因为它没有增加任何值,而且实际上混淆了问题 - 您使用的是对背景图像的静态引用,这意味着无论您创建了多少个"客户端"面板,在整个应用程序中只能使用一个图像。然后无论如何都要将该引用传递给面板,这是一种浪费
- 虽然您确实应该记录错误(并显示错误消息),但至少应该将堆栈跟踪转储到控制台,这将使您的生活更加轻松
- 完成框架设置后,请致电
setVisible
,否则会发生有趣的事情
尝试使用以下内容。。。
public class Client extends JPanel {
private Image backgroundImage;
public Client(Image backgroundImage) {
this.backgroundImage = backgroundImage;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
BufferedImage background = null;
try {
background = ImageIO.read(new File("Resources/Images/Background.png"));
JFrame client = new JFrame();
client.setTitle("Keldagrim: The lost Empire");
client.setSize(1280, 720);
client.setDefaultCloseOperation(client.EXIT_ON_CLOSE);
client.setContentPane(new Client(background));
client.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
此外,顺便说一句,你实际上并没有画背景,但我怀疑这是另一个问题;)
试着看看
- 如何使用Swing创建UI
试试这个:
if (backgroundImage != null) {
Client c = new Client(backgroundImage);
c.drawClient();
} else {
System.out.println("Error: No background Image.");
}