0

this is my viewclass

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.*;

    public class View 
    {
        JFrame frame;
        JPanel background;


        public View() 
        {
            frame = new JFrame("Platformer");
            background = new JPanel(new BorderLayout());

            BuildMenu();
            SetBackground();
            BuildLowerPanel();

            frame.getContentPane().add(background);
            frame.setSize(800, 700);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        }

        private void BuildMenu() 
        {           
            JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
        JMenuItem i = new JMenuItem("Resume");
        i.setActionCommand("RESUME");
        JMenuItem j = new JMenuItem("Pause");
        j.setActionCommand("PAUSE");
        menu.add(i);
        menu.add(j);
        }

        public void paintComponent(Graphics g)
        {


        }

        private void BuildLowerPanel()
        {

        }
    }

this is my Model class which essentially only reads in the image

    package introGUI;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;

    public class Model 
    {
        private BufferedImage img;

        public void ReadImage()
        {
            try
            {
                img = ImageIO.read(new              File("C:\\Users\\p333kle\\Pictures\\lvl1.jpg"));
            }
            catch(IOException e)
            {
                System.err.println("Error: Couldn't load background image");
            }
        }

}

Right now i want to add a setBackground method to my view class which will be called by the controller when a level is initialized, and will take in the image read by the model. However i am confused as to how i will be doing this, because i know for sure i have to use paintComponent and then draw the image i have read. However i am unsure of how to proceed writing the setBackground method.

Stephen Fuhry
  • 12,624
  • 6
  • 56
  • 55

1 Answers1

1

As illustrated here, use the observer pattern to let your model notify the view when a level change has been initiated in the model. In the particular case of a platform game, the model can manage the level, perhaps identified by an enum value, while the view can choose the matching background, perhaps chosen from a related set. See RCImage in the example cited.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045