-1

I am a beginner Java programmer, working on a very simple game. I have learned the basics of OOP and am trying to test & expand my knowledge.

In my game, there is a projectile object. The game takes place inside a JFrame.

How would I display the projectile object visually inside the JFrame? I would like to use an image/picture. I essentially want to 'attach' an image to the projectile object.

Main Class

public class MathGame implements ActionListener {

    private static JButton[] choices = new JButton[4];
    private static JLabel[] options = new JLabel[4];
    private static double[] nums = new double[4];
    public static JPanel container;

    public static void main(String[] args) {

        // frame
        JFrame frame = new JFrame("Math Game");
        frame.setSize(300, 500);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // container
        container = new JPanel();
        container.setLayout(null);
        frame.setContentPane(container);

        // title
        JLabel title = new JLabel("Math Game");
        title.setBounds(113, 10, 80, 15);

        // option labels
        for (int i = 0; i < choices.length; i++) {
            options[i] = new JLabel("", SwingConstants.CENTER);
            options[i].setBounds(5 + (72 * i), 50, 68, 68);
            nums[i] = (int) (Math.random() * 100);
            options[i].setText(Double.toString(nums[i]));
            options[i].setOpaque(true);
            options[i].setBackground(Color.RED);
        }

        // buttons
        for (int i = 0; i < choices.length; i++) {
            choices[i] = new JButton();
            choices[i].setBounds(5 + (72 * i), 420, 68, 20);
            choices[i].addActionListener(new MathGame());
            container.add(choices[i]);
        }

        // progress bar
        JProgressBar progress = new JProgressBar();
        progress.setBounds(5, 450, 284, 20);
        progress.setValue(0);
        progress.setBackground(Color.LIGHT_GRAY);
        progress.setStringPainted(true);

        // adding it all
        for (int i = 0; i < choices.length; i++) {
            container.add(choices[i]);
            container.add(options[i]);
        }
        container.add(title);
        container.add(progress);

        // extras
        frame.toFront();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Projectile bullet = new Projectile(150, 250);
        System.out.println(bullet.toString());
    }
}

Projectile Class

public class Projectile extends Piece {

    public Projectile(int x, int y) {
        super(x, y);
    }

    public void fire() {
        for (int i = 0; i < 10; i++) {
            this.yPos = this.yPos - 5;
        }
    }

    public void draw(Graphics2D g2)
            throws IOException {

        BufferedImage image = ImageIO.read(new File("C:\\Users\\jbakos\\Desktop\\pew.png"));
        g2.drawImage(image, this.xPos, this.yPos, null);

    }
}
jzbakos
  • 285
  • 1
  • 3
  • 12
  • Don't read the image in the drawing code. Read your images while you're constructing the GUI. Reading an image from your disk is like 1,000 times slower than drawing the image on your drawing JPanel. – Gilbert Le Blanc Nov 27 '16 at 21:40

2 Answers2

1

First, add a JPanel to JFrame. JFrame is a root container and is not recommended to take up elements like JButtons, JLabels, etc., so you need a sub-container for that

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);

Set the LayoutManager of panel to null, that allows you to position child elements absolutely inside the panel

panel.setLayout(null);

Now, add a JLabel with an ImageIcon which will carry the picture to the panel and position it via setBounds

JLabel lbl = new JLabel(new ImageIcon(picture));
lbl.setBounds(x,y,width,height)

I am not sure what your game application is going to look like, but I advise you to use null layout on the game content area only and use other layout managers for the remaining parts.

swiedenfeld
  • 298
  • 2
  • 14
0

New java programmer myself.

And it depends, I am doing something similar. I made a JPanel that displays a grid, so it displays a 'piece' or object on each square of an 8x8 grid. And then you could display the JPanel within that JFrame

To display an image, the draw method() is similar to below.

public void draw(Graphics2D g2, int x, int y)
            throws IOException {

        BufferedImage image = ImageIO.read(new File("location of image"));
        g2.drawImage(image, x, y, null);


    }

Refer to this for more specifics: How do I draw an image to a JPanel or JFrame?

Community
  • 1
  • 1
Bao Thai
  • 533
  • 1
  • 6
  • 25