简体   繁体   中英

Drawing an out fading transparent oval

I'm trying to draw an out fading transparent oval to highlight a tile on a tiled screen. Unfortunately I wasn't able to find an implemented feature in Swing that can do that, so I wrote my own:

private void drawGradientedOval(Graphics2D g2d, Rectangle bounds) {

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int loops = (int) Math.min(bounds.getWidth() / 2, bounds.getHeight() / 2);
    int x = bounds.x + (int) (bounds.getWidth() / 2);
    int y = bounds.y + (int) (bounds.getHeight() / 2);
    int scale = 1;
    float alpha = 0.5f;
    float step = alpha / loops;

    for (int i = 0; i < loops; i++) {
        g2d.setColor(new Color(1f, 1f, 1f, alpha));
        g2d.drawOval(x--, y--, scale, scale);
        scale += 2;
        alpha -= step;
    }

}

The resulting oval looks like(the crop is larger than the oval bounds):

在此处输入图片说明

The result is not a smooth oval, there is a "cross" and some other small artifacts.

I assume this is caused by "overlapping" pixels that are drawn on top of each other.

Is there an intelligent way of fixing this?

Please note that I rather avoid implementing a lower level solution like manipulating individual pixels on a Raster level simply because the effort involves a huge amount of variables not mentioned above.

Why not use a RadialGradientPaint object to do your painting? Something like:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import javax.swing.*;

@SuppressWarnings("serial")
public class GradOval extends JPanel {
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;
    private static final Color BG = Color.BLACK;
    private static final float[] FRACTIONS = {0.0f, 1.0f};
    private static final Color[] COLORS = {Color.LIGHT_GRAY, new Color(0, 0, 0, 0)};

    public GradOval() {
        setBackground(BG);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        float cx = getWidth() / 2f;
        float cy = getHeight() / 2f;
        float radius = cx;       
        Paint paint = new RadialGradientPaint(cy, cy, radius, FRACTIONS, COLORS);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(paint);
        g2.fillRect(0, 0, getWidth(), getHeight());        
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        GradOval mainPanel = new GradOval();

        JFrame frame = new JFrame("GradOval");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

This will display as:

在此处输入图片说明

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