gpt4 book ai didi

Java绘制RadialGradientPaint圆切成矩形

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:09:59 27 4
gpt4 key购买 nike

我有以下代码来绘制 RadialGradientPaint 圆:

public class Character {
public static void draw(Graphics2D g2d){
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, 1000, 750);
drawVisibilityPolygon(g2d);
}

private static void drawVisibilityPolygon(Graphics2D g2d){
Point center = new Point(1000 / 2, 750 / 2);
float radius = 200;
float[] dist = {
0f,
1f
};
Color[] colors = {
new Color(0, 0, 0, 0),
new Color(0, 0, 0, 255)
};
drawGradientCircle(g2d, radius, dist, colors, center);
}

private static void drawGradientCircle(Graphics2D g2d, float radius, float[] dist, Color[] colors, Point2D center){
RadialGradientPaint rgp = new RadialGradientPaint(center, radius, dist, colors);
g2d.setPaint(rgp);
g2d.fill(new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, radius * 2, radius * 2));
}
}

draw 方法由 Display 类调用:

public class Display extends JPanel {
@Override
public void paintComponent(Graphics g){
// SETUP
BufferedImage base = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = base.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());

// DRAW STUFF
Character.draw(g2d);

// FINISH UP
g.drawImage(base, 0, 0, null);
g2d.dispose();
g.dispose();
}
}

显示类包含在 JFrame 中:

public class Window extends JFrame {
private Display display = new Display();

public Window(String title, int width, int height){
super(title);
add(display);
setSize(width, height);
setIgnoreRepaint(false);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}

public Display getDisplay() {
return display;
}
}

enter image description here

这很好用,但我想用一种颜色填充圆圈外的部分(并且只有那部分)。

我尝试过使用 AlphaComposite,但我不知道如何使用它来完成此操作。我可以做一个更大的圆圈并减小 dist1 值,但是绘制时间太长(在我的机器上大约 10 毫秒,而不是图像中圆圈的 2 毫秒)。我正在尝试将其放入游戏中。

我该怎么做?提前致谢,如果这有一个明显的解决方案,我深表歉意,但我无法确定。

如果您需要更多信息,请询问。

最佳答案

用非不透明颜色绘画会导致显示背景,所以你必须设置背景,所以你必须设置合适的背景。
看看这个演示:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class APanel extends JPanel{

APanel(){

setPreferredSize(new Dimension(600,600));
setBackground(Color.YELLOW);
setBorder(new TitledBorder(new LineBorder(new Color(169, 169, 169)),
"Gradient Circle", TitledBorder.LEADING, TitledBorder.BELOW_TOP, null, null));
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw((Graphics2D) g);
}

public static void draw(Graphics2D g2d){

g2d.setColor(Color.RED);
g2d.fillRect(100,100, 400, 400);
drawVisibilityCircle(g2d);
}

private static void drawVisibilityCircle(Graphics2D g2d){
Point center = new Point(300, 300);
float radius = 200;
float[] dist = { 0f, 1f};
Color[] colors = { new Color(0, 0, 0, 0), new Color(0, 0, 0, 255)};
//workaround to prevent background color from showing
drawBackGroundCircle(g2d, radius, Color.WHITE, center);
drawGradientCircle(g2d, radius, dist, colors, center);
}

private static void drawBackGroundCircle(Graphics2D g2d, float radius, Color color, Point2D center){

g2d.setColor(color);
radius -= 1;//make radius a bit smaller to prevent fuzzy edge
g2d.fill(new Ellipse2D.Double(center.getX() - radius, center.getY()
- radius, radius * 2, radius * 2));
}

private static void drawGradientCircle(Graphics2D g2d, float radius, float[] dist, Color[] colors, Point2D center){
RadialGradientPaint rgp = new RadialGradientPaint(center, radius, dist, colors);
g2d.setPaint(rgp);
g2d.fill(new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, radius * 2, radius * 2));
}

public static void main(String[] args){

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel panel = new APanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

enter image description here

关于Java绘制RadialGradientPaint圆切成矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47230738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com