1

大声笑,我刚才完全失败了。删除某些内容后上传代码。

无论如何,我使用 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);
    }
}
4

2 回答 2

2

有很多事情不适合你...

  1. drawClient实际上并没有在任何地方调用,因此永远不会创建框架。我实际上会摆脱这种方法,因为它没有增加任何价值,实际上是混淆了问题。
  2. 您正在使用对背景图像的静态引用,这意味着在整个应用程序中只能使用一个图像,无论您创建了多少“客户端”面板。然后,无论如何您将该引用传递给面板,这是一种浪费。
  3. 虽然您至少应该真正记录错误(并显示错误消息),但您应该将堆栈跟踪转储到控制台,这将使您的生活更轻松。
  4. 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();
                }

            }
        });
    }
}

另外,作为旁注,您实际上并没有画背景,但我怀疑这是另一个问题;)

试着看看

于 2013-02-19T03:01:20.100 回答
0

尝试这个:

if (backgroundImage != null) {
         Client c =   new Client(backgroundImage);
         c.drawClient();
} else {
            System.out.println("Error: No background Image.");
}
于 2013-02-19T02:55:29.660 回答