0

我想停止执行一个函数来打印我在paintComponent中的内容

main(){
   f(){
      change(list)
      Timer to stop 1 minute and repaint
   }

}

change(){
   repaint();
}
4

1 回答 1

1

我想这里已经介绍过了

摇摆计时器。

public class exampe extends JPanel implements ActionListener {

int x;
int y;
int temp = 0;

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.fillOval(x - 2, y - 2, 4, 4);
}

public void set(int X, int Y) {

    x = X;
    y = Y;
}

public static void main(String args[]) {

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new exampe().createAndDisplayGUI();
        }
    });


 }


 public void createAndDisplayGUI(){


    JFrame frame = new JFrame("TEST");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    exampe ex = new exampe();
    JScrollPane scroll = new JScrollPane(ex);
    frame.getContentPane().add(scroll);
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    Timer PointTimer = new Timer(1000, ex);
    PointTimer.setInitialDelay(1000);
    PointTimer.start();
    System.out.println("started");
}

@Override
public void actionPerformed(ActionEvent e) {

   // set(rand.nextInt(350), rand.nextInt(350));
      set(temp+10,temp+10);
      temp=temp+2;
      repaint();
}
}
于 2012-04-27T21:01:28.900 回答