关于JPanel的加载问题;
请各位大神看一下下面的这个绘制一个地图的代码,为什么在第46行override的方法在没有引用,更没有传入参数的情况下被自动调用了,这个是什么机理?是某一个父类的构造器自动引用了么?甚至还自动传入了一个Graphics的参数?
程序代码:
import java.awt.Container; import java.awt.Graphics; import java.awt.image.BufferedImage; import import javax.imageio.ImageIO; import javax.swing.*; public class Maze extends JFrame { public Maze(){ setSize(510,550); setDefaultCloseOperation(3); Container cont = this.getContentPane(); MapImage map = new MapImage(); add(map); setVisible(true); } public static void main(String[] args) { new Maze(); } } class MapImage extends JPanel{ private BufferedImage wall; private int[][] map = new int[][]{ {2,1,1,1,1,1,1,1,1,1}, {0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,0,1}, {1,0,0,0,0,0,1,1,0,1}, {1,0,1,0,1,1,1,1,0,1}, {1,0,1,0,0,0,0,0,0,1}, {1,0,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,0,3}, {1,1,1,1,1,1,1,1,1,1}}; public void paint(Graphics g){ try { //注意下面的导入方式;很有用;在程序移植过程中,通过相对路径可以保证资源的正常加载 wall= ImageIO.read(this.getClass().getResource("/Material/brick.png")); for(int i = 0;i<10;i++){ for(int j=0;j<10;j++){ if(this.map[i][j]==1){ g.drawImage(wall,51*i,51*j,null); } } } } catch (IOException e) { e.printStackTrace(); } } }