我一直在寻找过去的StackOverflow帖子,并试图弄清楚为什么我的图像无法显示。

我知道ImageIO很好,因为我可以在getWidth()上运行BufferedImage并返回正确的宽度。

这是我的Graphic课,然后是我的主课。
(对不起,我的代码不完整,这是我的新手。)

Graphic类中的代码:

package blackjack;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Graphic extends JPanel implements ActionListener {
/**
 *
 */
private static final long serialVersionUID = 1L;


public JFrame frame = new JFrame("Game Window");
public JPanel layout = new JPanel(new BorderLayout());
public JPanel menu = new JPanel();
public JPanel playing = new JPanel(new BorderLayout());
public JPanel game = new JPanel();
public BufferedImage cardArray[] = new BufferedImage[52];
public void begin() {
    //starting menu
}


public void playersTurn() {
    menu.add(playing);

    Font font = new Font("",Font.PLAIN, 24);
    JPanel btnHolder = new JPanel();
    JLabel play = new JLabel("Playing:");
    JLabel or = new JLabel(" or ");
    JLabel question = new JLabel(" ? ");
    question.setFont(font);
    or.setFont(font);
    play.setFont(font);
    JButton hit = new JButton("Hit");
    JButton stand = new JButton("Stand");
    hit.addActionListener(this);
    stand.addActionListener(this);


    playing.add(play, BorderLayout.WEST);
    playing.add(btnHolder, BorderLayout.CENTER);
    btnHolder.add(hit);
    btnHolder.add(or);
    btnHolder.add(stand);
    btnHolder.add(question);


}


public void gui() {
    //main gui


    Dimension imageD = new Dimension(71,96);
    Dimension menuD = new Dimension(900,120);
    menu.setBorder(BorderFactory.createLineBorder(Color.black));
    menu.setPreferredSize(menuD);

    JPanel titlePanel = new JPanel();
    JLabel title = new JLabel("BlackJack");
    title.setFont(new Font("", Font.PLAIN, 14));
    titlePanel.add(title);

    Graphic gr = new Graphic();
    gr.setPreferredSize(imageD);

    //adding
    frame.add(layout);
    layout.add(menu, BorderLayout.SOUTH);
    layout.add(titlePanel, BorderLayout.NORTH);
    layout.add(gr, BorderLayout.CENTER);

    //frame settings
    frame.setSize(900, 650);
    frame.setResizable(false);
    frame.setVisible(true);
}

public void buildPathArray() {

    for(int i = 1; i<=52; i++){
    BufferedImage im = null;
    try {
        im = ImageIO.read(new File(Blackjack.getInstallDir() + Blackjack.s + "src" + Blackjack.s + "cardpngs"+ Blackjack.s + (100+i)+".png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
      cardArray[i-1]= im;
      //System.out.println(Blackjack.getInstallDir() + "\\src\\cardpngs\\" + (100+i)+".png");
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Hit")) {

    } else if(e.getActionCommand().equals("Stand")) {

    }

}
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //g.setColor(Color.GREEN);
    //g.fillOval(20, 20, 20, 20);
    g.drawImage(cardArray[0], 0, 0, this);
}

}


我的主要课程中的代码:

package blackjack;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Blackjack {
public static String installDir = "";
public static String s = "";

public static void main(String[] args) {
    Path currentRelativePath = Paths.get("");
    installDir = currentRelativePath.toAbsolutePath().toString();
    s = System.getProperty("file.separator");
    Graphic gr = new Graphic();
    gr.buildPathArray();
    gr.gui();
    //System.out.println(installDir);
    //g.playersTurn();

}
public static String getInstallDir() {
    return installDir;
}

}


输出是这样的:

java - 图形drawImage问题-LMLPHP

最佳答案

您正在您的Graphic类中创建Blackjack的一个实例...

public class Blackjack {
    public static String installDir = "";
    public static String s = "";

    public static void main(String[] args) {
        //...
        Graphic gr = new Graphic();
        gr.buildPathArray();
        gr.gui();
    }


还有另一个在您的Graphic类中

public void gui() {
    //...
    Graphic gr = new Graphic();
    gr.setPreferredSize(imageD);

    //adding
       //...
    layout.add(gr, BorderLayout.CENTER);
    //...
}


但是您只能使用buildPathArray类中实例的BlackBelt来初始化图像,这并不是屏幕上实际显示的内容...

作为一般经验法则,您不应出于显示该组件的明确目的而在另一个组件内创建JFrame的实例。您的Graphic组件也试图做太多事情。相反,我可能会有一个Game类,它将标题,菜单和Graphic组件放在一起,然后将其放到JFrame的实例上

这样做的主要原因是,您的Graphic类正在尝试做太多事情。它应全权负责显示和管理卡。 Game类应管理其他UI元素,并负责确保UI符合游戏“模型”的当前状态,接受用户输入(并侦听来自其他UI元素的事件)并更新模型和响应模型创建的事件,有点像...

BlackJack ...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BlackJack {

    public static void main(String[] args) {
        new BlackJack();
    }

    public BlackJack() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Game());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Game extends JPanel {

        private JPanel menu;
        private Graphic graphic;

        public Game() {

            menu = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(900, 120);
                }
            };
            menu.setBorder(BorderFactory.createLineBorder(Color.black));

            JPanel titlePanel = new JPanel();
            JLabel title = new JLabel("BlackJack");
            title.setFont(new Font("", Font.PLAIN, 14));
            titlePanel.add(title);

            Graphic gr = new Graphic();
            gr.buildPathArray();

            setLayout(new BorderLayout());

            add(menu, BorderLayout.SOUTH);
            add(titlePanel, BorderLayout.NORTH);
            add(gr, BorderLayout.CENTER);
        }

    }

}


Graphic ...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Graphic extends JPanel {

    private static final long serialVersionUID = 1L;

    public BufferedImage cardArray[] = new BufferedImage[52];

    public void begin() {
        //starting menu
    }

    public void playersTurn() {
        // All of this belongs in Game
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(71,96);
    }

    public void buildPathArray() {

        for (int i = 1; i <= 52; i++) {
            BufferedImage im = null;
            try {
                im = ImageIO.read(new File(Blackjack.getInstallDir() + Blackjack.s + "src" + Blackjack.s + "cardpngs" + Blackjack.s + (100 + i) + ".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            cardArray[i - 1] = im;
            //System.out.println(Blackjack.getInstallDir() + "\\src\\cardpngs\\" + (100+i)+".png");
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g.setColor(Color.GREEN);
        //g.fillOval(20, 20, 20, 20);
        g.drawImage(cardArray[0], 0, 0, this);
    }

}


您可能还想看看Model-View-Controller

10-05 20:02