首页 > Sktech > 习作BI_切换层叠顺序的方块
2017
10-31

习作BI_切换层叠顺序的方块

习作BI_切换层叠顺序的方块 - 第1张  | Processing编程艺术


练习散列表(?)和迭代器(?)的用法。
被选中的方块可拖动。被选中的方块会被切换到最下层,但点击只会选中最上层的方块。


– 问题:只通过散列表方法values() 返回的集合实例没有办法改变元素排列?
– 问题:用迭代器实现反向迭代必须先正向迭代把位置移到末尾?


[鼠标右键]:选择方块
[Q]:退出程序



//--
import java.util.*;
//--
int pbFocus=0;
String pbInfo=null;
//--
HashMap<Integer, EcElement> pbTheMap=new HashMap<Integer, EcElement>();
ArrayList<EcElement> pbTheList=new ArrayList<EcElement>();
//--
EcElement pbMyElementAD=new EcElement(60,40,"-D",4013);
EcElement pbMyElementAC=new EcElement(60,40,"-C",4012);
EcElement pbMyElementAB=new EcElement(60,40,"-B",4011);
EcElement pbMyElementAA=new EcElement(60,40,"-A",4010);
//--


void setup(){
  size(320,240);textAlign(LEFT,TOP);
  //--
  pbTheMap.put(pbMyElementAD.cmID,pbMyElementAD);pbTheList.add(pbMyElementAD);
  pbTheMap.put(pbMyElementAC.cmID,pbMyElementAC);pbTheList.add(pbMyElementAC);
  pbTheMap.put(pbMyElementAB.cmID,pbMyElementAB);pbTheList.add(pbMyElementAB);
  pbTheMap.put(pbMyElementAA.cmID,pbMyElementAA);pbTheList.add(pbMyElementAA);
  //--
  pbInfo="[MouseRightButton]to select..\n[MouseDrag]to drag while selected..\n[Q]to quit..";
  //--
}//+++


void draw(){
  background(0);
  //--
  /* usually an inhenced for loop
   ____________________________________________*/
  //for(EcElement it:pbTheList){it.ccUpdate();}
  //--
  /* alternately an iterator looping backward
   ____________________________________________*/
  ListIterator<EcElement> it=pbTheList.listIterator();
  while(it.hasNext()){it.next();}
  while(it.hasPrevious()){
    EcElement lpElement=it.previous();
    lpElement.ccUpdate();
  }
  //--
  fill(0x77,0xEE,0x77);fnTextValue("focus:",pbFocus,12,0);
  text(pbInfo,5,5);
  //--
}//+++


void keyPressed(){switch(key){ 
  case 'q':exit();break;
}}//+++


void mousePressed(){if(mouseButton==RIGHT){
  int lpID=0;
  //for(int i=pbTheList.size()-1;i>=0;i--){
  for(EcElement lpElement:pbTheList){
    //EcElement lpElement=pbTheList.get(i);
    lpID=lpElement.ccCheckMouseID();
    if(lpID!=0){break;}
  }
  pbFocus=lpID;
  for(EcElement it:pbTheList){it.cmFocused=false;}
  if(pbTheMap.containsKey(pbFocus)){
    EcElement lpElement=pbTheMap.get(pbFocus);
    lpElement.cmFocused=true;
  }
  fsShiftFocus();
}}//+++


void mouseDragged(){
  Integer lpID=pbFocus;
  if(pbTheMap.containsKey(lpID)){
    EcElement lpElement = pbTheMap.get(lpID);
    lpElement.cmX=mouseX;
    lpElement.cmY=mouseY;
  }
}//+++


void fsShiftFocus(){
  Integer lpID=pbFocus;
  if(pbTheMap.containsKey(lpID)){
    EcElement lpElement = pbTheMap.get(lpID);
    if(pbTheList.contains(lpElement)){
      pbTheList.remove(lpElement);
      pbTheList.add(lpElement);
    }
  }
}//+++


void fnTextValue(String pxLable, int pxVal, int pxLine, int pxPage){
  text(pxLable+nf(pxVal,4),pxPage*160+16,pxLine*16);
}//+++


class EcElement{
  int cmX, cmY, cmW, cmH;
  int cmID;
  String cmName;
  boolean cmFocused;
  EcElement(int pxW, int pxH,String pxName ,int pxID){
    cmX=ceil(random(9,99));
    cmY=ceil(random(9,99));
    cmW=pxW;
    cmH=pxH;
    cmID=pxID;
    cmName=pxName;
    cmFocused=false;
  }
  void ccUpdate(){
    fill(cmFocused?0xEE:0xAA);rect(cmX, cmY, cmW, cmH);
    fill(0x33);text(cmName,cmX+2,cmY+2);
  }
  boolean ccIsMouseOver(){
    return (mouseX>cmX)&&(mouseY>cmY)&&(mouseX<(cmX+cmW))&&(mouseY<(cmY+cmH));
  }
  int ccCheckMouseID(){
    return ccIsMouseOver()?cmID:0;
  }
  //--
}//+++

 



最后编辑:
作者:constrain
constrain
nullpointerexception

留下一个回复

你的email不会被公开。