import java.awt.*; import java.awt.event.*; import java.util.*; public class OrigamiApplet extends java.applet.Applet { Origami origami; public void init() { setBackground(Color.white); origami=new Origami(); } public void destroy() { origami.frame.dispose(); } } class Origami extends Canvas implements MouseListener,MouseMotionListener,ItemListener,ActionListener,WindowListener { Frame frame=new Frame("おりがみ"); Menu general=new Menu("おりがみ"), oru=new Menu("おる"), iroerabi=new Menu("いろ"); MenuItem sara=new MenuItem("かみをかえる"), yameru=new MenuItem("やめる"), modosu=new MenuItem("もとい"), yamaori=new MenuItem("やまおり"), taniori=new MenuItem("たにおり"), orime=new MenuItem("おりめをつける"), migi=new MenuItem("みぎにまわす"), hidari=new MenuItem("ひだりにまわす"), ura=new MenuItem("うらがえす"); CheckboxMenuItem grid=new CheckboxMenuItem("きちっと おる"), ugokasu=new CheckboxMenuItem("うごかす"); MenuBar menubar=new MenuBar(); Image offscreen; Graphics og; Vector kasanari,motoi; Point from,to,right,left; Color iro=new Color(0x66cc99); Hashtable colortable=new Hashtable(); static public void main(String[] args) { new Origami(); } public Origami() { addMouseListener(this); addMouseMotionListener(this); init(); init(); modosu.setEnabled(false); sara.addActionListener(this); yamaori.addActionListener(this); taniori.addActionListener(this); orime.addActionListener(this); modosu.addActionListener(this); migi.addActionListener(this); hidari.addActionListener(this); ura.addActionListener(this); yameru.addActionListener(this); setColor("あんずいろ",0xff9900); setColor("カプチーノ",0x996633); setColor("くもいろ",0xcccccc); setColor("すみいろ",0x333333); setColor("ミント",0x66cc99); setColor("モスグリーン",0x669966); setColor("よあけいろ",0xff6666); setColor("ラベンダー",0x6666cc); general.add(sara); general.addSeparator(); general.add(iroerabi); general.add(grid); general.addSeparator(); general.add(yameru); oru.add(modosu); oru.addSeparator(); oru.add(taniori); oru.add(yamaori); oru.add(orime); oru.addSeparator(); oru.add(ura); oru.add(ugokasu); oru.add(migi); oru.add(hidari); menubar.add(general); menubar.add(oru); frame.setMenuBar(menubar); frame.add(this,BorderLayout.CENTER); frame.addWindowListener(this); frame.setSize(360,360); frame.show(); } void init() { motoi=kasanari; kasanari=new Vector(); kasanari.addElement(new Kami(-128,-128,256,256)); from=to=right=left=null; } void setColor(String name,int color) { MenuItem item=new MenuItem(name); colortable.put(item,new Color(color)); item.addActionListener(this); iroerabi.add(item); } void attract(Point p) { double nearest=10; int x=p.x,y=p.y; for (int i=0;i=0 && t<=1 && Math.abs(s)*Math.sqrt(u*u+v*v)=0;i--) { double max=-Double.MAX_VALUE,min=Double.MAX_VALUE; Kami kami=(Kami)kasanari.elementAt(i); for (int j=0;j-0.01 && t<1.01) { if (s>max) max=s; if (s1) { source=kami; break; } } if (source!=null) apply(source,(from.x+to.x)/2,(from.y+to.y)/2,-v0,u0); double max=-Double.MAX_VALUE,min=Double.MAX_VALUE; for (int i=0;i=0 && t<=1) { if (s>max) max=s; if (s-Double.MAX_VALUE && min=0;i--) { Kami mirror=(Kami)fold.elementAt(i); if (mirror!=null) successor.addElement(mirror.reverse(right.x,right.y,left.x,left.y)); } else for (int i=0;i=0;i--) { Kami kami=(Kami)kasanari.elementAt(i); successor.addElement(kami.reverse(0,100,0,-100)); } for (int i=0;i=0;i--) ((Kami)kasanari.elementAt(i)).translate(to.x-from.x,to.y-from.y); from.setLocation(to); right=left=null; } else { if (grid.getState()) attract(to); drawLine(); } repaint(); } public void mouseMoved(MouseEvent evt) {} public void actionPerformed (ActionEvent evt) { if (evt.getSource()==sara) init(); else if (evt.getSource()==taniori) fold(true,true); else if (evt.getSource()==yamaori) fold(true,false); else if (evt.getSource()==orime) fold(false,false); else if (evt.getSource()==modosu) { Vector temp=kasanari; kasanari=motoi; motoi=temp; } else if (evt.getSource()==migi) turn(true); else if (evt.getSource()==hidari) turn(false); else if (evt.getSource()==ura) reverse(); else if (evt.getSource()==yameru) try { System.exit(0); } catch (Exception e) { frame.dispose(); } else if (colortable.containsKey(evt.getSource())) iro=(Color)colortable.get(evt.getSource()); repaint(); } public void itemStateChanged(ItemEvent evt) {} public void paint(Graphics g) { offscreen=createImage(getSize().width,getSize().height); og=offscreen.getGraphics(); og.translate(getSize().width/2,getSize().height/2); update(g); } public void update(Graphics g) { try { og.clearRect(-getSize().width/2,-getSize().height/2,getSize().width,getSize().height); for (int i=0;i=0 && t<1) { if (s>max) { max=s; imax=i; } if (s=0 && imin>=0 && imin!=imax) { Kami cut=new Kami((imin-imax+npoints)%npoints+2); cut.xpoints[0]=x0+(int)(max*u0); cut.ypoints[0]=y0+(int)(max*v0); cut.extention[0]=extention[imax]; for (int i=1;i0) return next=null; return next=mirror(); } } Kami reverse(int x0,int y0,int x1,int y1) { Kami reverse=new Kami(npoints); int u=x1-x0,v=y1-y0; for (int i=0;i=0 && t>=0 && s<1 && t<1 && (xpoints[i]+s*u0-x)*v-(ypoints[i]+s*v0-y)*u<0) return true; } } return false; } }