简体   繁体   中英

White spots appear with text in JFrame

It looks like:

在此处输入图像描述

As seen in the image there are white spots with the text.

My code is:

import javax.swing.*;
import java.awt.*;

public class SubsFrame extends JFrame {
    private String text;
    private JLabel label;
    private byte mode;

    private static SubsFrame instance;

    public static void build(long mode) throws Exception{
        if(instance == null) {
            instance = new SubsFrame(mode);
        }else{
            throw new Exception("Another instance already exists!");
        }
    }

    public static void buildSilent(long mode) throws Exception{
        if(instance == null)
            instance = new SubsFrame(mode);
    }

    public static void showFrame(){
        instance.setVisible(true);
    }

    public static void hideFrame(){
        instance.setVisible(false);
    }

    public static void destroy() {
        instance.dispose();
        instance = null;
    }

    public static void setOpacity(double opacity){
        instance.setBackground(new Color(1.0f,1.0f,1.0f,(float)opacity));
    }

    private SubsFrame(long mode) throws Exception{
        if(JSubsConstants.isValidMode(mode))
            this.mode = (byte)mode;
        else
            throw new Exception("Invalid Mode Selected!");
        if(!this.isAlwaysOnTopSupported()){
            throw new Exception("Always on top is not supported!");
        }
        label = new JLabel("Hello World!          Welcome to this app!");
        label.setFont(new Font("Consolas", Font.PLAIN, 25));
        this.setLayout(new FlowLayout());
        this.add(label);
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setUndecorated(true);
        if(this.mode == JSubsConstants.AUTO_SIZED_FRAME_AUTO_POSITIONED){
            handleFrameSize();
            handleFrameLocation();
        }
        else if(this.mode == JSubsConstants.AUTO_SIZED_FRAME_MANUAL_POSITIONED){
            handleFrameSize();
        }
    }

    private void handleFrameLocation() {
        this.setLocation(250, 350);
    }

    private void handleFrameSize() {
        this.pack();
        this.setSize(this.getWidth() + 20, this.getHeight() + 10);
    }
}

Why are these coming and how can I remove them?

I guess these are to do with the font itself and not Java but I am not sure of that. I tried with some other fonts as well same results.

If you need more information please ask me.

My "suspicion" is the algorithm trying to figure out how the composition should work is having trouble with the anti aliasing of the text.

Generally when I want to do these things, I make the window completely transparent and move the logic to another container instead, this seems to do a better job, generally and eliminates the "oddities" created by the platform and how it handles transparent windows - but that's me.

例子

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TranscluentWindow {

    public static void main(String[] args) {
        new TranscluentWindow();
    }

    public TranscluentWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JWindow frame = new JWindow();
                frame.setAlwaysOnTop(true);
                frame.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2) {
                            SwingUtilities.getWindowAncestor(e.getComponent()).dispose();
                        }
                    }
                });
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.add(new TranslucentPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TranslucentPane extends JPanel {

        private float opacity = 1;

        public TranslucentPane() {
            setOpaque(false);
            setLayout(new BorderLayout());
            JLabel label = new JLabel("Hello World!          Welcome to this app!");
            label.setForeground(Color.RED);
            label.setBorder(new EmptyBorder(8, 8, 8, 8));
            add(label);

            JSlider slider = new JSlider();
            slider.setMinimum(0);
            slider.setMaximum(100);
            slider.setValue(100);
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    setOpacity(slider.getValue() / 100f);
                }
            });

            add(slider, BorderLayout.SOUTH);
        }

        public void setOpacity(float opacity) {
            this.opacity = opacity;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(opacity));
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM