如何使用 MVC 将图像数组显示到另一帧中

How can I make using the MVC that an array of images show into another frame

提问人:Jlearnnn 提问时间:10/29/2023 更新时间:10/29/2023 访问量:21

问:

我试图将模型展示到名为 GameView 的 vista 中,但需要使用控制器来完成,但我真的在尝试并且无法得到它

package model;

import controller.FirstController;
import java.util.ArrayList;
import java.util.List;



public class GameModel {
    private List<Cubo> squareArray;
    private FirstController myController;
    public GameModel() {
        
        squareArray = new ArrayList<>();
    
        Cubo cubo1 = new Cubo("/images/image1.jpg",100,100);
        squareArray.add(cubo1);
        Cubo cubo2 = new Cubo("/images/image2.jpg",100,100);
        squareArray.add(cubo2);
        Cubo cubo3 = new Cubo("/images/image3.jpg",100,100);
        squareArray.add(cubo3);
        Cubo cubo4 = new Cubo("/images/image4.jpg",100,100);
        squareArray.add(cubo4);
        Cubo cubo5 = new Cubo("/images/image4.jpg",100,100);
        squareArray.add(cubo5);
        Cubo cubo6 = new Cubo("/images/image3.jpg",100,100);
        squareArray.add(cubo6);
        Cubo cubo7 = new Cubo("/images/image2.jpg",100,100);
        squareArray.add(cubo7);
        Cubo cubo8 = new Cubo("/images/image1.jpg",100,100);
        squareArray.add(cubo8);
    }
       
    public void setFirstController(FirstController myController) {
       this.myController = myController;
  }
    public void setSquareArray(List<Cubo> squareArray){
        this.squareArray= squareArray;
    
    }
    public List<Cubo> getSquareArray(){
          
        return squareArray;}
}

在该部分中,我创建了要添加列表的对象,其中有 10 个对象,每个对象都有一个图像

package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import view.FirstView;
import view.GameView; 
import model.GameModel;
import model.Cubo;

public class FirstController {
    private FirstView frame;
    private GameView game;
    private GameModel model;
    private List<Cubo> squareArray;
    
    
    public FirstController() {
        this.frame=frame;
        this.game=game;
   }    

    public void setFirstView(FirstView frame) {
        this.frame=frame;
    }

    public void chargeGameFrame() {
        GameView game = new GameView();
        frame.dispose();
        
    }

    public void setGameView(GameView game) {
       this.game=game;
       List<Cubo> squareArray = model.getSquareArray();
    }

    public Object getGameModel() {
        return model;
    }


}        
   

在控制器中,我想创建一条从模型到视图的道路以显示它

package view;

import controller.FirstController;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import model.Cubo; // Import the Square class

public class GameView extends JFrame {
    private FirstController myController;
    private ImageIcon backgroundIcon;
    private JLabel jContent;
    private JPanel gamePanel;
    private List<Cubo> squareArray; // Add squareArray property

    public GameView() {
        startComponent();
    }

    private void startComponent() {
        // Configuration of screen and background--------------------------------------------
        backgroundIcon = new ImageIcon(this.getClass().getResource("/images/background.jpg"));
        jContent = new JLabel(backgroundIcon);
        jContent.setSize(1200, 800);
        setTitle("GAME!!!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 800);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        setLayout(null);
        add(jContent);
        gamePanel = new JPanel();
        gamePanel.setBackground(new java.awt.Color(102, 0, 204));
        gamePanel.setLayout(null);
    }

    public void setFirstController(FirstController myController) {
        this.myController = myController;

    }



    public JPanel getGamePanel() {
        return gamePanel;
    }
}

并在 JFrame 中展示它,但我真的不知道该怎么做,我已经查看了视频,但我没有得到任何信息

java 数组 model-view-controller jframe jpanel

评论


答: 暂无答案