From: Astrid S. <ast...@us...> - 2007-08-15 21:17:27
|
Update of /cvsroot/arianne/stendhal/tests/data/sprites/monsters In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv6301/tests/data/sprites/monsters Modified Files: AnimationRunner.java CreatureAnimationPreview.java FileTree.java ImageViewerSwing.java Log Message: code beautify Index: CreatureAnimationPreview.java =================================================================== RCS file: /cvsroot/arianne/stendhal/tests/data/sprites/monsters/CreatureAnimationPreview.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** CreatureAnimationPreview.java 14 Aug 2007 21:01:11 -0000 1.10 --- CreatureAnimationPreview.java 15 Aug 2007 21:16:44 -0000 1.11 *************** *** 22,26 **** /** * This method initializes jPanel ! * * @return javax.swing.JPanel */ --- 22,26 ---- /** * This method initializes jPanel ! * * @return javax.swing.JPanel */ *************** *** 40,44 **** /** * This method initializes imageViewerSwing ! * * @return data.sprites.monsters.ImageViewerSwing */ --- 40,44 ---- /** * This method initializes imageViewerSwing ! * * @return data.sprites.monsters.ImageViewerSwing */ *************** *** 54,58 **** /** * This method initializes imageViewerSwing1 ! * * @return data.sprites.monsters.ImageViewerSwing */ --- 54,58 ---- /** * This method initializes imageViewerSwing1 ! * * @return data.sprites.monsters.ImageViewerSwing */ *************** *** 68,72 **** /** * This method initializes imageViewerSwing2 ! * * @return data.sprites.monsters.ImageViewerSwing */ --- 68,72 ---- /** * This method initializes imageViewerSwing2 ! * * @return data.sprites.monsters.ImageViewerSwing */ *************** *** 82,86 **** /** * This method initializes imageViewerSwing3 ! * * @return data.sprites.monsters.ImageViewerSwing */ --- 82,86 ---- /** * This method initializes imageViewerSwing3 ! * * @return data.sprites.monsters.ImageViewerSwing */ *************** *** 96,100 **** /** * This method initializes imageViewerSwing4 ! * * @return data.sprites.monsters.ImageViewerSwing */ --- 96,100 ---- /** * This method initializes imageViewerSwing4 ! * * @return data.sprites.monsters.ImageViewerSwing */ *************** *** 132,140 **** private ImageViewerSwing imageViewerSwing4; ! private AnimationRunner[] animations ; /** * This method initializes jFrame ! * * @return javax.swing.JFrame */ --- 132,140 ---- private ImageViewerSwing imageViewerSwing4; ! private AnimationRunner[] animations; /** * This method initializes jFrame ! * * @return javax.swing.JFrame */ *************** *** 180,184 **** /** * This method initializes jSplitPane ! * * @return javax.swing.JSplitPane */ --- 180,184 ---- /** * This method initializes jSplitPane ! * * @return javax.swing.JSplitPane */ *************** *** 195,199 **** /** * This method initializes jScrollPane ! * * @return javax.swing.JScrollPane */ --- 195,199 ---- /** * This method initializes jScrollPane ! * * @return javax.swing.JScrollPane */ *************** *** 208,212 **** /** * This method initializes jTree ! * * @return data.sprites.monsters.FileTree */ --- 208,212 ---- /** * This method initializes jTree ! * * @return data.sprites.monsters.FileTree */ *************** *** 258,262 **** } - getImageViewerSwing1().setImage(buf); if (animations == null) { --- 258,261 ---- Index: FileTree.java =================================================================== RCS file: /cvsroot/arianne/stendhal/tests/data/sprites/monsters/FileTree.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FileTree.java 11 May 2007 20:24:57 -0000 1.3 --- FileTree.java 15 Aug 2007 21:16:44 -0000 1.4 *************** *** 1,5 **** package data.sprites.monsters; /** ! * */ --- 1,6 ---- package data.sprites.monsters; + /** ! * */ *************** *** 15,187 **** import javax.swing.tree.TreePath; - public class FileTree extends JTree { ! /** ! * */ private static final long serialVersionUID = 1L; ! public FileTree(String path) ! throws FileNotFoundException, SecurityException { ! super((TreeModel)null);// Create the JTree itself ! // Use horizontal and vertical lines ! putClientProperty("JTree.lineStyle", "Angled"); ! // Create the first node ! FileTreeNode rootNode = new FileTreeNode(null, path); ! // Populate the root node with its subdirectories ! rootNode.populateDirectories(true); ! setModel(new DefaultTreeModel(rootNode)); ! // Listen for Tree Selection Events ! addTreeExpansionListener(new TreeExpansionHandler()); ! } ! // Returns the full pathname for a path, or null ! // if not a known path ! public String getPathName(TreePath path) { ! Object o = path.getLastPathComponent(); ! if (o instanceof FileTreeNode) { ! return ((FileTreeNode)o).file.getAbsolutePath(); ! } return null; ! } ! // Returns the File for a path, or null if not a known path ! public File getFile(TreePath path) { ! Object o = path.getLastPathComponent(); ! if (o instanceof FileTreeNode) { ! return ((FileTreeNode)o).file; ! } ! return null; ! } ! // Inner class that represents a node in this ! // file system tree ! protected class FileTreeNode ! extends DefaultMutableTreeNode { ! /** ! * ! */ ! private static final long serialVersionUID = 3223106240309250204L; ! public FileTreeNode(File parent, String name) ! throws SecurityException, FileNotFoundException { ! this.name = name; ! // See if this node exists and whether it ! // is a directory ! file = new File(parent, name); ! if (!file.exists()) { ! throw new FileNotFoundException("File " + name + " does not exist"); ! } ! isDir = file.isDirectory(); ! // Hold the File as the user object. ! setUserObject(file); ! } ! // Override isLeaf to check whether this is a directory ! @Override ! public boolean isLeaf() { ! return !isDir; ! } ! // Override getAllowsChildren to check whether ! // this is a directory ! @Override ! public boolean getAllowsChildren() { ! return isDir; ! } ! // For display purposes, we return our own name public String toString() { return name; } ! // If we are a directory, scan our contents and populate ! // with children. In addition, populate those children ! // if the "descend" flag is true. We only descend once, ! // to avoid recursing the whole subtree. ! // Returns true if some nodes were added ! boolean populateDirectories(boolean descend) { ! boolean addedNodes = false; ! // Do this only once ! if (populated == false) { ! if (interim == true) { ! // We have had a quick look here before: ! // remove the dummy node that we added last time ! removeAllChildren(); ! interim = false; ! } ! String[] names = file.list();// Get list of contents ! // Process the directories ! for (int i = 0; i < names.length; i++) { ! String name = names[i]; ! try { ! // if (d.isDirectory()) { ! FileTreeNode node = ! new FileTreeNode(file, name); ! this.add(node); ! if (descend) { ! node.populateDirectories(false); ! } ! addedNodes = true; ! if (descend == false) { ! // Only add one node if not descending ! break; } ! //} ! //else{ ! ! //} ! } catch (Throwable t) { ! // Ignore phantoms or access problems ! } ! } ! // If we were scanning to get all subdirectories, ! // or if we found no subdirectories, there is no ! // reason to look at this directory again, so ! // set populated to true. Otherwise, we set interim ! // so that we look again in the future if we need to ! if (descend == true || addedNodes == false) { ! populated = true; ! } else { ! // Just set interim state ! interim = true; ! } ! } ! return addedNodes; ! } ! protected File file;// File object for this node ! protected String name;// Name of this node ! protected boolean populated; ! // true if we have been populated ! protected boolean interim; ! // true if we are in interim state ! protected boolean isDir;// true if this is a directory ! } ! // Inner class that handles Tree Expansion Events ! protected class TreeExpansionHandler ! implements TreeExpansionListener { ! public void treeExpanded(TreeExpansionEvent evt) { ! TreePath path = evt.getPath();// The expanded path ! JTree tree = (JTree)evt.getSource();// The tree ! // Get the last component of the path and ! // arrange to have it fully populated. ! FileTreeNode node = ! (FileTreeNode)path.getLastPathComponent(); ! if (node.populateDirectories(true)) { ! ((DefaultTreeModel)tree.getModel()). ! nodeStructureChanged(node); ! } ! } ! public void treeCollapsed(TreeExpansionEvent evt) { ! // Nothing to do ! } ! } ! } \ No newline at end of file --- 16,189 ---- import javax.swing.tree.TreePath; public class FileTree extends JTree { ! /** ! * */ private static final long serialVersionUID = 1L; ! public FileTree(String path) throws FileNotFoundException { ! super((TreeModel) null); // Create the JTree itself ! // Use horizontal and vertical lines ! putClientProperty("JTree.lineStyle", "Angled"); ! // Create the first node ! FileTreeNode rootNode = new FileTreeNode(null, path); ! // Populate the root node with its subdirectories ! rootNode.populateDirectories(true); ! setModel(new DefaultTreeModel(rootNode)); ! // Listen for Tree Selection Events ! addTreeExpansionListener(new TreeExpansionHandler()); ! } ! // Returns the full pathname for a path, or null ! // if not a known path ! public String getPathName(TreePath path) { ! Object o = path.getLastPathComponent(); ! if (o instanceof FileTreeNode) { ! return ((FileTreeNode) o).file.getAbsolutePath(); ! } ! return null; ! } ! // Returns the File for a path, or null if not a known path ! public File getFile(TreePath path) { ! Object o = path.getLastPathComponent(); ! if (o instanceof FileTreeNode) { ! return ((FileTreeNode) o).file; ! } ! return null; ! } ! // Inner class that represents a node in this ! // file system tree ! protected class FileTreeNode extends DefaultMutableTreeNode { ! /** ! * ! */ ! private static final long serialVersionUID = 3223106240309250204L; ! public FileTreeNode(File parent, String name) throws FileNotFoundException { ! this.name = name; ! // See if this node exists and whether it ! // is a directory ! file = new File(parent, name); ! if (!file.exists()) { ! throw new FileNotFoundException("File " + name ! + " does not exist"); ! } ! isDir = file.isDirectory(); ! // Hold the File as the user object. ! setUserObject(file); ! } ! // Override isLeaf to check whether this is a directory ! @Override ! public boolean isLeaf() { ! return !isDir; ! } ! // Override getAllowsChildren to check whether ! // this is a directory ! @Override ! public boolean getAllowsChildren() { ! return isDir; ! } ! // For display purposes, we return our own name public String toString() ! // { return name; } ! // If we are a directory, scan our contents and populate ! // with children. In addition, populate those children ! // if the "descend" flag is true. We only descend once, ! // to avoid recursing the whole subtree. ! // Returns true if some nodes were added ! boolean populateDirectories(boolean descend) { ! boolean addedNodes = false; ! // Do this only once ! if (!populated) { ! if (interim) { ! // We have had a quick look here before: ! // remove the dummy node that we added last time ! removeAllChildren(); ! interim = false; ! } ! String[] names = file.list(); // Get list of contents ! // Process the directories ! for (int i = 0; i < names.length; i++) { ! String name = names[i]; ! try { ! // if (d.isDirectory()) { ! FileTreeNode node = new FileTreeNode(file, name); ! this.add(node); ! if (descend) { ! node.populateDirectories(false); ! } ! addedNodes = true; ! if (!descend) { ! // Only add one node if not descending ! break; ! } ! // } ! // else{ ! // } ! } catch (Throwable t) { ! // Ignore phantoms or access problems ! } ! } ! // If we were scanning to get all subdirectories, ! // or if we found no subdirectories, there is no ! // reason to look at this directory again, so ! // set populated to true. Otherwise, we set interim ! // so that we look again in the future if we need to ! if (descend || !addedNodes) { ! populated = true; ! } else { ! // Just set interim state ! interim = true; ! } ! } ! return addedNodes; ! } ! protected File file; // File object for this node ! protected String name; // Name of this node ! ! protected boolean populated; ! ! // true if we have been populated ! protected boolean interim; ! ! // true if we are in interim state ! protected boolean isDir; // true if this is a directory ! } ! ! // Inner class that handles Tree Expansion Events ! protected class TreeExpansionHandler implements TreeExpansionListener { ! public void treeExpanded(TreeExpansionEvent evt) { ! TreePath path = evt.getPath(); // The expanded path ! JTree tree = (JTree) evt.getSource(); // The tree ! ! // Get the last component of the path and ! // arrange to have it fully populated. ! FileTreeNode node = (FileTreeNode) path.getLastPathComponent(); ! if (node.populateDirectories(true)) { ! ((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node); ! } ! } ! ! public void treeCollapsed(TreeExpansionEvent evt) { ! // Nothing to do ! } ! } ! } Index: AnimationRunner.java =================================================================== RCS file: /cvsroot/arianne/stendhal/tests/data/sprites/monsters/AnimationRunner.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** AnimationRunner.java 14 Aug 2007 21:01:11 -0000 1.3 --- AnimationRunner.java 15 Aug 2007 21:16:44 -0000 1.4 *************** *** 1,5 **** package data.sprites.monsters; - import java.awt.event.ActionEvent; import java.awt.event.ActionListener; --- 1,4 ---- *************** *** 8,54 **** import javax.swing.Timer; ! ! public class AnimationRunner implements ActionListener { Timer timer; BufferedImage[] frames; ! ImageViewerSwing ivs ; int currentframe; private int number_of_frames; public AnimationRunner(ImageViewerSwing ivs) { ! this.ivs = ivs; ! timer = new Timer(200, this); } - //Set up the components in the GUI. - - public synchronized void startAnimation(BufferedImage[] frames) { - this.frames= frames; - number_of_frames = frames.length; - timer.start(); ! } ! public synchronized void stopAnimation() { ! timer.stop(); - } public void actionPerformed(ActionEvent e) { ivs.setImage(frames[currentframe]); currentframe++; ! if (currentframe==number_of_frames){ ! currentframe=0; } } ! public void tearDown(){ timer.removeActionListener(this); ! timer=null; } ! } --- 7,56 ---- import javax.swing.Timer; ! public class AnimationRunner implements ActionListener { Timer timer; + BufferedImage[] frames; ! ! ImageViewerSwing ivs; int currentframe; + private int number_of_frames; + public AnimationRunner(ImageViewerSwing ivs) { ! this.ivs = ivs; ! timer = new Timer(200, this); } + // Set up the components in the GUI. ! public synchronized void startAnimation(BufferedImage[] frames) { ! this.frames = frames; ! number_of_frames = frames.length; ! timer.start(); ! } + public synchronized void stopAnimation() { + timer.stop(); + } public void actionPerformed(ActionEvent e) { ivs.setImage(frames[currentframe]); currentframe++; ! if (currentframe == number_of_frames) { ! currentframe = 0; } } ! ! public void tearDown() { timer.removeActionListener(this); ! timer = null; } ! } Index: ImageViewerSwing.java =================================================================== RCS file: /cvsroot/arianne/stendhal/tests/data/sprites/monsters/ImageViewerSwing.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ImageViewerSwing.java 11 May 2007 20:24:57 -0000 1.3 --- ImageViewerSwing.java 15 Aug 2007 21:16:44 -0000 1.4 *************** *** 1,5 **** package data.sprites.monsters; - import java.awt.Dimension; import java.awt.Graphics; --- 1,4 ---- *************** *** 12,181 **** /** * Component for viewing an Image. Simplified version of Symantec ImageViewer ! * displays an Image. Does not resize the image, though may crop it. ! * Thus always maintains original magnification and aspect ratio. ! * Similar to IconImage. * @author Roedy Green */ ! public class ImageViewerSwing extends JComponent ! { ! /** * */ private static final long serialVersionUID = 8190301649166149694L; ! /** ! * default Constructor ! */ ! public ImageViewerSwing() ! { ! setImage(Toolkit.getDefaultToolkit().getImage("data/sprites/ideas/admin.png")); ! initialize(); ! } ! /** ! * Constructor with Image. ! * ! * @param image the Image to be displayed . ! * See the Java glossary under Image for ways to create an Image from a file. ! */ ! public ImageViewerSwing( Image image ) ! { ! this(); ! setImage( image ); ! } ! /** ! * This method initializes this ! * ! */ ! private void initialize() { ! this.setSize(new Dimension(20,20)); ! ! } ! /** ! * Set or change the current Image to display. ! * setImage does a MediaTracker to ensure the Image is loaded. ! * You don't have to. ! * If you don't plan to use the old image again you should ! * do a getImage().flush(); ! * ! * @param image the new Image to be displayed. ! * If the image jpg may have recently changed, don't use ! * getImage to create it, use ! * URL.openConnection() ! * URLConnection.setUseCaches( false ) ! * Connection.getContent ! * Component.createImage ! * ! */ ! public void setImage( Image image ) ! { ! // even if Image object is same, we use it since it may have changed state. ! this.image = image; ! if ( image != null ) ! { ! MediaTracker tracker; ! try ! { ! // wait until image is fully loaded. ! // and so that paint will be instantaneous, rather than gradual as ! // the image arrives. ! // MediaTracker notifies of progress via our Component.ImageObsever interface ! tracker = new MediaTracker( this ); ! tracker.addImage( image, 0 ); ! tracker.waitForID( 0 ); ! } ! catch ( InterruptedException e ) ! { ! } ! } ! // image is now ready, let's paint it ! repaint(); ! } ! /** ! * Get the Image currently being displayed. ! * ! * @return the Image currently displayed or null if no Image ! */ ! public Image getImage() ! { ! return image; ! } ! /** ! * Paints this component using the given graphics context. ! * ! * @param g Graphics context where to paint, e.g. to screen, printer, RAM. ! */ ! @Override ! public void paintComponent( Graphics g ) ! { ! // super.paint( g ); ! // get size of box we have to draw in ! Dimension dim = getSize(); ! if ( image != null ) ! { ! /* center Image in box, normally should exactly fill the box. ! If we overflow, no problem, drawImage will clip. */ ! int imageWidth = image.getWidth( this ); ! int imageHeight = image.getHeight( this ); ! // this does not complete the job, just starts it. ! // We are notified of progress through our Component ImageObserver interface. ! g.drawImage ( image, /* Image to draw */ ! ( dim.width - imageWidth ) / 2, /* x */ ! ( dim.height - imageHeight ) / 2, /* y */ ! imageWidth, /* width */ ! imageHeight, /* height */ ! this ); /* this ImageViewerSwing component */ ! } ! else ! { ! /* we have no Image, clear the box */ ! g.setColor( getBackground() ); ! g.clearRect( 0, 0, dim.width, dim.height ); ! } ! } ! /** ! * Preferred Layout size. ! * ! * @return the recommended dimensions to display the Image. ! */ ! @Override ! public Dimension getPreferredSize() ! { ! if ( image != null ) ! { ! /* should just fit the Image */ ! return( new Dimension( image.getWidth(this), image.getHeight(this) ) ); ! } ! else ! { /* empty square as a placeholder */ ! return new Dimension( 100, 100 ); ! } ! } ! /** ! * Minimum layout size. ! * ! * @return he minimum dimensions to properly display the Image ! */ ! @Override ! public Dimension getMinimumSize() ! { ! return getPreferredSize(); ! } ! /** ! * Image that this viewer is currently displaying. ! */ ! private Image image; // @jve:decl-index=0: ! } // @jve:decl-index=0:visual-constraint="10,10" // end class ImageViewerSwing \ No newline at end of file --- 11,168 ---- /** * Component for viewing an Image. Simplified version of Symantec ImageViewer ! * displays an Image. Does not resize the image, though may crop it. Thus always ! * maintains original magnification and aspect ratio. Similar to IconImage. ! * * @author Roedy Green */ ! public class ImageViewerSwing extends JComponent { ! /** * */ private static final long serialVersionUID = 8190301649166149694L; ! /** ! * default Constructor ! */ ! public ImageViewerSwing() { ! setImage(Toolkit.getDefaultToolkit().getImage( ! "data/sprites/ideas/admin.png")); ! initialize(); ! } ! /** ! * Constructor with Image. ! * ! * @param image ! * the Image to be displayed . See the Java glossary under Image ! * for ways to create an Image from a file. ! */ ! public ImageViewerSwing(Image image) { ! this(); ! setImage(image); ! } ! /** ! * This method initializes this ! * ! */ ! private void initialize() { ! this.setSize(new Dimension(20, 20)); ! } ! /** ! * Set or change the current Image to display. setImage does a MediaTracker ! * to ensure the Image is loaded. You don't have to. If you don't plan to ! * use the old image again you should do a getImage().flush(); ! * ! * @param image ! * the new Image to be displayed. If the image jpg may have ! * recently changed, don't use getImage to create it, use ! * URL.openConnection() URLConnection.setUseCaches( false ) ! * Connection.getContent Component.createImage ! * ! */ ! public void setImage(Image image) { ! // even if Image object is same, we use it since it may have changed ! // state. ! this.image = image; ! if (image != null) { ! MediaTracker tracker; ! try { ! // wait until image is fully loaded. ! // and so that paint will be instantaneous, rather than gradual ! // as ! // the image arrives. ! // MediaTracker notifies of progress via our ! // Component.ImageObsever interface ! tracker = new MediaTracker(this); ! tracker.addImage(image, 0); ! tracker.waitForID(0); ! } catch (InterruptedException e) { ! } ! } ! // image is now ready, let's paint it ! repaint(); ! } ! /** ! * Get the Image currently being displayed. ! * ! * @return the Image currently displayed or null if no Image ! */ ! public Image getImage() { ! return image; ! } ! /** ! * Paints this component using the given graphics context. ! * ! * @param g ! * Graphics context where to paint, e.g. to screen, printer, RAM. ! */ ! @Override ! public void paintComponent(Graphics g) { ! // super.paint( g ); ! // get size of box we have to draw in ! Dimension dim = getSize(); ! if (image != null) { ! /* ! * center Image in box, normally should exactly fill the box. If we ! * overflow, no problem, drawImage will clip. ! */ ! int imageWidth = image.getWidth(this); ! int imageHeight = image.getHeight(this); ! // this does not complete the job, just starts it. ! // We are notified of progress through our Component ImageObserver ! // interface. ! g.drawImage(image, /* Image to draw */ ! (dim.width - imageWidth) / 2, /* x */ ! (dim.height - imageHeight) / 2, /* y */ ! imageWidth, /* width */ ! imageHeight, /* height */ ! this); /* this ImageViewerSwing component */ ! } else { ! /* we have no Image, clear the box */ ! g.setColor(getBackground()); ! g.clearRect(0, 0, dim.width, dim.height); ! } ! } ! /** ! * Preferred Layout size. ! * ! * @return the recommended dimensions to display the Image. ! */ ! @Override ! public Dimension getPreferredSize() { ! if (image != null) { ! /* should just fit the Image */ ! return (new Dimension(image.getWidth(this), image.getHeight(this))); ! } else { /* empty square as a placeholder */ ! return new Dimension(100, 100); ! } ! } ! /** ! * Minimum layout size. ! * ! * @return he minimum dimensions to properly display the Image ! */ ! @Override ! public Dimension getMinimumSize() { ! return getPreferredSize(); ! } ! ! /** ! * Image that this viewer is currently displaying. ! */ ! private Image image; // @jve:decl-index=0: ! ! } // @jve:decl-index=0:visual-constraint="10,10" // end class ImageViewerSwing |