不规则窗体边缘总有锯齿,如何让窗体边缘抗锯齿

解决方案 »

  1.   

    在程序中依次设置以下几个参数:
    设置窗口完全透明:AWTUtilities.setWindowOpaque(frame, false);
    设置窗口无边缘:frame.setUndecorated(true);
    设置窗口的ContentPane为要显示的Pane:frame.setContentPane(myPane);
    在myPane中放置具体要显示的内容,也可以重载paint方法进行Java2D绘制。这些paint会直接发生在桌面背景上。
      

  2.   

    已经成功的代码
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.io.IOException;import javax.swing.JFrame;import com.sun.awt.AWTUtilities;public class Test extends JFrame {
    private static final long serialVersionUID = -6707913364098611652L;
    private Image img;
    private Point origin; public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    makeFrame();
    setVisible(true);
    } private void makeFrame() {
    MediaTracker mt = new MediaTracker(this);
    img = Toolkit.getDefaultToolkit().createImage("a.png");
    mt.addImage(img, 0);
    try {
    mt.waitForAll();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } try {
    initialize();// 窗体初始化
    } catch (IOException e) {
    e.printStackTrace();
    }
    } private void initialize() throws IOException {
    this.setSize(img.getWidth(null), img.getHeight(null));
    this.setUndecorated(true);
    this.origin = new Point(); AWTUtilities.setWindowOpaque(this, false); this.setLocationRelativeTo(null); addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    origin.x = e.getX();
    origin.y = e.getY();
    }
    }); addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
    Point p = getLocation();
    setLocation(p.x + e.getX() - origin.x, p.y + e.getY()
    - origin.y);
    }
    });
    } public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(img, 0, 0, null);
    } public static void main(String[] args) {
    new Test();
    }}
      

  3.   

    http://www.pushing-pixels.org/wp-content/uploads/2008/03/softclippedwindow.java
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.*;public class SoftClippedWindow extends JFrame {
        public SoftClippedWindow() {
    super("Test soft-clipped window"); JButton open = new JButton("Open window");
    open.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
        @Override
    public void run() {
    final JWindow window = new JWindow();
    window.setLayout(new BorderLayout()); try {
        // picture from
        // http://flickr.com/photos/petursey/366204314/
        final BufferedImage avatar = ImageIO
    .read(SoftClippedWindow.class
          .getResource("leaf.jpg"));     JPanel mainPanel = new JPanel(new BorderLayout()) {
        @Override
    protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create(); // code from
    // http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html
    int width = avatar.getWidth();
    int height = avatar.getHeight();
    GraphicsConfiguration gc = g2d
        .getDeviceConfiguration();
    BufferedImage img = gc
        .createCompatibleImage(width,
       height,
       Transparency.TRANSLUCENT);
    Graphics2D g2 = img.createGraphics(); g2.setComposite(AlphaComposite.Clear);
    g2.fillRect(0, 0, width, height); g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);
    g2.fillRoundRect(0, 0, width, height + 10,
     10, 10); g2.setComposite(AlphaComposite.SrcAtop);
    g2.drawImage(avatar, 0, 0, null);
    g2.dispose(); // at this point the 'img' contains a soft
    // clipped round rectangle with the avatar // do the reflection with the code from
    // http://www.jroller.com/gfx/entry/swing_glint
    int avatarWidth = img.getWidth();
    int avatarHeight = img.getHeight(); BufferedImage reflection = new BufferedImage(
         avatarWidth, avatarHeight,
         BufferedImage.TYPE_INT_ARGB);
    Graphics2D reflectionGraphics = reflection
        .createGraphics(); AffineTransform tranform = AffineTransform
        .getScaleInstance(1.0, -1.0);
    tranform.translate(0, -avatarHeight);
    reflectionGraphics.drawImage(img, tranform,
         this); GradientPaint painter = new GradientPaint(
      0.0f, 0.0f, new Color(0.0f, 0.0f,
    0.0f, 0.7f), 0.0f,
      avatarHeight / 2.0f, new Color(
     0.0f, 0.0f, 0.0f, 1.0f)); reflectionGraphics
        .setComposite(AlphaComposite.DstOut);
    reflectionGraphics.setPaint(painter);
    reflectionGraphics
        .fill(new Rectangle2D.Double(0, 0,
     avatarWidth, avatarHeight)); reflectionGraphics.dispose(); g2d.drawImage(img, 0, 0, this);
    g2d.drawImage(reflection, 0, avatarHeight,
          this); g2d.dispose();
        }
    };
        JButton close = new JButton("close");
        close.addActionListener(new ActionListener() {
        @Override
    public void actionPerformed(ActionEvent e) {
    window.dispose();
        }
    });
        JPanel buttonPanel = new JPanel(new FlowLayout(
       FlowLayout.RIGHT));
        buttonPanel.add(close);
        buttonPanel.setOpaque(false);
        buttonPanel.setDoubleBuffered(false);
        mainPanel.add(buttonPanel, BorderLayout.NORTH);     mainPanel.setDoubleBuffered(false);
        mainPanel.setOpaque(false);
        window.add(mainPanel, BorderLayout.CENTER);     window.setSize(180, 200);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        com.sun.awt.AWTUtilities.setWindowOpaque(window,
         false);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
        }
    });
    }
        });
    this.setLayout(new FlowLayout());
    this.add(open); this.setSize(new Dimension(400, 300));
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }    public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    SwingUtilities.invokeLater(new Runnable() {
    @Override
        public void run() {
        Window w = new SoftClippedWindow();
        w.setVisible(true);
    }
        });
        }}
      

  4.   

    恶心的是 GFW 把 Pushing Pixels 拒之墙外
      

  5.   

    呵呵,没实战过,你能上传一下你的a.png嘛?估计是这个出了问题,大概当初是按照gif的方式操作的。比如周围阴影,由于gif没有半透明的效果,只能用灰度,掺和背景色来模拟。所以白底上的黑影就会存为#888888或者类似的灰色。而蓝底上的黑影就会存为#8888CC或者类似的蓝灰色。因此放到不同颜色的背景上,会出现锯齿。而PNG支持半透明(alpha),所以,阴影的颜色只和其透明度有关,比如#CC000000,其中低位的#000000就是平时的黑色,而高位的CC代表alpha值,也就是透明度80%的黑色。
      

  6.   

    BTW,NEVER EVER
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);等价于System.exit(0),也就是整个JVM都shutdown了。多窗口下,或者以后扩展,代码多了,你会后悔死的。DISPOSE_ON_CLOSE,如果只有一个窗口,效果类似EXIT_ON_CLOSE