1

1.塗り直し中はウィンドウが点滅します。どうすればそれを排除できますか?updateメソッドの使用も機能しません。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

EDTを呼び出すThread.sleep(...)のではなく、例に示すようにスイングタイマーを使用します。paintComponent(...)また、 Swingのデフォルトのダブルバッファリングを使用するために、JWindowが保持するJPanelのメソッドを描画する必要があります。

于 2012-05-09T22:22:00.927 に答える
3

問題は、JWindowのpaintメソッドをオーバーライドするという事実に起因します。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 に答える