1

1.重绘时窗口闪烁。我怎样才能消除它?使用更新方法也不起作用。2.如何更新netbeans中所有非最新的类?看起来我的 netbeans 使用了一些旧类(在 jdk 7 之前)。

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JWindow;


public class Splash extends JWindow {

    private boolean mIsRunning;
    private boolean mIsFadingOut;
    volatile static boolean s = true;
    private int mAngle;
    private int mFadeCount;
    private int mFadeLimit = 15;

    Splash(Frame f) {
        super(f);
    }

    public void startt() {
        while (s) {
            repaint();

            mAngle += 3;
            if (mAngle >= 360) {
                mAngle = 0;
            }
            if (mIsFadingOut) {
                if (--mFadeCount == 0) {
                    mIsRunning = false;
                }
            } else if (mFadeCount < mFadeLimit) {
                mFadeCount++;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Splash.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        // Paint the view.
        super.paintComponents(g);
        Graphics2D g2 = (Graphics2D) g.create();
        float fade = (float) mFadeCount / (float) mFadeLimit;
        // Gray it out.
        Composite urComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, .5f * fade));
        g2.fillRect(0, 0, w, h);
        g2.setComposite(urComposite);

        // Paint the wait indicator.
        int s = Math.min(w, h) / 5;
        int cx = w / 2;
        int cy = h / 2;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(
                new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2.setPaint(Color.white);
        g2.rotate(Math.PI * mAngle / 180, cx, cy);
        for (int i = 0; i < 12; i++) {
            float scale = (11.0f - (float) i) / 11.0f;
            g2.drawLine(cx + s, cy, cx + s * 2, cy);
            g2.rotate(-Math.PI / 6, cx, cy);
            g2.setComposite(AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, scale * fade));
        }

        g2.dispose();
    }
}

public void showSplash(){
    final JFrame p = this; //parent
    final  Rectangle s = this.getBounds(); //parent size

    new Thread(){
         public void run(){
             splash = new Splash(p);
             splash.setBounds(s);
             splash.setVisible(true);
             splash.startt();
         }
    }.start();
}

}

4

2 回答 2

4

不要调用Thread.sleep(...)EDT,而是使用示例所示的 Swing Timer。此外,您还需要paintComponent(...)在 JWindow 持有的 JPanel 方法中绘制以使用 Swing 的默认双缓冲。

于 2012-05-09T22:22:00.927 回答
3

问题来自您覆盖 JWindow 的绘制方法这一事实。改写paintComponent()JPanel 的,它有双缓冲:

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JWindow window = new JWindow();
                final Splash splash = new Splash();
                window.add(splash);
                window.setSize(100, 30);
                window.setVisible(true);

                new Thread() {

                    @Override
                    public void run() {
                        splash.startt();
                    }
                }.start();
            }
        });
    }

    public static class Splash extends JPanel {

        private boolean mIsRunning;
        private boolean mIsFadingOut;
        volatile static boolean s = true;
        private int mAngle;
        private int mFadeCount;
        private int mFadeLimit = 30;

        Splash() {
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();

            // Paint the view.
            super.paintComponents(g);
            Graphics2D g2 = (Graphics2D) g.create();
            float fade = (float) mFadeCount / (float) mFadeLimit;
            // Gray it out.
            Composite urComposite = g2.getComposite();
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f * fade));
            g2.fillRect(0, 0, w, h);
            g2.setComposite(urComposite);

            // Paint the wait indicator.
            int s = Math.min(w, h) / 5;
            int cx = w / 2;
            int cy = h / 2;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setStroke(new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g2.setPaint(Color.white);
            g2.rotate(Math.PI * mAngle / 180, cx, cy);
            for (int i = 0; i < 12; i++) {
                float scale = (11.0f - i) / 11.0f;
                g2.drawLine(cx + s, cy, cx + s * 2, cy);
                g2.rotate(-Math.PI / 6, cx, cy);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, scale * fade));
            }

            g2.dispose();
        }

        public void startt() {
            while (s) {

                mAngle += 3;
                if (mAngle >= 360) {
                    mAngle = 0;
                }
                if (mIsFadingOut) {
                    if (--mFadeCount == 0) {
                        mIsRunning = false;
                    }
                } else if (mFadeCount < mFadeLimit) {
                    mFadeCount++;
                }
                repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Splash.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

}
于 2012-05-09T22:23:06.920 回答