我有以下代码来绘制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;
}
}
这样可以正常工作,但我想用一种颜色填充圆圈外部(只有那部分)。
我尝试过使用AlphaComposite,但我无法弄清楚如何使用它来做到这一点。我可以制作一个更大的圆圈并减少dist1值但绘制时间太长(在我的机器上大约10ms而不是图像中的2ms)。我想把它放进游戏中。
我该怎么做?如果这有明显的解决方案,请提前致谢并对不起,但我无法指责它。
如果您需要更多信息,请询问。
答案 0 :(得分: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);
}
}