请求高手帮忙。
如何重绘不规则的部分区域?
setClip()如何使用?
repaint()用法?

解决方案 »

  1.   

    1. setClip方法简介来之互联网
        setClip方法的原理是通过只在屏幕上显示一部分内容,让图片恰好位于该部分的内容显示出来。  setClip的方法就是在屏幕上设置这个“透视孔”,只有位于该孔中的图片内容显示出来。  setClip的方法说明如下:  public void setClip(int x, int y, int width, int height)  其中:  x——可以显示的矩形区域左上角的x坐标  y——可以显示的举行区域左上角的y坐标  width——矩形的宽度  height——矩型的高度2.在需要重绘时,调用repaint(),系统就会自动调用paint()
       paint是回调函数,你需要重写,一般不会单独调用,而是调用repaint()
       实际上,repaint是先调用update()方法,再由他调用paint()
       所以假如你图像有闪烁的话,不妨重写update(),实现双缓冲
      

  2.   

    给你个例子 
     
    上次写的  把图片 做成圆角  就是用的 setClip
    import java.awt.event.ActionListener;
    import java.awt.geom.GeneralPath;
    //import java.awt.geom.Rectangle2D;
    import java.awt.geom.RoundRectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    //import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    //import javax.swing.JTextArea;
    //import javax.swing.JTextPane;
    import javax.swing.filechooser.FileNameExtensionFilter;
    public class GeneralPathRect 
    {    /**
         * @param args
         */
        public static void main(String[] args) 
        {
            new GeneralPathRectFrame();    }}
    class GeneralPathRectFrame extends JFrame
    {
        
        private static final long serialVersionUID = 1L;
        private GeneralPath path=new GeneralPath();
        BufferedImage image;
        JFileChooser chooser=new JFileChooser();
        
        JMenuBar bar=new JMenuBar();
        JPanel panel=new drawPanel();
        public GeneralPathRectFrame()
        {
            setSize(400,400);
            
            JMenu file=new JMenu("file");
            JMenuItem openItem=new JMenuItem("导入图片");
            
            openItem.addActionListener(new ActionListener()
            {            @Override
                public void actionPerformed(ActionEvent arg0) 
                {
                    // TODO Auto-generated method stub
                    chooser.setCurrentDirectory(new File("."));
                    String []suffiex=ImageIO.getReaderFileSuffixes();
                    chooser.setFileFilter(new FileNameExtensionFilter("Image file",suffiex));
                    int ans=chooser.showOpenDialog(GeneralPathRectFrame.this);
                    if(ans==JFileChooser.APPROVE_OPTION)
                    {
                        try 
                        {
                            image=ImageIO.read(chooser.getSelectedFile());
                            repaint();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    
        }});
            file.add(openItem);
            //pane.insertComponent(area);
            add(panel);
            
            bar.add(file);
            setJMenuBar(bar);
            setVisible(true);
        }
        class drawPanel extends JPanel
        {
        
            private static final long serialVersionUID = 1L;    public void paint(Graphics g)
        {
            Graphics2D g2=(Graphics2D)g;
            if(image!=null)
            {
            int width=image.getWidth(),height=image.getHeight();
            RoundRectangle2D rect=new RoundRectangle2D.Double(0,0,width,height,20,20);
            path.append(rect,false);
            g2.setClip(path);
            g2.drawImage(image,0,0,null);
            }
            else g.drawString("nihoa",0,0);
        }
        }
        
    }