图形drawImage的问题

时间:2016-01-17 21:59:21

标签: java swing graphics drawimage

我一直在寻找过去的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; 
}

}

输出是这样的:

enter image description here

1 个答案:

答案 0 :(得分:3)

您正在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