class GraphPanel extends JPanel
{
private int IntMove = 100; public GraphPanel()
{
setSize(GraphPanel_WIDTH, GraphPanel_HEIGHT); setBackground(Color.BLACK); } class MyListenter implements ActionListener 
{
public void actionPerformed(ActionEvent evt)
{
IntMove +=1;
}


 
}//问题出现区域
public void paint(Graphics g)
{

super.paint(g); if (IntMove == 250)
{ g.dispose(); } else
{
g.setColor(Color.RED);
g.drawString("★", 200,100+IntMove); g.setColor(Color.RED);
g.drawString("★", 250, IntMove); MyListenter myListenter = new MyListenter(); Timer myTimer = new Timer(0, myListenter); myTimer.start(); repaint();
}

}
public static final int GraphPanel_WIDTH = 450;
public static final int GraphPanel_HEIGHT =700;
}
----------------------------------------------------想做一个类似下星星的程序,但是遇到一个难题就是星星掉下去后用g.dispose();释放空间,但是所有用g画出来的星星都清除了有什么解决办法,只清除已经掉地的星星,而不影响其他的星星

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主截止到2008-06-19 09:39:34的汇总数据:
    注册日期:2008-4-22
    上次登录:2008-6-19
    发帖数:3
    结贴数:0
    结贴率: 0.00%
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    不用dispose吧?
    判断如果星星在显示区域外就不去paint它
      

  3.   

    明显给你写了个死循环...import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;public class Csdn extends JFrame { public Csdn() {
    this.getContentPane().add(new GraphPanel());
    } class GraphPanel extends JPanel {
    private int IntMoveStar1 = 200; private int IntMoveStar2 = 100; MyListenter myListenter = new MyListenter(); Timer myTimer = new Timer(0, myListenter); public GraphPanel() {
    setSize(GraphPanel_WIDTH, GraphPanel_HEIGHT); setBackground(Color.BLACK); } class MyListenter implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
    IntMoveStar1 += 1;
    IntMoveStar2 += 1;
    }
    } // 问题出现区域
    public void paint(Graphics g) { super.paint(g); if (IntMoveStar2 == 350) { // g.dispose();
    IntMoveStar2 = 100;
    System.out.println("if(IntMoveStar2 == 250)");
    repaint();
    // return;
    } if (IntMoveStar1 == 350) {
    IntMoveStar1 = 200;
    System.out.println("if(IntMoveStar1 == 350)");
    repaint();
    } else {
    g.setColor(Color.RED);
    g.drawString("★", 200, IntMoveStar1); g.setColor(Color.RED);
    g.drawString("★", 250, IntMoveStar2);
    myTimer.start();
    repaint();
    } } public static final int GraphPanel_WIDTH = 450; public static final int GraphPanel_HEIGHT = 700; } public static void main(String[] args) {
    Csdn c = new Csdn();
    c.setVisible(true);
    c.setSize(800, 300);
    c.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    }