0

I've been hunting through past StackOverflow posts and trying to figure out why my image won't display.

I know that the ImageIO is fine since I can run getWidth() on my BufferedImage and it returns the correct width.

Here is my Graphic class, followed by my main class. (I'm sorry for trashy code, I'm new to this.)

Code in Graphic class:

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); 
}

}

Code in my main class:

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; 
}

}

The output is this:

enter image description here

0

1 Answer 1

3

You're creating one instance of Graphic in your Blackjack class...

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();
    }

And another in your Graphic class

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

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

But you only initialise the images, using buildPathArray of the instance in you BlackBelt class, which is not what is actually displayed on the screen...

As a general rule of thumb, you shouldn't be creating an instance of JFrame from within another component with the express purpose of display that component. Your Graphic component is also trying to do too much. Instead, I would have a Game class, maybe, which pulled the title, menu and Graphic components together and then put that onto an instance of JFrame

The main reason for this is, is your Graphic class is trying to do too much. It should be solely responsible for display the cards and managing them. The Game class should manage the other UI elements and be responsible for ensuring that the UI meets the current state of the game "model", taking in user input (and listening to events from the other UI elements) and updating the model and responding to events that the model creates, a little more like...

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);
    }

}

You might also want to have a look at Model-View-Controller.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.