油漆闪烁的java

时间:2012-05-09 22:07:36

标签: java swing paint

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();
}

}

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

问题来自于您覆盖JWindow的paint方法。改为覆盖JPanel的paintComponent(),它具有双缓冲:

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);
                }
            }
        }

    }

}