所以,经过一段时间的努力,试图弄清楚为什么我的JPanel在移动我的鼠标时闪烁,我发现根本不是这样的。根据我的发现,从更改绘制图像的顺序,是在绘制所有内容之前绘制缓冲图像。我只能假设移动我的鼠标以某种方式导致在渲染周期结束之前绘制图像。我将所有内容绘制到一个单独的图像,覆盖JPanels paintComponent方法,然后让JFrame绘制面板。移动框架或调整大小会导致同样的事情,但我不担心这些。
public void render() {
Graphics g = screen.getGraphics();
// State based system, renders all subsequent images
Manager.render(g);
// For fading between state changes
g.setColor(new Color(0, 0, 0, Variables.alpha));
g.fillRect(0, 0, Variables.frame.getWidth(), Variables.frame.getHeight());
// Only way that worked to get the frame to update the JPanel
Variables.frame.repaint();
}
public static void generateFrame(String title, int width, int height, boolean resize, KeyListener keys,
MouseListener mouse, MouseMotionListener mMotion, MouseWheelListener mWheel) {
frame = new JFrame(title);
frame.setResizable(resize);
frame.addKeyListener(keys);
frame.addMouseListener(mouse);
frame.addMouseMotionListener(mMotion);
frame.addMouseWheelListener(mWheel);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
shutdown();
}
});
pane = new JPanel() {
private static final long serialVersionUID = -7796800583217917918L;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(Variables.screen, -Variables.xOffset, -Variables.yOffset,
Variables.frame.getWidth() + Variables.yOffset * 2,
Variables.frame.getHeight() + Variables.xOffset * 2, null);
}
};
frame.add(Variables.pane);
frame.pack();
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}