²é¿´: 5521  |  »Ø¸´: 48

lqshn

гæ (СÓÐÃûÆø)

[ÇóÖú] Çë½ÌÈçºÎÓÃmatlabʶ±ð³öÒ»ÕÅÇúÏßͼÉÏ×î¸ßµã¶ÔÓ¦µÄX,YÖá×ø±ê ÒÑÓÐ14È˲ÎÓë

¶ÔÓÚÒ»ÕÅjpgͼ£¨ÀýÈçÏÂͼ£©£¬Ã»ÓÐԭʼµÄÊý¾Ý£¬¶ÔÓÚͼÖÐÇúÏߣ¬ÈçºÎÓÃmatlabʶ±ð³ö×î¸ßµã¶ÔÓ¦µÄX,YÖá×ø±êÄØ£¿ÎÒÓкܶàÕâÑùµÄͼ£¬ËùÒÔÄܱà³ö³ÌÐò»òд³ö´úÂë×Ô¶¯Ê¶±ð¾ÍºÃÁË£¬È»ºó½«×ø±êÊä³ö£¬ÇëÎÊË­ÓÐʲôÏë·¨»òÕßË­»áÂð£¿

Çë½ÌÈçºÎÓÃmatlabʶ±ð³öÒ»ÕÅÇúÏßͼÉÏ×î¸ßµã¶ÔÓ¦µÄX,YÖá×ø±ê
ref.jpg
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

» ±¾Ö÷ÌâÏà¹Ø¼ÛÖµÌùÍÆ¼ö£¬¶ÔÄúͬÑùÓаïÖú:

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
»ØÌûÖ§³Ö ( ÏÔʾ֧³Ö¶È×î¸ßµÄǰ 50 Ãû )

Wormhole55

гæ (³õÈëÎÄ̳)

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¡ï
lqshn: ½ð±Ò+1, ¡ïÓаïÖú 2015-06-16 13:15:36
ÊÔÊÔÌ©ÀÕ¹«Ê½£¬ ½¥½üÏߣ¬ ¸ßÊý¿Î±¾Àïѧ¹ý£¬ ¿ÉÒÔÄ£Äâ¸÷ÖÖÇúÏßµÄ×ø±ê£¬ »òÕß»¹ÓÐÆäËû·½·¨£¬ ¾ßÌåµÄ¼Ç²»ÇåÁË¡£ ÕâÆäʵ²»½öÊDZà³Ì£¬¸ü¶àµÄÊǶÔÊýѧ½¨Ä£µÄÀí½âºÍ·½·¨µÄÑ¡Ôñ£¬ Äã¿ÉÒÔÕÒ¸öѧÊýѧµÄרҵÈËÊ¿ÌÖÂÛһϣ¬ ¿Ï¶¨»áÓÐÊÕ»ñ¡£
ÖÁÓÚͼÏñµÄɨÃèºÍÏñËØµÄʶ±ð£¬ ±à³Ì·½ÃæÃ»ÓÐʲô´óµÄÄѶȣ¬ ÿ¸öJPG ÏñËØ¶¼ÊÇÒ»¸öÊýÖµµÄÐÎʽ´¢´æµÄ£¬ ¶ÔÏñËØÑÕÉ«ÊýÖµ½øÐзÖÎö¾ÍÊÇÁË£¨¸öÈËÏë·¨£¬Ã»ÊÔ¹ý£©¡£windows »òc++ »òjava ¶¼ÓÐ×Ô¼ºµÄÀà¿â רÃÅ×öͼÏñ·ÖÎöºÍ´¦ÀíµÄ£¬ÊÇһЩDLL Àà¿â£¬ Ö±½ÓÄÃÀ´ÓþÍÊÇÁË¡£Ò»ÏÂÊÇÒ»¸öjavaÀý³Ì£¬Í¼Æ¬´¦ÀíµÄ£¬
Below is the syntax highlighted version of Picture.java from ¡ì Standard Libraries.   Here is the Javadoc.


/*************************************************************************
*  Compilation:  javac Picture.java
*  Execution:    java Picture imagename
*
*  Data type for manipulating individual pixels of an image. The original
*  image can be read from a file in jpg, gif, or png format, or the
*  user can create a blank image of a given size. Includes methods for
*  displaying the image in a window on the screen or saving to a file.
*
*  % java Picture mandrill.jpg
*
*  Remarks
*  -------
*   - pixel (x, y) is column x and row y, where (0, 0) is upper left
*
*   - see also GrayPicture.java for a grayscale version
*
*************************************************************************/

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;


/**
*  This class provides methods for manipulating individual pixels of
*  an image. The original image can be read from a <tt>.jpg</tt>, <tt>.gif</tt>,
*  or <tt>.png</tt> file or the user can create a blank image of a given size.
*  This class includes methods for displaying the image in a window on
*  the screen or saving it to a file.
*  <p>
*  Pixel (<em>x</em>, <em>y</em> is column <em>x</em> and row <em>y</em>.
*  By default, the origin (0, 0) is upper left, which is a common convention
*  in image processing.
*  The method <tt>setOriginLowerLeft()</tt> change the origin to the lower left.
*  <p>
*  For additional documentation, see
*  <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of
*  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
*  by Robert Sedgewick and Kevin Wayne.
*
*  @author Robert Sedgewick
*  @author Kevin Wayne
*/
public final class Picture implements ActionListener {
    private BufferedImage image;               // the rasterized image
    private JFrame frame;                      // on-screen view
    private String filename;                   // name of file
    private boolean isOriginUpperLeft = true;  // location of origin
    private final int width, height;           // width and height

   /**
     * Initializes a blank <tt>width</tt>-by-<tt>height</tt> picture, with <tt>width</tt> columns
     * and <tt>height</tt> rows, where each pixel is black.
     */
    public Picture(int width, int height) {
        if (width  < 0) throw new IllegalArgumentException("width must be nonnegative";
        if (height < 0) throw new IllegalArgumentException("height must be nonnegative";
        this.width  = width;
        this.height = height;
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // set to TYPE_INT_ARGB to support transparency
        filename = width + "-by-" + height;
    }

   /**
     * Initializes a new picture that is a deep copy of <tt>picture</tt>.
     */
    public Picture(Picture picture) {
        width  = picture.width();
        height = picture.height();
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        filename = picture.filename;
        for (int col = 0; col < width(); col++)
            for (int row = 0; row < height(); row++)
                image.setRGB(col, row, picture.get(col, row).getRGB());
    }

   /**
     * Initializes a picture by reading in a .png, .gif, or .jpg from
     * the given filename or URL name.
     */
    public Picture(String filename) {
        this.filename = filename;
        try {
            // try to read from file in working directory
            File file = new File(filename);
            if (file.isFile()) {
                image = ImageIO.read(file);
            }

            // now try to read from file in same directory as this .class file
            else {
                URL url = getClass().getResource(filename);
                if (url == null) { url = new URL(filename); }
                image = ImageIO.read(url);
            }
            width  = image.getWidth(null);
            height = image.getHeight(null);
        }
        catch (IOException e) {
            // e.printStackTrace();
            throw new RuntimeException("Could not open file: " + filename);
        }
    }

   /**
     * Initializes a picture by reading in a .png, .gif, or .jpg from a File.
     */
    public Picture(File file) {
        try { image = ImageIO.read(file); }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Could not open file: " + file);
        }
        if (image == null) {
            throw new RuntimeException("Invalid image file: " + file);
        }
        width  = image.getWidth(null);
        height = image.getHeight(null);
        filename = file.getName();
    }

   /**
     * Returns a JLabel containing this picture, for embedding in a JPanel,
     * JFrame or other GUI widget.
     * @return the <tt>JLabel</tt>
     */
    public JLabel getJLabel() {
        if (image == null) { return null; }         // no image available
        ImageIcon icon = new ImageIcon(image);
        return new JLabel(icon);
    }

   /**
     * Sets the origin to be the upper left pixel. This is the default.
     */
    public void setOriginUpperLeft() {
        isOriginUpperLeft = true;
    }

   /**
     * Sets the origin to be the lower left pixel.
     */
    public void setOriginLowerLeft() {
        isOriginUpperLeft = false;
    }

   /**
     * Displays the picture in a window on the screen.
     */
    public void show() {

        // create the GUI for viewing the image if needed
        if (frame == null) {
            frame = new JFrame();

            JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("File";
            menuBar.add(menu);
            JMenuItem menuItem1 = new JMenuItem(" Save...   ";
            menuItem1.addActionListener(this);
            menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                                     Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            menu.add(menuItem1);
            frame.setJMenuBar(menuBar);



            frame.setContentPane(getJLabel());
            // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setTitle(filename);
            frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
        }

        // draw
        frame.repaint();
    }

   /**
     * Returns the height of the picture.
     * @return the height of the picture (in pixels)
     */
    public int height() {
        return height;
    }

   /**
     * Returns the width of the picture.
     * @return the width of the picture (in pixels)
     */
    public int width() {
        return width;
    }

   /**
     * Returns the color of pixel (<tt>col</tt>, <tt>row</tt>.
     * @return the color of pixel (<tt>col</tt>, <tt>row</tt>
     * @throws IndexOutOfBoundsException unless both 0 &le; <tt>col</tt> &lt; <tt>width</tt>
     * and 0 &le; <tt>row</tt> &lt; <tt>height</tt>
     */
    public Color get(int col, int row) {
        if (col < 0 || col >= width())  throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
        if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
        if (isOriginUpperLeft) return new Color(image.getRGB(col, row));
        else                   return new Color(image.getRGB(col, height - row - 1));
    }

   /**
     * Sets the color of pixel (<tt>col</tt>, <tt>row</tt> to given color.
     * @throws IndexOutOfBoundsException unless both 0 &le; <tt>col</tt> &lt; <tt>width</tt>
     * and 0 &le; <tt>row</tt> &lt; <tt>height</tt>
     * @throws NullPointerException if <tt>color</tt> is <tt>null</tt>
     */
    public void set(int col, int row, Color color) {
        if (col < 0 || col >= width())  throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
        if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
        if (color == null) throw new NullPointerException("can't set Color to null";
        if (isOriginUpperLeft) image.setRGB(col, row, color.getRGB());
        else                   image.setRGB(col, height - row - 1, color.getRGB());
    }

   /**
     * Is this Picture equal to obj?
     * @return <tt>true</tt> if this picture is the same dimension as <tt>obj</tt>
     * and if all pixels have the same color
     */
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null) return false;
        if (obj.getClass() != this.getClass()) return false;
        Picture that = (Picture) obj;
        if (this.width()  != that.width())  return false;
        if (this.height() != that.height()) return false;
        for (int col = 0; col < width(); col++)
            for (int row = 0; row < height(); row++)
                if (!this.get(col, row).equals(that.get(col, row))) return false;
        return true;
    }


   /**
     * Saves the picture to a file in a standard image format.
     * The filetype must be .png or .jpg.
     */
    public void save(String name) {
        save(new File(name));
    }

   /**
     * Saves the picture to a file in a standard image format.
     */
    public void save(File file) {
        this.filename = file.getName();
        if (frame != null) { frame.setTitle(filename); }
        String suffix = filename.substring(filename.lastIndexOf('.') + 1);
        suffix = suffix.toLowerCase();
        if (suffix.equals("jpg" || suffix.equals("png") {
            try { ImageIO.write(image, suffix, file); }
            catch (IOException e) { e.printStackTrace(); }
        }
        else {
            System.out.println("Error: filename must end in .jpg or .png";
        }
    }

   /**
     * Opens a save dialog box when the user selects "Save As" from the menu.
     */
    public void actionPerformed(ActionEvent e) {
        FileDialog chooser = new FileDialog(frame,
                             "Use a .png or .jpg extension", FileDialog.SAVE);
        chooser.setVisible(true);
        if (chooser.getFile() != null) {
            save(chooser.getDirectory() + File.separator + chooser.getFile());
        }
    }


   /**
     * Tests this <tt>Picture</tt> data type. Reads a picture specified by the command-line argument,
     * and shows it in a window on the screen.
     */
    public static void main(String[] args) {
        Picture picture = new Picture(args[0]);
        System.out.printf("%d-by-%d\n", picture.width(), picture.height());
        picture.show();
    }

}
7Â¥2015-04-05 01:46:34
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

xytlitu

Òø³æ (СÓÐÃûÆø)

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
²»ÓÃmatlabÄÇô¸´ÔÓ¡£ÓÐרÃÅÈí¼þ£¬²»¹ýÍüÃû×ÖÁË¡£Äã¿ÉÒÔ×Ô¼º²é²é

[ ·¢×ÔСľ³æ¿Í»§¶Ë ]
8Â¥2015-04-05 02:26:04
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
ÆÕͨ»ØÌû

WanderingHeart

Ìú¸Ëľ³æ (ÖøÃûдÊÖ)

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
¼ÈȻֻÓÐͼ£¬ÄÇÄã¾ÍÒªÏë°ì·¨°Ñͼ¶Áµ½MATLABÀȻºó¿´ÔõôÅжÏÁË£¬¿Ï¶¨ÊÇ¿ÉÒԸ㶨µÄ¡£
2Â¥2015-04-03 16:50:54
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

lqshn

гæ (СÓÐÃûÆø)

ÒýÓûØÌû:
2Â¥: Originally posted by WanderingHeart at 2015-04-03 16:50:54
¼ÈȻֻÓÐͼ£¬ÄÇÄã¾ÍÒªÏë°ì·¨°Ñͼ¶Áµ½MATLABÀȻºó¿´ÔõôÅжÏÁË£¬¿Ï¶¨ÊÇ¿ÉÒԸ㶨µÄ¡£

ÓÐ˼·Âð£¿ÎÒÊÇÔÚÏëÄÃʲô×÷ΪÅжÏÌõ¼þ
3Â¥2015-04-03 18:35:38
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

WanderingHeart

Ìú¸Ëľ³æ (ÖøÃûдÊÖ)

ÒýÓûØÌû:
3Â¥: Originally posted by lqshn at 2015-04-03 18:35:38
ÓÐ˼·Âð£¿ÎÒÊÇÔÚÏëÄÃʲô×÷ΪÅжÏÌõ¼þ...

ÄãÖ»ÓÐͼ£¬ÄǾÍÖ»Äܵ¼ÈëͼƬÒÔºóÓñíʾÇúÏßÑÕÉ«µÄÁ¿À´Åжϡ£
4Â¥2015-04-03 22:25:36
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

ÄäÃû

Óû§×¢Ïú (Ö°Òµ×÷¼Ò)

Matlabר¼Ò

¡ï
¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
lqshn: ½ð±Ò+1, ¡ïÓаïÖú 2015-06-16 13:19:58
±¾Ìû½öÂ¥Ö÷¿É¼û
5Â¥2015-04-04 01:05:56
ÒÑÔÄ   ÉêÇë³ÌÐòÇ¿Ìû   »Ø¸´´ËÂ¥   ±à¼­   ²é¿´ÎÒµÄÖ÷Ò³

Wormhole55

гæ (³õÈëÎÄ̳)

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¡ï
¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
lqshn: ½ð±Ò+1, ¡ïÓаïÖú 2015-06-16 13:16:18
ÊÔÊÔÌ©ÀÕ¹«Ê½£¬ ½¥½üÏߣ¬ ¸ßÊý¿Î±¾Àïѧ¹ý£¬ ¿ÉÒÔÄ£Äâ¸÷ÖÖÇúÏßµÄ×ø±ê£¬ »òÕß»¹ÓÐÆäËû·½·¨£¬ ¾ßÌåµÄ¼Ç²»ÇåÁË¡£ ÕâÆäʵ²»½öÊDZà³Ì£¬¸ü¶àµÄÊǶÔÊýѧ½¨Ä£µÄÀí½âºÍ·½·¨µÄÑ¡Ôñ£¬ Äã¿ÉÒÔÕÒ¸öѧÊýѧµÄרҵÈËÊ¿ÌÖÂÛһϣ¬ ¿Ï¶¨»áÓÐÊÕ»ñ¡£
ÖÁÓÚͼÏñµÄɨÃèºÍÏñËØµÄʶ±ð£¬ ±à³Ì·½ÃæÃ»ÓÐʲô´óµÄÄѶȣ¬ ÿ¸öJPG ÏñËØ¶¼ÊÇÒ»¸öÊýÖµµÄÐÎʽ´¢´æµÄ£¬ ¶ÔÏñËØÑÕÉ«ÊýÖµ½øÐзÖÎö¾ÍÊÇÁË£¨¸öÈËÏë·¨£¬Ã»ÊÔ¹ý£©¡£windows »òc++ »òjava ¶¼ÓÐ×Ô¼ºµÄÀà¿â רÃÅ×öͼÏñ·ÖÎöºÍ´¦ÀíµÄ£¬ÊÇһЩDLL Àà¿â£¬ Ö±½ÓÄÃÀ´ÓþÍÊÇÁË¡£Ò»ÏÂÊÇÒ»¸öjavaÀý³Ì£¬Í¼Æ¬´¦ÀíµÄ£¬
Below is the syntax highlighted version of Picture.java from ¡ì Standard Libraries.   Here is the Javadoc.


/*************************************************************************
*  Compilation:  javac Picture.java
*  Execution:    java Picture imagename
*
*  Data type for manipulating individual pixels of an image. The original
*  image can be read from a file in jpg, gif, or png format, or the
*  user can create a blank image of a given size. Includes methods for
*  displaying the image in a window on the screen or saving to a file.
*
*  % java Picture mandrill.jpg
*
*  Remarks
*  -------
*   - pixel (x, y) is column x and row y, where (0, 0) is upper left
*
*   - see also GrayPicture.java for a grayscale version
*
*************************************************************************/

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;


/**
*  This class provides methods for manipulating individual pixels of
*  an image. The original image can be read from a <tt>.jpg</tt>, <tt>.gif</tt>,
*  or <tt>.png</tt> file or the user can create a blank image of a given size.
*  This class includes methods for displaying the image in a window on
*  the screen or saving it to a file.
*  <p>
*  Pixel (<em>x</em>, <em>y</em> is column <em>x</em> and row <em>y</em>.
*  By default, the origin (0, 0) is upper left, which is a common convention
*  in image processing.
*  The method <tt>setOriginLowerLeft()</tt> change the origin to the lower left.
*  <p>
*  For additional documentation, see
*  <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of
*  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
*  by Robert Sedgewick and Kevin Wayne.
*
*  @author Robert Sedgewick
*  @author Kevin Wayne
*/
public final class Picture implements ActionListener {
    private BufferedImage image;               // the rasterized image
    private JFrame frame;                      // on-screen view
    private String filename;                   // name of file
    private boolean isOriginUpperLeft = true;  // location of origin
    private final int width, height;           // width and height

   /**
     * Initializes a blank <tt>width</tt>-by-<tt>height</tt> picture, with <tt>width</tt> columns
     * and <tt>height</tt> rows, where each pixel is black.
     */
    public Picture(int width, int height) {
        if (width  < 0) throw new IllegalArgumentException("width must be nonnegative";
        if (height < 0) throw new IllegalArgumentException("height must be nonnegative";
        this.width  = width;
        this.height = height;
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // set to TYPE_INT_ARGB to support transparency
        filename = width + "-by-" + height;
    }

   /**
     * Initializes a new picture that is a deep copy of <tt>picture</tt>.
     */
    public Picture(Picture picture) {
        width  = picture.width();
        height = picture.height();
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        filename = picture.filename;
        for (int col = 0; col < width(); col++)
            for (int row = 0; row < height(); row++)
                image.setRGB(col, row, picture.get(col, row).getRGB());
    }

   /**
     * Initializes a picture by reading in a .png, .gif, or .jpg from
     * the given filename or URL name.
     */
    public Picture(String filename) {
        this.filename = filename;
        try {
            // try to read from file in working directory
            File file = new File(filename);
            if (file.isFile()) {
                image = ImageIO.read(file);
            }

            // now try to read from file in same directory as this .class file
            else {
                URL url = getClass().getResource(filename);
                if (url == null) { url = new URL(filename); }
                image = ImageIO.read(url);
            }
            width  = image.getWidth(null);
            height = image.getHeight(null);
        }
        catch (IOException e) {
            // e.printStackTrace();
            throw new RuntimeException("Could not open file: " + filename);
        }
    }

   /**
     * Initializes a picture by reading in a .png, .gif, or .jpg from a File.
     */
    public Picture(File file) {
        try { image = ImageIO.read(file); }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Could not open file: " + file);
        }
        if (image == null) {
            throw new RuntimeException("Invalid image file: " + file);
        }
        width  = image.getWidth(null);
        height = image.getHeight(null);
        filename = file.getName();
    }

   /**
     * Returns a JLabel containing this picture, for embedding in a JPanel,
     * JFrame or other GUI widget.
     * @return the <tt>JLabel</tt>
     */
    public JLabel getJLabel() {
        if (image == null) { return null; }         // no image available
        ImageIcon icon = new ImageIcon(image);
        return new JLabel(icon);
    }

   /**
     * Sets the origin to be the upper left pixel. This is the default.
     */
    public void setOriginUpperLeft() {
        isOriginUpperLeft = true;
    }

   /**
     * Sets the origin to be the lower left pixel.
     */
    public void setOriginLowerLeft() {
        isOriginUpperLeft = false;
    }

   /**
     * Displays the picture in a window on the screen.
     */
    public void show() {

        // create the GUI for viewing the image if needed
        if (frame == null) {
            frame = new JFrame();

            JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("File";
            menuBar.add(menu);
            JMenuItem menuItem1 = new JMenuItem(" Save...   ";
            menuItem1.addActionListener(this);
            menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                                     Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            menu.add(menuItem1);
            frame.setJMenuBar(menuBar);



            frame.setContentPane(getJLabel());
            // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setTitle(filename);
            frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
        }

        // draw
        frame.repaint();
    }

   /**
     * Returns the height of the picture.
     * @return the height of the picture (in pixels)
     */
    public int height() {
        return height;
    }

   /**
     * Returns the width of the picture.
     * @return the width of the picture (in pixels)
     */
    public int width() {
        return width;
    }

   /**
     * Returns the color of pixel (<tt>col</tt>, <tt>row</tt>.
     * @return the color of pixel (<tt>col</tt>, <tt>row</tt>
     * @throws IndexOutOfBoundsException unless both 0 &le; <tt>col</tt> &lt; <tt>width</tt>
     * and 0 &le; <tt>row</tt> &lt; <tt>height</tt>
     */
    public Color get(int col, int row) {
        if (col < 0 || col >= width())  throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
        if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
        if (isOriginUpperLeft) return new Color(image.getRGB(col, row));
        else                   return new Color(image.getRGB(col, height - row - 1));
    }

   /**
     * Sets the color of pixel (<tt>col</tt>, <tt>row</tt> to given color.
     * @throws IndexOutOfBoundsException unless both 0 &le; <tt>col</tt> &lt; <tt>width</tt>
     * and 0 &le; <tt>row</tt> &lt; <tt>height</tt>
     * @throws NullPointerException if <tt>color</tt> is <tt>null</tt>
     */
    public void set(int col, int row, Color color) {
        if (col < 0 || col >= width())  throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
        if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
        if (color == null) throw new NullPointerException("can't set Color to null";
        if (isOriginUpperLeft) image.setRGB(col, row, color.getRGB());
        else                   image.setRGB(col, height - row - 1, color.getRGB());
    }

   /**
     * Is this Picture equal to obj?
     * @return <tt>true</tt> if this picture is the same dimension as <tt>obj</tt>
     * and if all pixels have the same color
     */
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null) return false;
        if (obj.getClass() != this.getClass()) return false;
        Picture that = (Picture) obj;
        if (this.width()  != that.width())  return false;
        if (this.height() != that.height()) return false;
        for (int col = 0; col < width(); col++)
            for (int row = 0; row < height(); row++)
                if (!this.get(col, row).equals(that.get(col, row))) return false;
        return true;
    }


   /**
     * Saves the picture to a file in a standard image format.
     * The filetype must be .png or .jpg.
     */
    public void save(String name) {
        save(new File(name));
    }

   /**
     * Saves the picture to a file in a standard image format.
     */
    public void save(File file) {
        this.filename = file.getName();
        if (frame != null) { frame.setTitle(filename); }
        String suffix = filename.substring(filename.lastIndexOf('.') + 1);
        suffix = suffix.toLowerCase();
        if (suffix.equals("jpg" || suffix.equals("png") {
            try { ImageIO.write(image, suffix, file); }
            catch (IOException e) { e.printStackTrace(); }
        }
        else {
            System.out.println("Error: filename must end in .jpg or .png";
        }
    }

   /**
     * Opens a save dialog box when the user selects "Save As" from the menu.
     */
    public void actionPerformed(ActionEvent e) {
        FileDialog chooser = new FileDialog(frame,
                             "Use a .png or .jpg extension", FileDialog.SAVE);
        chooser.setVisible(true);
        if (chooser.getFile() != null) {
            save(chooser.getDirectory() + File.separator + chooser.getFile());
        }
    }


   /**
     * Tests this <tt>Picture</tt> data type. Reads a picture specified by the command-line argument,
     * and shows it in a window on the screen.
     */
    public static void main(String[] args) {
        Picture picture = new Picture(args[0]);
        System.out.printf("%d-by-%d\n", picture.width(), picture.height());
        picture.show();
    }

}
6Â¥2015-04-05 00:02:06
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

zhuhai0917

ľ³æ (ÕýʽдÊÖ)

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
ÓжÁȡͼƬÊý¾ÝµÄÈí¼þ£¬±¾¿ÆÂÛÎĵ¼Ê¦ÍƼö¸øÎҵģ¬ÍüÁ˽ÐɶÁË£¬Ã²ËÆdatecraft»¹ÊÇɶ

[ ·¢×ÔСľ³æ¿Í»§¶Ë ]
9Â¥2015-04-05 03:32:20
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

ÏíÊܼÇÒä

гæ (СÓÐÃûÆø)

Æäʵ¿ÉÒÔɨÃè½øÈ¥£¬È»ºó°´ÕÕÑÕÉ«ÀëÉ¢»¯µã£¬ÕâÑùͼÐÎÎÊÌâת»»³ÉÁËÊýÖµÎÊÌ⣬֮ºó¾ÍÈÝÒ×ÁË

[ ·¢×ÔСľ³æ¿Í»§¶Ë ]
10Â¥2015-04-05 07:29:17
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ lqshn µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] 288Çóµ÷¼Á +3 ÓÚº£º£º£º£ 2026-03-19 3/150 2026-03-19 10:38 by ÓÐÖ»ÀêÅ«
[¿¼ÑÐ] ²ÄÁÏר˶ӢһÊý¶þ306 +5 z1z2z3879 2026-03-18 5/250 2026-03-19 07:43 by BruceLiu320
[¿¼ÑÐ] 0854¿É¿çµ÷¼Á£¬Ò»×÷Ò»ÏîºËÐÄÂÛÎÄÎåÏîרÀû£¬Ê¡¡¢¹ú¼¶Ö¤Êé40+ÊýÒ»Ó¢Ò»287 +8 СÀî0854 2026-03-16 8/400 2026-03-18 14:35 by ²«»÷518
[¿¼ÑÐ] 331Çóµ÷¼Á£¨0703Óлú»¯Ñ§ +7 ZY-05 2026-03-13 8/400 2026-03-18 14:13 by 007_lilei
[¿¼ÑÐ] ÉúÎïѧ071000 329·ÖÇóµ÷¼Á +3 ÎÒ°®ÉúÎïÉúÎﰮΠ2026-03-17 3/150 2026-03-18 10:12 by macy2011
[¿¼ÑÐ] 0703»¯Ñ§336·ÖÇóµ÷¼Á +6 zbzihdhd 2026-03-15 7/350 2026-03-18 09:53 by zhukairuo
[¿¼ÑÐ] 268Çóµ÷¼Á +7 ºÃÔËÁ¬Ã಻¾ø 2026-03-12 8/400 2026-03-17 20:28 by xilongliang
[˶²©¼ÒÔ°] ºþ±±¹¤Òµ´óѧ ÉúÃü¿ÆÑ§Ó뽡¿µÑ§Ôº-¿ÎÌâ×éÕÐÊÕ2026¼¶Ê³Æ·/ÉúÎï·½Ïò˶ʿ +3 1ϲ´º8 2026-03-17 5/250 2026-03-17 17:18 by ber´¨cool×Ó
[¿¼ÑÐ] 085601Çóµ÷¼Á +4 Du.11 2026-03-16 4/200 2026-03-17 17:08 by ruiyingmiao
[¿¼ÑÐ] ÓÐûÓеÀÌú/ÍÁľµÄÏëµ÷¼ÁÄÏÁÖ£¬¸ø×Ô¼ºÕÐʦµÜÖС« +3 TqlXswl 2026-03-16 7/350 2026-03-17 15:23 by TqlXswl
[¿¼ÑÐ] 290Çóµ÷¼Á +6 ¿×Ö¾ºÆ 2026-03-12 11/550 2026-03-17 14:41 by ÖÜÖÛÖÛ77
[¿¼ÑÐ] 302Çóµ÷¼Á +4 С¼Öͬѧ123 2026-03-15 8/400 2026-03-17 10:33 by С¼Öͬѧ123
[»ù½ðÉêÇë] ½ñÄêµÄ¹ú»ù½ðÊÇ´ò·ÖÖÆÂ𣿠50+3 zhanghaozhu 2026-03-14 3/150 2026-03-16 17:07 by ±±¾©À³ÒðÈóÉ«
[¿¼ÑÐ] ²ÄÁÏÓ뻯¹¤ 323 Ó¢Ò»+Êý¶þ+Îﻯ£¬Ò»Ö¾Ô¸£º¹þ¹¤´ó ±¾È˱¾¿ÆË«Ò»Á÷ +4 ×ÔÓɵÄ_·ÉÏè 2026-03-13 5/250 2026-03-14 19:39 by hmn_wj
[»ù½ðÉêÇë] ÏÖÔÚÈçºÎ»Ø±ÜÈ¥ÄêµÄijһ¸öר¼Ò£¬²»ÖªµÀÃû×Ö +3 zk200107 2026-03-12 6/300 2026-03-14 17:13 by zk200107
[¿¼ÑÐ] ¸´ÊÔµ÷¼Á +3 ºôºô£¿~+123456 2026-03-14 3/150 2026-03-14 16:53 by WTUChen
[¿¼ÑÐ] 297Çóµ÷¼Á +4 ѧº£Æ¯²´ 2026-03-13 4/200 2026-03-14 11:51 by ÈÈÇéɳĮ
[¿¼ÑÐ] 266Çóµ÷¼Á +4 ѧԱ97LZgn 2026-03-13 4/200 2026-03-14 08:37 by zhukairuo
[¿¼ÑÐ] £Û0860£Ý321·ÖÇóµ÷¼Á£¬abÇø½Ô¿É +4 ±¦¹óÈÈ 2026-03-13 4/200 2026-03-13 22:01 by ÐÇ¿ÕÐÇÔÂ
[¿¼ÑÐ] 321Çóµ÷¼Á£¨Ê³Æ·/ר˶£© +3 xc321 2026-03-12 6/300 2026-03-13 08:45 by xc321
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û