我想要做的是绘制图像,然后我使用此方法隐藏并再次绘制鼠标所在的图像。
我面临的问题是,我仍然从旧图像中得到一些小东西,它并没有完全隐藏图片,当我尝试使用不同的方法,如“KeyPressed”时,一切运行良好。
这是做鼠标事件的正确方法吗?或者此代码不正确?
public class GameFrame extends javax.swing.JFrame implements java.awt.event.ActionListener
{
private java.util.Vector<Milk> foodList = new java.util.Vector<Milk>();
private javax.swing.Timer moveTimer = new javax.swing.Timer(500, this);
private MotherFigure motherFig;
private BabyFigure babyFig;
private FastFood burgerFig;
private int speedTrack = 200;
private int milkCount = 1;
@Override
public void actionPerformed(ActionEvent ae)
{
motherFig.draw();
babyFig.draw();
moveFood();
collided();
motherFig.hide();
}
/**
* Creates new form GameFrame
*/
public GameFrame()
{
initComponents();
motherFig = new MotherFigure(gamePanel);
babyFig = new BabyFigure(gamePanel);
burgerFig = new FastFood(gamePanel);
moveTimer.start();
gamePanel.requestFocus();
}
/**
* This method is called from within the constructor to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
gamePanel = new javax.swing.JPanel();
txtCount = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
getContentPane().setLayout(null);
gamePanel.setBackground(new java.awt.Color(255, 255, 255));
gamePanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
gamePanelMouseMoved(evt);
}
});
gamePanel.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
gamePanelFocusGained(evt);
}
});
gamePanel.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
gamePanelKeyPressed(evt);
}
});
txtCount.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
txtCount.setBorder(null);
txtCount.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtCount.setSelectionColor(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel);
gamePanel.setLayout(gamePanelLayout);
gamePanelLayout.setHorizontalGroup(
gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gamePanelLayout.createSequentialGroup()
.addContainerGap(596, Short.MAX_VALUE)
.addComponent(txtCount, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
gamePanelLayout.setVerticalGroup(
gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(gamePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(txtCount, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(545, Short.MAX_VALUE))
);
getContentPane().add(gamePanel);
gamePanel.setBounds(80, 10, 700, 580);
pack();
}// </editor-fold>
private void gamePanelKeyPressed(java.awt.event.KeyEvent evt)
{
if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_LEFT)
motherFig.move(-10, 0); //babyFig.move(-10, 0);
else if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_RIGHT)
motherFig.move(10, 0); //babyFig.move(10, 0);
motherFig.draw();
}
private void gamePanelFocusGained(java.awt.event.FocusEvent evt)
{
// TODO add your handling code here:
txtCount.setText("Milk Catched: ");
Milk b = new Milk(gamePanel);
b.draw();
foodList.add(b);
}
private void gamePanelMouseMoved(java.awt.event.MouseEvent evt) {
motherFig.hide();
int X = evt.getX();
int Y = evt.getY();
motherFig.move(X, Y);
motherFig.reDraw(X, Y);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new GameFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel gamePanel;
private javax.swing.JTextField txtCount;
// End of variables declaration
private void moveFood()
{
for (int i = 0; i < foodList.size(); i++)
{
foodList.get(i).hide();
}
for (int i = 0; i < foodList.size(); i++)
{
foodList.get(i).move();
}
for (int i = 0; i < foodList.size(); i++)
{
foodList.get(i).draw();
}
}
答案 0 :(得分:2)
getGraphics()
,然后在它上面绘制,因为这样获得的Graphics对象不会持久存在,你的绘图也不会... paintComponent(Graphics g)
方法覆盖中绘制。 super.paintComponent(g);
,通常是在方法覆盖的第一行。这将删除任何旧图像。