代码框架我已经写好了,麻烦高手帮忙。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import javax.swing.*;public class DrawTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               DrawFrame frame = new DrawFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}
class DrawFrame extends JFrame
{
   private JButton btn1,btn2;
   private DrawComponent component;
   
   public DrawFrame()
   {
      setTitle("DrawTest");
      setSize(400,400);
      
      btn1=new JButton("放大");
      btn2=new JButton("缩小");
      
      btn1.addActionListener(new ButtonActionListener());
      
      Panel controlPanel=new Panel();
      controlPanel.add(btn1);
      controlPanel.add(btn2);
      
      component = new DrawComponent();
      
      add(controlPanel,BorderLayout.NORTH);
      add(component,BorderLayout.CENTER);
   }
   class ButtonActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        JButton button = (JButton)e.getSource();
        if(button==btn1){    
            
        }
    }
   }
}
class DrawComponent extends JComponent
{
public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      Rectangle2D rect = new Rectangle2D.Double(100,100,200,200);
      g2.draw(rect);
   }
}

解决方案 »

  1.   

    放大的话,原图应该是张大图。
    初始显示的时候创建Image可以设置size.
    方法的话,根据你的倍率来,提供多大的倍率,就放大多少倍,都以原图为基础,因为原图是最大的。
    比如当前图片为50*50,原图为1000*1000(这缩小了20倍)。如果要放大一倍,就是100*100.那么就在原图的基础上缩小10倍。
      

  2.   

    贴一个  别人写的 我改改的,,,很多地方不规范 还有多于的东西 你自己 改吧 package com.element.swing;import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.color.ColorSpace;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorConvertOp;
    import java.awt.image.CropImageFilter;
    import java.awt.image.FilteredImageSource;
    import java.awt.image.ImageFilter;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;public class ChangeImageSize extends JPanel { static Image image1 = null; static int i = 1;
    JLabel label3; public ChangeImageSize() {
    super(new GridLayout(3, 1)); // 3 rows, 1 column label3 = new JLabel(new ImageIcon("c:/google1.png"));
    add(label3);
    JButton setBigButton = new JButton("放大");
    add(setBigButton);
    setBigButton.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent e) { setBigButton();
    }
    });
    } protected void setBigButton() {
    Image temp = scale("c:/google1.png", "C:/image.png", i++, true);
    label3.setIcon(new ImageIcon(temp));
    } /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */
    private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("LabelDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add content to the window.
    frame.add(new ChangeImageSize());
    frame.setPreferredSize(new Dimension(800, 600)); // Display the window.
    frame.pack();
    frame.setVisible(true);
    } public static void main(String[] args) {
    // Schedule a job for the event dispatch thread:
    // creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    // Turn off metal's use of bold fonts
    UIManager.put("swing.boldMetal", Boolean.FALSE); createAndShowGUI();
    }
    }); } /** */
    /**
     * 缩放图像
     * 
     * @param srcImageFile
     *            源图像文件地址
     * @param result
     *            缩放后的图像地址
     * @param scale
     *            缩放比例
     * @param flag
     *            缩放选择:true 放大; false 缩小;
     */
    public static Image scale(String srcImageFile, String result, double scale,
    boolean flag) {
    try {
    BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
    int width = src.getWidth(); // 得到源图宽
    int height = src.getHeight(); // 得到源图长
    if (flag) {
    // 放大
    width = (int) (width * scale);
    height = (int) (height * scale);
    } else {
    // 缩小
    width = (int) (width / scale);
    height = (int) (height / scale);
    }
    Image image = src.getScaledInstance(width, height,
    Image.SCALE_SMOOTH);
    // BufferedImage tag = new BufferedImage(width, height,
    // BufferedImage.TYPE_INT_RGB);
    // Graphics g = tag.getGraphics();
    // g.drawImage(image, 0, 0, null); // 绘制缩小后的图
    // g.dispose();
    image1 = image; // ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
    } catch (IOException e) {
    e.printStackTrace();
    }
    return image1;
    } /** */
    /**
     * 图像切割
     * 
     * @param srcImageFile
     *            源图像地址
     * @param descDir
     *            切片目标文件夹
     * @param destWidth
     *            目标切片宽度
     * @param destHeight
     *            目标切片高度
     */
    public static void cut(String srcImageFile, String descDir, int destWidth,
    int destHeight) {
    try {
    Image img;
    ImageFilter cropFilter;
    // 读取源图像
    BufferedImage bi = ImageIO.read(new File(srcImageFile));
    int srcWidth = bi.getHeight(); // 源图宽度
    int srcHeight = bi.getWidth(); // 源图高度
    if (srcWidth > destWidth && srcHeight > destHeight) {
    Image image = bi.getScaledInstance(srcWidth, srcHeight,
    Image.SCALE_DEFAULT);
    destWidth = 200; // 切片宽度
    destHeight = 150; // 切片高度
    int cols = 0; // 切片横向数量
    int rows = 0; // 切片纵向数量
    // 计算切片的横向和纵向数量
    if (srcWidth % destWidth == 0) {
    cols = srcWidth / destWidth;
    } else {
    cols = (int) Math.floor(srcWidth / destWidth) + 1;
    }
    if (srcHeight % destHeight == 0) {
    rows = srcHeight / destHeight;
    } else {
    rows = (int) Math.floor(srcHeight / destHeight) + 1;
    }
    // 循环建立切片
    // 改进的想法:是否可用多线程加快切割速度
    for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
    // 四个参数分别为图像起点坐标和宽高
    // 即: CropImageFilter(int x,int y,int width,int height)
    cropFilter = new CropImageFilter(j * 200, i * 150,
    destWidth, destHeight);
    img = Toolkit.getDefaultToolkit().createImage(
    new FilteredImageSource(image.getSource(),
    cropFilter));
    BufferedImage tag = new BufferedImage(destWidth,
    destHeight, BufferedImage.TYPE_INT_RGB);
    Graphics g = tag.getGraphics();
    g.drawImage(img, 0, 0, null); // 绘制缩小后的图
    g.dispose();
    // 输出为文件
    ImageIO.write(tag, "JPEG", new File(descDir
    + "pre_map_" + i + "_" + j + ".jpg"));
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /** */
    /**
     * 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
     */
    public static void convert(String source, String result) {
    try {
    File f = new File(source);
    f.canRead();
    f.canWrite();
    BufferedImage src = ImageIO.read(f);
    ImageIO.write(src, "JPG", new File(result));
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } /** */
    /**
     * 彩色转为黑白
     * 
     * @param source
     * @param result
     */
    public static void gray(String source, String result) {
    try {
    BufferedImage src = ImageIO.read(new File(source));
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);
    src = op.filter(src, null);
    ImageIO.write(src, "JPEG", new File(result));
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}