⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpgencodegraph.java

📁 JAVA版的蚂蚁算法(Ant Colony Optimization Algorithms)
💻 JAVA
字号:
package dk.itu.nulx30.graph;

import att.grappa.Subgraph;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * This class can draw a picture of a given Grappa <code>Subgraph</code> in
 * JPEG-format.
 *
 * @see
 * <a href="http://www.research.att.com/~john/Grappa/docs/att/grappa/Subgraph.html">
 * att.grappa.Subgraph</a>
 *
 * @author  Mikkel Bundgaard
 * @author  Troels C. Damgaard
 * @author  Federico Decara
 * @author  Jacob W. Winther
 */
public class JpgEncodeGraph extends att.grappa.GrappaPanel {

  /**
   * The constructor for <code>JpgEncodeGraph</code>. This initializes
   * some of the variables used in the drawing and saving of the panel.
   *
   * @param graph the <code>Grappa SubGraph</code> which will be shown or saved
   * in a file.
   * @param filename the name of the file to output the image in (this is only
   * used if showInJFrame is false).
   * @param showInJFrame this variable decides whether the graph should be
   * shown in a <code>JFrame</code> or output to a file.
   *
   */
  public JpgEncodeGraph( Subgraph graph, String filename,
                                                        boolean showInJFrame ) {
    super( graph );

    // Set the size of the panel 
    setSize( new Dimension( 1024, 768 ) );
    setVisible( true );

    // Should this panel be shown in a frame or saved to a file.
    if ( showInJFrame ) {
      showInJFrame( this );
    }
    else {
      setScaleToSize( new Dimension( 1024, 768 ) );
      Container cont = new Container();
      convertComponentToBufferedImage( this, cont, filename );
      setVisible( false );
    }
  }

  /**
   * The constructor for <code>JpgEncodeGraph</code>. This initializes
   * some of the variables used in the drawing and saving of the panel.
   *
   * @param graph the <code>Grappa SubGraph</code> which will be saved in a file.
   * @param filename the name of the file to output the image in.
   *
   */
  public JpgEncodeGraph( att.grappa.Subgraph graph, String filename ) {
    this( graph, filename, false );
  }

  /**
   * Draws the graph panel on the North-region of the of the frame and adds an
   * exit button on the South-region.
   *
   * @param graphPanel the panel containing the graph. This panel is drawn on
   * the North-region of the frame.
   */
  private static void showInJFrame( JPanel graphPanel ){
    JFrame contentFrame = new JFrame("Show Graph test");
    contentFrame.getContentPane().setLayout( new BorderLayout() );
    contentFrame.getContentPane().add( graphPanel, BorderLayout.NORTH );
    JButton button = new JButton( "Exit");
    contentFrame.getContentPane().add( button, BorderLayout.SOUTH );

    button.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e) {
        System.exit(0);
      }
    });

    graphPanel.setPreferredSize( new Dimension( 1024, 768 ) );
    // fit the preferred size and layouts of the contentFrame subcomponents
    contentFrame.pack();
    contentFrame.show();
  }

  /**
   * <code>convertComponentToBufferedImage</code> converts the component
   * to a <code>BufferedImage</code> and calls the 
   * {@link #saveImageAsJPEG(BufferedImage, String) saveImageAsJPEG} method.
   *
   * @param cmp the <code>Component</code> to draw.
   * @param cont an intermedate <code>Container</code> which becomes parent
   *        for the component. This prevents c.validate() and and c.repaint()
   *        calls from propogating up the tree.
   * @param jpegfile the name of the output-file.
   */
  private static void convertComponentToBufferedImage( Component cmp,
                                             Container cont, String jpegfile ) {
    Rectangle d = cmp.getBounds();
    // Create a BufferedImage with the right size and image type.
    BufferedImage bi = new BufferedImage( d.width, d.height,
                                                   BufferedImage.TYPE_INT_RGB );
    Graphics2D g2d = bi.createGraphics();
    // Paint the component cmp on g2d. The intermediate container has no
    // effect other than to be an intermedate parent of cmp.
    SwingUtilities.paintComponent( g2d, cmp, cont, 0, 0, d.width, d.height );
    // Save the BufferedImage in a file.
    saveImageAsJPEG( bi, jpegfile );
  }

  /**
   * <code>saveImageAsJPEG</code> saves the image encoded as JPEG in the file
   * <code>filename</code>.
   *
   * @param bi a <code>BufferedImage</code> containing the graph.
   * @param filename the name of the file which will contain the graph
   *        in JPEG-format.
   */
  private static void saveImageAsJPEG( BufferedImage bi, String filename ) {
    try {
      // Make a new outputstream
      ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
      // Create an instance of a JPEGImageEncoder that can be used to encode
      // image data as JPEG Data streams.
      JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder( boutstream );
      // Encode the image data
      enc.encode( bi );
      FileOutputStream fimage = new FileOutputStream( new File( filename ) );
      // Write the encoded image to the output-file.
      boutstream.writeTo( fimage );
      fimage.close();
    }
    catch ( Exception e ) { System.out.println( e ); }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -