这里只Fill package com.loveyou.pic;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingUtilities;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class FillPictureFrame extends JFrame...{ public static final String FILLED_PREFIX="filled_"; private static final long serialVersionUID = 1L; private JLabel img=new JLabel(); private JLabel lblMsg=new JLabel(); private JPanel barPanel=new JPanel(); private JPanel toolPanel=new JPanel(); private JTextField txtX=new JTextField(5); private JTextField txtY=new JTextField(5); private JTextField txtW=new JTextField(5); private JTextField txtH=new JTextField(5); private JTextField txtColor=new JTextField(7); private JTextField txtPath=new JTextField(30); private JButton btnTest=new JButton("Test"); private JButton btnSelectPath=new JButton("..."); private JButton btnFill=new JButton("batch fill"); private JFileChooser jfc=new JFileChooser(); public FillPictureFrame()...{ super(); initUI(); } private void initUI() ...{ jfc.setCurrentDirectory(new File("")); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); this.setTitle("[Picture Tool] Fill Rect"); this.setSize(800,600); Dimension d=this.getToolkit().getScreenSize(); this.setLocation((d.width-getWidth())/2, (d.height-getHeight())/2); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container cp=this.getContentPane(); cp.setLayout(new BorderLayout()); cp.add(img,BorderLayout.CENTER); cp.add(barPanel,BorderLayout.NORTH); barPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,5)); barPanel.add(new JLabel("X:")); barPanel.add(txtX); barPanel.add(new JLabel("Y:")); barPanel.add(txtY); barPanel.add(new JLabel("Width:")); barPanel.add(txtW); barPanel.add(new JLabel("Height:")); barPanel.add(txtH); barPanel.add(new JLabel("Color(RGB):")); barPanel.add(txtColor); txtColor.setToolTipText("Range:000000-FFFFFF"); barPanel.add(btnTest); btnTest.addActionListener(new ActionListener()...{ public void actionPerformed(ActionEvent e) ...{ test(); } }); toolPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,5)); toolPanel.add(new JLabel("Picture file path(*.jpg):")); toolPanel.add(txtPath); toolPanel.add(btnSelectPath); toolPanel.add(btnFill); toolPanel.add(lblMsg); lblMsg.setPreferredSize(new Dimension(200,19)); cp.add(toolPanel,BorderLayout.SOUTH); btnSelectPath.addActionListener(new ActionListener()...{ public void actionPerformed(ActionEvent e) ...{ selectPath(); } }); btnFill.addActionListener(new ActionListener()...{ public void actionPerformed(ActionEvent e) ...{ fill(); } }); txtPath.addMouseListener(new MouseAdapter()...{ public void mouseClicked(MouseEvent e) ...{ if(e.getClickCount()==2)...{ String p=txtPath.getText().trim(); try ...{ Runtime.getRuntime().exec("cmd /c explorer "+p); } catch (Exception ex) ...{ } } } }); reset(); test(); } // private void process(final String msg){// SwingUtilities.invokeLater(new Runnable(){// public void run() {// lblMsg.setText(msg);// lblMsg.updateUI();// }});// } private void selectPath()...{ if(JFileChooser.APPROVE_OPTION==jfc.showOpenDialog(this))...{ this.txtPath.setText(jfc.getSelectedFile().getAbsolutePath()); } } private void fill()...{ String fn=this.txtPath.getText().trim(); File f=new File(fn); if(!f.exists())...{ JOptionPane.showMessageDialog(this, "Path ""+fn+"" not exists!"); txtPath.requestFocus(); return; } if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(this,"Output file name will with '"+FILLED_PREFIX+"' prefix, ready for start now?","Batch fill",JOptionPane.YES_NO_OPTION))...{ List<String> list=new ArrayList<String>(); getJpgFiles(f,list); ChangeThread t=new ChangeThread(list); t.start(); try ...{ while(!t.isTerminal())...{ Thread.sleep(100); } } catch (Exception e) ...{ e.printStackTrace(); } JOptionPane.showMessageDialog(this, "Batch fill complete! (files="+list.size()+")"); } } class ChangeThread extends Thread...{ List<String> list; boolean terminal=false; ChangeThread(List<String> list)...{ this.list=list; } public boolean isTerminal()...{ return terminal; } public void run()...{ Rectangle r=getRange(); if(r==null)...{ return; } Color fillColor=getFillColor(); if(fillColor==null)...{ return; } int n=0; for (String file : list) ...{ n++; ImageIcon icon=new ImageIcon(file); int w=icon.getIconWidth(); int h=icon.getIconHeight(); int xx=r.x; int yy=r.y; if(xx<0)...{ xx+=w; } if(yy<0)...{ yy+=h; } BufferedImage buf = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics g = buf.createGraphics( ); g.drawImage(icon.getImage(), 0, 0, null); g.setColor(fillColor); g.fillRect(xx, yy, r.width, r.height); buf.flush(); try...{ int i=file.lastIndexOf('/'); if(i!=-1)...{ file=file.substring(0,i+1)+FILLED_PREFIX+file.substring(i+1); }else...{ file=FILLED_PREFIX+file; }// process(n+""); FileOutputStream os =new FileOutputStream(file); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(os); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf); param.setQuality(1f, false); encoder.setJPEGEncodeParam(param); encoder.encode(buf); os.close(); }catch(Exception e)...{ e.printStackTrace(); } } terminal=true; } } private void getJpgFiles(File f,List<String> list)...{ if(f.isFile())...{ String s=f.getAbsolutePath(); s=s.replace('/', '/'); int i=s.lastIndexOf('/'); if(i!=-1)...{ String name=s.substring(i+1); if(name.startsWith(FILLED_PREFIX))...{ return; } } if(s.toLowerCase().endsWith(".jpg"))...{ list.add(s); } }else...{ File[] fs=f.listFiles(); for(int i=0,n=fs.length;i<n;i++)...{ getJpgFiles(fs[i],list); } } } private Rectangle getRange() ...{ try ...{ Rectangle r = new Rectangle(); r.x = getNumber(txtX); r.y = getNumber(txtY); r.width = getNumber(txtW); r.height = getNumber(txtH); return r; } catch (NumberFormatException e) ...{ return null; } } private Color getFillColor()...{ String s=txtColor.getText().trim(); if(s.length()>0)...{ if(s.matches("[/da-fA-F]{6}"))...{ int r=Integer.parseInt(s.substring(0,2), 16); int g=Integer.parseInt(s.substring(2,4), 16); int b=Integer.parseInt(s.substring(4,6), 16); return new Color(r,g,b); } } txtColor.requestFocus(); return null; } private int getNumber(JTextField txt)throws NumberFormatException...{ String s=txt.getText().trim(); if(s.length()>0)...{ if(s.matches("(/-)?/d+"))...{ return Integer.parseInt(s); } } txt.requestFocus(); throw new NumberFormatException(s); } private void test()...{ Rectangle r=getRange(); if(r==null)...{ return; } Color fillColor=getFillColor(); if(fillColor==null)...{ return; } ImageIcon icon=new ImageIcon(getClass().getResource("test.jpg")); int w=icon.getIconWidth(); int h=icon.getIconHeight(); if(r.x<0)...{ r.x+=w; } if(r.y<0)...{ r.y+=h; } BufferedImage buf = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics g = buf.createGraphics( ); g.drawImage(icon.getImage(), 0, 0, null); g.setColor(fillColor); g.fillRect(r.x, r.y, r.width, r.height); buf.flush(); img.setIcon(new ImageIcon(buf)); img.updateUI(); } private void reset()...{ txtX.setText("-180"); txtY.setText("10"); txtW.setText("150"); txtH.setText("50"); txtColor.setText("FF0000");// txtPath.setText("C:/Documents and Settings/Administrator/My Documents/新建文件夹"); } public void loadImage(String path)...{ ImageIcon icon=new ImageIcon(path); int w=icon.getIconWidth(); int h=icon.getIconHeight(); BufferedImage buf = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics g = buf.createGraphics( ); g.drawImage(icon.getImage(), 0, 0, null); g.setColor(Color.red); g.fillRect(0, 0, 100, 100); buf.flush(); img.setIcon(new ImageIcon(buf)); img.updateUI(); } public static void main(String[] args) ...{ FillPictureFrame e=new FillPictureFrame(); e.setVisible(true); }}