[英]How to use MVC and paintComponent to create a setBackground method in the view class for my platformer?
这是我的观点
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()
{
}
}
这是我的Model类,基本上只读取图像
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");
}
}
}
现在,我想向我的视图类中添加setBackground方法,该方法将在初始化级别时由控制器调用,并将接收模型读取的图像。 但是我对如何做到这一点感到困惑,因为我确定我必须使用paintComponent然后绘制我已阅读的图像。 但是我不确定如何继续编写setBackground方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.