Java2D:使形状看起来不那么“平坦”

时间:2018-09-10 03:39:56

标签: java graphics

enter image description here

我正在创造一个命运之轮,例如游戏,让孩子们在线学习英语。问题是图形看起来非常平坦。我想添加一些深度(然后再添加一些花或粒子)。我正在努力吸引8-12岁左右的孩子(此游戏将在在线课程中与老师一起使用)。背景还可以,我更关心轮子的切片。但是任何提示都会很好。

这就是我绘制它们的方式(我使用的是彩虹色):

        int c = 0;

        for (int i = 0; i < this.slices.length; i++) {

            switch (c) {
                case 0: g2d.setPaint(new Color(148, 0, 211)); break;
                case 1: g2d.setPaint(new Color(75, 0, 130)); break;
                case 2: g2d.setPaint(new Color(0, 0, 255)); break;
                case 3: g2d.setPaint(new Color(0, 255, 0)); break;
                case 4: g2d.setPaint(new Color(255, 255, 0)); break;
                case 5: g2d.setPaint(new Color(255, 127, 0)); break;
                case 6: g2d.setPaint(new Color(255, 0, 0)); break;
            }

            c++;
            if (c > 6) c = 0;

            g2d.fill(this.slices[i]);
            g2d.setPaint(new Color(0, 0, 0, 80));
            g2d.draw(this.slices[i]);

        }

我想我可以使用渐变填充使外观看起来更好。我尝试过,但无法使其看起来不错。以下屏幕截图给出了一个酷炫外观的想法。

enter image description here

我该怎么做才能使轮子更深一点,并且基本上使它看起来更凉爽?

1 个答案:

答案 0 :(得分:1)

这是一个非常基本的示例,它使用了相互重叠的多个绘画效果...

Simple

如果我有更多的时间玩耍,我可能会考虑使用更多的RadialGradientPaint而不是LinearGradientPaint,但这可能会为您提供一个起点

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Slice slice = new Slice(100, 200, 60, 40);

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            g2d.translate((getWidth() / 2.0) - slice.getBounds().width,
                    (getHeight() - slice.getBounds().height) / 2.0);
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON
            );
            g2d.setRenderingHints(hints);

            Color primaryColor = Color.MAGENTA;
            Color alphaColor = new Color(primaryColor.getRed(), primaryColor.getGreen(), primaryColor.getBlue(), 0);
            Color shadowColor = new Color(0, 0, 0, 128);

            g2d.setColor(primaryColor);
            g2d.fill(slice);

            RadialGradientPaint rgp = new RadialGradientPaint(
                    new Point2D.Double(slice.getBounds2D().getWidth(), slice.getBounds2D().getHeight() / 2d), 
                    (float)slice.getBounds2D().getHeight() / 2.0f, 
                    new float[] {0.6f, 1.0f}, 
                    new Color[] {alphaColor, shadowColor});
            g2d.setPaint(rgp);
            g2d.fill(slice);

            Graphics2D gScale = (Graphics2D) g2d.create();
            LinearGradientPaint lgp = new LinearGradientPaint(
                    new Point2D.Double(0, 0), 
                    new Point2D.Double(slice.getBounds2D().getWidth(), 0), 
                    new float[]{0f, 1f}, 
                    new Color[] {shadowColor, alphaColor});
            gScale.setPaint(lgp);
            gScale.fill(slice);
            lgp = new LinearGradientPaint(
                    new Point2D.Double(slice.getBounds2D().getWidth(), 0), 
                    new Point2D.Double(slice.getBounds2D().getWidth() * 2, 0), 
                    new float[]{0f, 1f}, 
                    new Color[] {alphaColor, shadowColor});
            gScale.setPaint(lgp);
            gScale.fill(slice);
            gScale.dispose();


            g2d.setPaint(primaryColor.brighter());
            g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g2d.draw(slice);
            g2d.dispose();
        }

    }

    public class Slice extends Path2D.Double {

        public Slice(double width, double height, double range, double innerHeight) {
            moveTo(0, 0);
            append(new Arc2D.Double(0, 0, width, height,
                    90 - (range / 2.0), range,
                    Arc2D.OPEN), false);

            double sliceWidth = width / 2.0;
            double x = (width - sliceWidth) / 2.0;

            append(new Arc2D.Double(x, height, sliceWidth, innerHeight,
                    90 + (range / 2.0), -range,
                    Arc2D.OPEN), true);

            closePath();
        }
    }

}