JAVA Program on JFrame

//Java Program to display a moving rectangle.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MovingRectangle extends JPanel implements ActionListener
 {

  int x, y;
  Timer timer;
  
  MovingRectangle() 
  {
    x = 0;
    y = 0;
    timer = new Timer(20, this);
  }
  
  public void actionPerformed(ActionEvent e) 
  {
    x += 1;
    y += 1;
    repaint();
  }
  
  public void paintComponent(Graphics g)
   {
    super.paintComponent(g);
    g.fillRect(x, y, 10, 10);
  }
  
  public static void main(String[] args)
   {
    JFrame f = new JFrame("Moving");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MovingRectangle m = new MovingRectangle();
    f.add(m);
    f.setSize(500, 500);
    f.setVisible(true);
    m.timer.start();
  }
}
  




Comments

Popular posts from this blog

IT resources @ DAIICT

I blinked and the day passed ...

The world is what it is