• fullscreen
  • RectClass.pde
  • test.pde
  • 
    public class RectClass{
      
      private float rectWidth = 20;
      private float gPosX,gPosY;
    
      RectClass(float posX){
    
        gPosX = posX;
        gPosY = height/2;
     
      }
      
      void rectDraw(){
        fill(255,0,0);
        rect(gPosX,gPosY,rectWidth,rectWidth);
      }
      
      
      void updata(){
        
        float distance = dist(gPosX,gPosY,mouseX,mouseY);
        
        if(distance<20 && dragFlag==true){
          gPosX = mouseX;
          gPosY = mouseY;
        }
      
      }
    
    
    
    }
    
    
    ArrayList rectArray;
    RectClass workobj;
    
    boolean dragFlag = false;
    
    
    void setup() {
      
      size(200, 200);
      smooth();
      noStroke();
    
      // 空の動的配列を作成
      rectArray = new ArrayList();
      
      
    }
    
    void draw() {
      background(255);
     
      for (int i = 0; i < rectArray.size(); i++){
          ((RectClass)rectArray.get(i)) .rectDraw();
          ((RectClass)rectArray.get(i)) .updata();
      }
        
       println(rectArray.size());
    
    }
    
    void keyPressed(){
    
      if(key == CODED){
          if (keyCode == RIGHT){
            rectArray.add(new RectClass(30 *(rectArray.size())));  //配列の要素(RectClassオブジェクト)を追加
          }
          
          if (keyCode == LEFT){
              if(rectArray.size() > 0){  //0以下だとエラーだから
                rectArray.remove(rectArray.size() -1);  //配列の一番後ろを消す。0番目があるから必ず-1
              }
          }
          
      }
    
    }
    
    void mouseDragged(){
      dragFlag = true;
    }
    
    void mouseReleased(){
      dragFlag = false;
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    creators-high

    ArrayList test

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    Guigui plus+
    6 Aug 2010
    Hi, here you can see my own previous ArrayList test:

    http://www.openprocessing.org/visuals/?visualID=2065

    8^)
    Nathan Nifong
    6 Aug 2010
    An ArrayList test is a boring thing to see. But at least your code looks clean. Keep it up
    You need to login/register to comment.