paintComponent 在绘制图像时清除 JPanel
paintComponent clears the JPanel when paint an image
我有这个代码:
package game;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw {
Object block;
public Draw(JFrame frame, Object object) {
this.block = object;
JPanel pane = new JPanel() {
private static final long serialVersionUID = 3869097656854760151L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
g.drawImage(ImageIO.read(new File(object.getTexture())), object.getX(), object.getY(), object.getWidth(),
object.getHeight(), null);
} catch (IOException e) {
System.err.println("Image '" + object.getTexture() + "' could not be found!");
}
}
};
frame.add(pane);
}
}
我在这里调用 class:
package game;
import javax.swing.JFrame;
public class Frame {
private final int X_SIZE = new vena.util.Computer().screenWidth();
private final int Y_SIZE = new vena.util.Computer().screenHeight();
public Frame() {
JFrame f = new vena.util.Frame().frame("2D Game", X_SIZE - X_SIZE / 5, Y_SIZE - Y_SIZE / 5, true, false, false,
"res/icon.png");
new Draw(f, new Object(0, 0, 100, 100, "grass"));
new Draw(f, new Object(100, 0, 100, 100, "grass"));
f.setVisible(true);
}
public static void main(String[] args) {
new Frame();
}
}
当我用
调用图像时它会呈现
new Draw(f, new Object(0, 0, 100, 100, "grass"));
但是当我下次调用图像时,在它旁边
new Draw(f, new Object(100, 0, 100, 100, "grass"));
它只渲染第二张图片,并删除第一张。我注意到当我在 paintComponent 方法中调用 g.drawImage() 两次时不会发生这种情况。有没有办法让我可以根据需要多次调用 Draw class,而无需清除 JPanel?
框架内容窗格的默认布局管理器是 BorderLayout。当您将组件添加到 BorderLayout 并且您没有指定约束时,组件将转到 CENTER。 CENTER中只能显示最后添加的组件。
如果您希望框架上有多个组件,那么您可以更改布局管理器。尝试
f.setLayout( new FlowLayout() );
看看区别。
I have noticed that this doesn't occur when I call g.drawImage() twice in the paintComponent method.
是的,如果您尝试在框架的特定位置绘制图像,那么您真的应该重写 paintComponent() 来绘制每个图像。
我有这个代码:
package game;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw {
Object block;
public Draw(JFrame frame, Object object) {
this.block = object;
JPanel pane = new JPanel() {
private static final long serialVersionUID = 3869097656854760151L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
g.drawImage(ImageIO.read(new File(object.getTexture())), object.getX(), object.getY(), object.getWidth(),
object.getHeight(), null);
} catch (IOException e) {
System.err.println("Image '" + object.getTexture() + "' could not be found!");
}
}
};
frame.add(pane);
}
}
我在这里调用 class:
package game;
import javax.swing.JFrame;
public class Frame {
private final int X_SIZE = new vena.util.Computer().screenWidth();
private final int Y_SIZE = new vena.util.Computer().screenHeight();
public Frame() {
JFrame f = new vena.util.Frame().frame("2D Game", X_SIZE - X_SIZE / 5, Y_SIZE - Y_SIZE / 5, true, false, false,
"res/icon.png");
new Draw(f, new Object(0, 0, 100, 100, "grass"));
new Draw(f, new Object(100, 0, 100, 100, "grass"));
f.setVisible(true);
}
public static void main(String[] args) {
new Frame();
}
}
当我用
调用图像时它会呈现new Draw(f, new Object(0, 0, 100, 100, "grass"));
但是当我下次调用图像时,在它旁边
new Draw(f, new Object(100, 0, 100, 100, "grass"));
它只渲染第二张图片,并删除第一张。我注意到当我在 paintComponent 方法中调用 g.drawImage() 两次时不会发生这种情况。有没有办法让我可以根据需要多次调用 Draw class,而无需清除 JPanel?
框架内容窗格的默认布局管理器是 BorderLayout。当您将组件添加到 BorderLayout 并且您没有指定约束时,组件将转到 CENTER。 CENTER中只能显示最后添加的组件。
如果您希望框架上有多个组件,那么您可以更改布局管理器。尝试
f.setLayout( new FlowLayout() );
看看区别。
I have noticed that this doesn't occur when I call g.drawImage() twice in the paintComponent method.
是的,如果您尝试在框架的特定位置绘制图像,那么您真的应该重写 paintComponent() 来绘制每个图像。