// // Setting.java // BodiesInASetting // // Created by Guy Lapalme on 20/05/05. // Copyright (c) 2005 Universite de Montreal. All rights reserved. // updated June 2014 // import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.HashMap; import java.util.Map; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; public class Setting extends JPanel{ private Map bodies; private JPopupMenu creationMenu; private int xMouse,yMouse; // CLASS part Setting (){ setBackground(Color.green); bodies = new HashMap(); creationMenu = new JPopupMenu(); creationMenu.add(new JMenuItem("Create")); creationMenu.addSeparator(); addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ xMouse = e.getX(); yMouse = e.getY(); // hack to show all butterflies and flowers when clicking in the top left corner... if(xMouse<5&&yMouse<5) displayAllButterfliesAndFlowers(); AnimatedBody b = findBody(xMouse,yMouse); if(b==null) creationMenu.show(Setting.this,xMouse,yMouse); else b.showMenu(xMouse,yMouse); } }); } // Setting public AnimatedBody findBody(int x, int y){ for(AnimatedBody b: bodies.values()) if(b.contains(xMouse,yMouse))return b; return null; } public AnimatedBody findBody(String name){ return bodies.get(name); } // asking information from the user... public String askString(String prompt){ return JOptionPane.showInputDialog(this,prompt,"",JOptionPane.QUESTION_MESSAGE); } public int askInt(String prompt){ while (true) try{ String s = askString(prompt); return Integer.parseInt(s); } catch (NumberFormatException e){} } public MyPoint askPosition(String prompt){ MyPoint pt = null; while (pt==null){ String s = askString(prompt); pt=MyPoint.parse(s); } return pt; } public AnimatedBody askBody(String prompt){ return findBody(askString(prompt)); } public String askNewBodyName(String prompt){ String bodyName = null; while(bodyName==null || bodyName.length()==0 || bodies.get(bodyName)!=null) bodyName = JOptionPane.showInputDialog(Setting.this, prompt, "", JOptionPane.QUESTION_MESSAGE); return bodyName; } // INSTANCE part // instance variables accessor methods // instance methods public void registerAnimatedBody(final Class c){ JMenuItem item = new JMenuItem(c.getName()); item.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String bodyName = askNewBodyName("Enter name of new "+c.getName()); MyPoint pos = new MyPoint(xMouse,yMouse); try { AnimatedBody b = (AnimatedBody) c.newInstance();// creation of a new body b.setName(bodyName);// initialisation of fields b.setPosition(pos); bodies.put(bodyName,b);// add to the known bodies b.trace("created"); repaint(); } catch (Exception ie){ System.out.println(ie+"** could not create "+c.getName()+ "("+bodyName+","+pos+")"); } } }); creationMenu.add(item); } // for animations public void paintComponent(Graphics g) { super.paintComponent(g); int width = getWidth(); int height = getHeight(); // draw border g.setColor(Color.black); g.fillRect(0,0, width, height); g.setColor(getBackground()); g.fillRect(5,5,width - 10, height - 10); // draw bodies for(AnimatedBody b:bodies.values()) b.display(g); // cheap trick to slow the animations... try{Thread.sleep(100);}catch (InterruptedException e){}; } // paint method public void paintImmediately(){ paintImmediately(0,0,getWidth(),getHeight()); } public void displayAllButterfliesAndFlowers(){ // show all directions and aspects of Flowers and Butterflies // only useful for creating figure 2 of the document... bodies.clear(); Butterfly b = new Butterfly("b",new MyPoint(0,0)); int nbAspects = b.getNbAspects(); int nbDirections = b. getNbDirections(); for (int a=0;a < nbAspects;a++){ for (int d=0;d