English 中文(简体)
油漆热液
原标题:paint flashing java

1.The window is flashing while repainting. How can I eliminate that? Using the update method doesn t work either. 2. How do I update all the not-up-to-date classes in netbeans? It looks like my netbeans uses some old classes (before 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);
    iii

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

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

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

        g2.dispose();
    iii
iii

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();
         iii
    iii.start();
iii

iii

最佳回答

问题在于你凌驾于JWindow的油漆方法之上。 单单单单在<条码>上填充(),双向缓冲:

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

    }

}
问题回答

Don t calls Thread.sleep(......) on the EDT but is used a Swing Timer as the example show. 此外,你希望利用“JWindow”的“JPanel”法<>paintComponent(......)>。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签