Ewe*_*wen 3 java swing jframe paintcomponent
我创建了一个扩展a JFrame并JPanel在其中添加内部的类,但该paintComponents()方法不会在其上绘制任何内容JPanel.继承人的代码paintComponents(),我选择使用双重图像缓冲.
public void paintComponents(Graphics graphics) {
panel.paintComponents(graphics);
bufferedImage = createImage(sizeX, sizeY);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
for (ImageData myImage : imageData) {
g.drawImage(myImage.getImage(), myImage.getX(), myImage.getY(), null);
}
graphics.drawImage(bufferedImage, 0, 0, null);
}
Run Code Online (Sandbox Code Playgroud)
这有什么不对吗?顺便说一句,我尝试过paint()它但是我认为这不是正确的方法.
谢谢你的时间.:)
不要扩展顶级组件,如a JFrame.而是保留一个框架的实例,并添加一个面板.所有自定义绘画或组件的添加都在面板中完成.
当面板做的风俗画,做的 paintComponent方法(不 paintComponents不重写).
super.paintComponent(g);.这对于确保考虑边界和填充等很重要.null的this.每一个JComponent都是ImageObserver.