Java sample(A lot of chicken)


run

---(Source List)---

//Example18
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Example18{
  JLabel[] lbl=new JLabel[32];
  int cnt=0;
  //Main
  public static void main(String ar[]){
    Example18 sample=new Example18();
  }
  //constructor
  public Example18(){
    //make frame
    JFrame f=new JFrame("A lot of chicken(Please click!)");
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setLayout(null);
    //make chicken labels
    for(int i=0;i<32;i++){
      String imgname="cut" + (int)(Math.random()*7) + ".gif";
      Icon img=new ImageIcon(imgname);
      lbl[i]=new JLabel("", img, JLabel.LEFT);
      p.add(lbl[i]);
    }
    //add mouselistener
    p.addMouseListener(new myMouseListener());
    //add panel
    f.getContentPane().add(p);
    //show frame
    f.setSize(640,400);
    f.getContentPane().setLayout(new GridLayout(1,1));
    f.setVisible(true);
  }
  //listener receive mouse events
  class myMouseListener extends MouseAdapter{
    public void mouseClicked(MouseEvent me){
      cnt=cnt+1;
      if (cnt==32) cnt=0;
      int x=me.getX();
      int y=me.getY();
      lbl[cnt].setBounds(x-29,y-42,72,88);
    }
  }
}