0

I'm new at working with JFrames. So I dont really know how to draw/paint/display everything in a JFrame. Im working on a memory game. Currently im working at the first form, this form must display a background image, a welcome text, a dropdown list with the amount of cards for the memory game and a button which should start the game. I succesfully displayed the background image and the welcome text. After adding the JButton and combobox my form got messed up (I only see a blue background). I have no idea what im doing wrong, also I dont know how I can place the button and dropdown on the right x and y position. I want to place the dropdown box under the welcome text and the button to the right of the dropdownbox.

This is my code:

Main:

package Memory;

public class Main {

    public static Memory memory;

    public static void main(String[] args) {
    memory = new Memory();
    }
}

Renderer:

package Memory;

import javax.swing.*;
import java.awt.*;

public class Renderer extends JPanel {

    private static final long serialVersionUID = 1L;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Memory.backImage = new ImageIcon("Memory/memoryGame.jpg");
        Main.memory.repaint(g);

    }
}

Memory:

package Memory;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import javax.imageio.*;
import java.awt.FlowLayout;



public class Memory implements ActionListener {

    public String[] amountOfCards = {"8","16","32"};

    private JButton startButton;

    public Renderer renderer;

    public static ImageIcon backImage;
    public boolean screen1;

    public static final int WORLD_WIDTH=1250, WORLD_HEIGHT=800;

    public Memory() { 

    JComboBox comboBox = new JComboBox(amountOfCards);
    comboBox.setSelectedIndex(1);
    startButton = new JButton("Start game");


    JFrame jframe = new JFrame();
    Timer timer = new Timer(20,this);
    renderer = new Renderer();

    jframe.add(renderer);
    jframe.setTitle("Memory game");
    jframe.setSize(WORLD_WIDTH,WORLD_HEIGHT);
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setResizable(false);jframe.add(startButton);
    jframe.add(comboBox);
    jframe.setVisible(true);

    screen1=true;

    timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    renderer.repaint();

    }

    public void repaint(Graphics g) {

        //welcome screen
        if(screen1) {
            BufferedImage scaledImage = getScaledImage();
            g.drawImage(scaledImage, 0, 0, null);

            g.setColor(new Color(150, 196, 100));
            g.setFont(new Font("TimesRoman", Font.PLAIN, 75));
            g.drawString("MEMORY GAME", WORLD_WIDTH / 2 - 275, 100);

            g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
            g.drawString("Please select the amount of cards u want to play with and start the game!", WORLD_WIDTH / 2 -400, 200);


        }

    }

    public BufferedImage getScaledImage() {
        BufferedImage image = new BufferedImage(WORLD_WIDTH,WORLD_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) image.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(backImage.getImage(), 0, 0,WORLD_WIDTH,WORLD_HEIGHT, null);

        return image;
    }

}
ack
  • 1,181
  • 1
  • 17
  • 36
user7432713
  • 197
  • 3
  • 17

2 Answers2

2

Alright, multiple errors in a first view of your code

  1. public static Memory memory; : That line, having static and being a public member of class will harm you a lot! Why? Because your memory variable is the same for all the instances you create, because it's a "Class variable" not an "instance variable", and being public is going against the convention and security reasons. For reference see: What does the 'static' keyword do in a class? and Why use getters and setters?

  2. From the above point, static is not a "magic cross method / class" word that allows you to use variables through classes or with main and other methods... and thus this line public static ImageIcon backImage; and this one: Memory.backImage = new ImageIcon("Memory/memoryGame.jpg"); shouldn't exist. And the same for this one: Main.memory.repaint(g);

  3. That being said, you should NEVER NEVER NEVER EVER!!! override repaint() method, all of that code should be into the paintComponent(...) mehtod!

  4. Also it is wise to treat images as embedded resources, because, once you export your program as a JAR file, they will become embedded resources, it's better to treat them like if they already were, here you can find information about how to change your program accordingly.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
1

Try and set your ImageIcon BackImage from public static, to public. Also, try and write the jframe.setVisible(true); command after timer.start()

note that setting your JFrame to visible, before completing every initialisation, might lead to unwanted behaviour of your program.

To set the position of your button on the screen, after adding your JButton to your JFrame, you can use: startButton.setLocation(x,y);

After you play a little bit with the coordinates of your window, you'll get the spot you want for your JButton. Keep in mind that (0,0) is usually the upper left corner of your JFrame.

Be careful when using static variables in your program. There seems to be no reason in setting your Memory class, your width and height variables as static.

For more information regarding this, check out this post: Why are static variables considered evil?

Soutzikevich
  • 991
  • 3
  • 13
  • 29