如何使秋千上的动画更快,更流畅

时间:2018-10-23 02:22:56

标签: java swing animation

我正在进行一种颜色过渡(从渐变的开始颜色到结束颜色以及从结束颜色到开始颜色(无限期地进行)),但是我找不到实现快速,平滑的颜色过渡的方法,要么平稳但慢速或快速但缓慢,我该怎么办?

变化率是为了移动而改变梯度的极限。

旋转计时器以进行颜色转换:

            Timer t2 = new Timer(200, new ActionListener() { // I want to make this animation to happen faster and smoothly

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Rate of change on timer: " + rateOfChange);
                rateOfChange += 30;
                if (rateOfChange > mainWidth + 100) {
                    rateOfChange = 0;
                    switchEvenOdd++;
                }
                if (switchEvenOdd % 2 == 0)
                    switchGradient = false;
                else
                    switchGradient = true;
                repaint();
            }

        });
        t2.setInitialDelay(0);
        t2.setRepeats(true);
        t2.start();

计时器每秒更新一次时钟:

    Timer timer = new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minutes = calendar.get(Calendar.MINUTE);
                int seconds = calendar.get(Calendar.SECOND);

                hourLeft = (hour / 10) % 10;
                hourRight = (hour % 10);
                minuteLeft = (minutes / 10) % 10;
                minuteRight = (minutes % 10);
                secondLeft = (seconds / 10) % 10;
                secondRight = (seconds % 10);
                // System.out.println("Hour: " + hour + " Minutes: " + minutes + " Seconds: " +
                // seconds);

                dotsIluminated++;

            }
        });
        timer.setInitialDelay(0);
        timer.setRepeats(true);
        timer.start();

渐变定义(使用colorOff跳过渐变):

        private GradientPaint getColor(byte val, int shift) {
        int a = 255 * (val >> shift) & 1;
        if (!switchGradient) {
            if (a == 0)
                return new GradientPaint(0, 0, colorOff, (int) mainWidth, (int) mainHeight, colorOff);
            else {
                actualGradient = new GradientPaint(0, 0, colorStart, 1 + rateOfChange, 1 + rateOfChange, colorEnd);
                return actualGradient;
            }
        } else {
            if (a == 0)
                return new GradientPaint(0, 0, colorOff, (int) mainWidth, (int) mainHeight, colorOff);
            else {
                actualGradient = new GradientPaint(0, 0, colorEnd, 1 + rateOfChange, 1 + rateOfChange, colorStart);
                return actualGradient;
            }
        }
    }

具有更新代码的示例:

(从RGB:29、43、100到RGB:248、205、218的渐变) enter image description here

整个源代码(700行且设计不正确):

public class MainWindow extends JFrame {

private JPanel contentPane;
private CustomRectangle custRect = null;
private JButton btnCancel;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow frame = new MainWindow();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */

public MainWindow() {
    getContentPane().setBackground(Color.BLACK);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setUndecorated(true);
    setSize(1130, 350);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    custRect = new CustomRectangle(1130, 350);
    getContentPane().add(custRect);
    getContentPane().add(getBtnCancel(), BorderLayout.SOUTH);
}

private Color complementaryColor(Color background) {
    int alpha = background.getAlpha();
    int red = background.getRed();
    int blue = background.getBlue();
    int green = background.getGreen();

    // find compliments
    red = (~red) & 0xff;
    blue = (~blue) & 0xff;
    green = (~green) & 0xff;

    return new Color(red, green, blue, alpha);
}

private JButton getBtnCancel() {
    if (btnCancel == null) {
        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnCancel.setBackground(null);
        btnCancel.setBorder(null);
        btnCancel.setForeground(complementaryColor(getContentPane().getBackground()));
        btnCancel.setFocusPainted(false);
    }
    return btnCancel;
}

private class CustomRectangle extends JComponent {

    private byte[] nums = new byte[] { 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B };

    private double mainWidth = 0.0;
    private double mainHeight = 0.0;

    private boolean switchGradient = false;

    private double XbHL;
    private double widthbHL;
    private double YbHL;
    private double heightbHL;

    private double XcHL;
    private double widthcHL;
    private double YcHL;
    private double heightcHL;

    private double[][][] originalCoordinates;

    private double heightcML;
    private double YcML;
    private double widthcML;
    private double XcML;

    private double XbML;
    private double YbML;
    private double widthbML;
    private double heightbML;

    private int hourLeft;
    private int hourRight;
    private int minuteLeft;
    private int minuteRight;
    private int secondLeft;
    private int secondRight;
    private int dotsIluminated = 1;
    private int switchEvenOdd = 0;

    private Color colorStart = new Color(29, 43, 100);
    private Color colorEnd = new Color(248, 205, 218);

    private Color colorOff = new Color(15, 15, 15);

    private int rateOfChange = 0;

    private static final double offset = 140.0;

    private GradientPaint actualGradient;

    public CustomRectangle(int width, int height) {
        mainWidth = width;
        mainHeight = height;

        originalCoordinates = new double[7][2][2];

        Timer timer = new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minutes = calendar.get(Calendar.MINUTE);
                int seconds = calendar.get(Calendar.SECOND);

                hourLeft = (hour / 10) % 10;
                hourRight = (hour % 10);
                minuteLeft = (minutes / 10) % 10;
                minuteRight = (minutes % 10);
                secondLeft = (seconds / 10) % 10;
                secondRight = (seconds % 10);
                // System.out.println("Hour: " + hour + " Minutes: " + minutes + " Seconds: " +
                // seconds);

                repaint();
                dotsIluminated++;

            }
        });
        timer.setInitialDelay(0);
        timer.setRepeats(true);
        timer.start();

        Timer t2 = new Timer(200, new ActionListener() { // I want to make this animation to happen faster and smoothly

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Rate of change on timer: " + rateOfChange);
                rateOfChange += 30;
                if (rateOfChange > mainWidth + 100) {
                    rateOfChange = 0;
                    switchEvenOdd++;
                }
                if (switchEvenOdd % 2 == 0)
                    switchGradient = false;
                else
                    switchGradient = true;
            }

        });
        t2.setInitialDelay(0);
        t2.setRepeats(true);
        t2.start();

    }

    public byte[] getNums() {
        return nums;
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        generalCall(g2d, hourLeft, hourRight, minuteLeft, minuteRight, secondLeft, secondRight);

    }

    public void generalCall(Graphics2D g2d, int hoursLeft, int hoursRight, int minutesLeft, int minutesRight,
            int secondsLeft, int secondsRight) {
        // Hours
        drawAHoursHigh(g2d, nums[hoursLeft], 0);
        drawBHoursHigh(g2d, nums[hoursLeft], 0);
        drawCHoursHigh(g2d, nums[hoursLeft], 0);
        drawDHoursHigh(g2d, nums[hoursLeft], 0);
        drawEHoursHigh(g2d, nums[hoursLeft], 0);
        drawFHoursHigh(g2d, nums[hoursLeft], 0);
        drawGHoursHigh(g2d, nums[hoursLeft], 0);

        drawAHoursLow(g2d, nums[hoursRight], offset);
        drawBHoursLow(g2d, nums[hoursRight], offset);
        drawCHoursLow(g2d, nums[hoursRight], offset);
        drawDHoursLow(g2d, nums[hoursRight], offset);
        drawEHoursLow(g2d, nums[hoursRight], offset);
        drawFHoursLow(g2d, nums[hoursRight], offset);
        drawGHoursLow(g2d, nums[hoursRight], offset);

        drawPointUp(g2d, 2.5 * offset);
        drawPointDown(g2d, 2.5 * offset);

        // Minutes
        drawAMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);
        drawBMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);
        drawCMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);
        drawDMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);
        drawEMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);
        drawFMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);
        drawGMinutesHigh(g2d, nums[minutesLeft], 2.8 * offset);

        drawAMinutesLow(g2d, nums[minutesRight], 3.8 * offset);
        drawBMinutesLow(g2d, nums[minutesRight], 3.8 * offset);
        drawCMinutesLow(g2d, nums[minutesRight], 3.8 * offset);
        drawDMinutesLow(g2d, nums[minutesRight], 3.8 * offset);
        drawEMinutesLow(g2d, nums[minutesRight], 3.8 * offset);
        drawFMinutesLow(g2d, nums[minutesRight], 3.8 * offset);
        drawGMinutesLow(g2d, nums[minutesRight], 3.8 * offset);

        drawPointSUp(g2d, 5.3 * offset);
        drawPointSDown(g2d, 5.3 * offset);

        // Seconds
        drawASecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);
        drawBSecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);
        drawCSecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);
        drawDSecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);
        drawESecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);
        drawFSecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);
        drawGSecondsHigh(g2d, nums[secondsLeft], 5.6 * offset);

        drawASecondsLow(g2d, nums[secondsRight], 6.6 * offset);
        drawBSecondsLow(g2d, nums[secondsRight], 6.6 * offset);
        drawCSecondsLow(g2d, nums[secondsRight], 6.6 * offset);
        drawDSecondsLow(g2d, nums[secondsRight], 6.6 * offset);
        drawESecondsLow(g2d, nums[secondsRight], 6.6 * offset);
        drawFSecondsLow(g2d, nums[secondsRight], 6.6 * offset);
        drawGSecondsLow(g2d, nums[secondsRight], 6.6 * offset);
    }

    private GradientPaint getColor(byte val, int shift) {
        int a = 255 * (val >> shift) & 1;
        if (!switchGradient) {
            if (a == 0)
                return new GradientPaint(0, 0, colorOff, (int) mainWidth, (int) mainHeight, colorOff);
            else {
                actualGradient = new GradientPaint(0, 0, colorStart, 1 + rateOfChange, 1 + rateOfChange, colorEnd);
                return actualGradient;
            }
        } else {
            if (a == 0)
                return new GradientPaint(0, 0, colorOff, (int) mainWidth, (int) mainHeight, colorOff);
            else {
                actualGradient = new GradientPaint(0, 0, colorEnd, 1 + rateOfChange, 1 + rateOfChange, colorStart);
                return actualGradient;
            }
        }
    }

    // Number hours left

    private void drawAHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape a = new RoundRectangle2D.Double(60, 20, 78, 18, 10, 10);

        originalCoordinates[0][0][0] = 60;
        originalCoordinates[0][0][1] = 20;
        originalCoordinates[0][1][0] = 78;
        originalCoordinates[0][1][1] = 18;

        g2d.setPaint(getColor(val, 6));
        g2d.fill(a);
        g2d.draw(a);
    }

    private void drawBHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape b = new RoundRectangle2D.Double(140, 40, 18, 98, 10, 10);

        originalCoordinates[1][0][0] = 140;
        originalCoordinates[1][0][1] = 40;
        originalCoordinates[1][1][0] = 18;
        originalCoordinates[1][1][1] = 98;

        g2d.setPaint(getColor(val, 5));
        g2d.fill(b);
        g2d.draw(b);
    }

    private void drawCHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape c = new RoundRectangle2D.Double(140, 160, 18, 98, 10, 10);

        originalCoordinates[2][0][0] = 140;
        originalCoordinates[2][0][1] = 160;
        originalCoordinates[2][1][0] = 18;
        originalCoordinates[2][1][1] = 98;

        g2d.setPaint(getColor(val, 4));
        g2d.fill(c);
        g2d.draw(c);
    }

    private void drawDHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape d = new RoundRectangle2D.Double(60, 260, 78, 18, 10, 10);

        originalCoordinates[3][0][0] = 60;
        originalCoordinates[3][0][1] = 260;
        originalCoordinates[3][1][0] = 78;
        originalCoordinates[3][1][1] = 18;

        g2d.setPaint(getColor(val, 3));
        g2d.fill(d);
        g2d.draw(d);
    }

    private void drawEHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape e = new RoundRectangle2D.Double(40, 160, 18, 98, 10, 10);

        originalCoordinates[4][0][0] = 40;
        originalCoordinates[4][0][1] = 160;
        originalCoordinates[4][1][0] = 18;
        originalCoordinates[4][1][1] = 98;

        g2d.setPaint(getColor(val, 2));
        g2d.fill(e);
        g2d.draw(e);
    }

    private void drawFHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape f = new RoundRectangle2D.Double(40, 40, 18, 98, 10, 10);

        originalCoordinates[5][0][0] = 40;
        originalCoordinates[5][0][1] = 40;
        originalCoordinates[5][1][0] = 18;
        originalCoordinates[5][1][1] = 98;

        g2d.setPaint(getColor(val, 1));
        g2d.fill(f);
        g2d.draw(f);
    }

    private void drawGHoursHigh(Graphics2D g2d, byte val, double offset) {
        Shape g = new RoundRectangle2D.Double(60, 140, 78, 18, 10, 10);

        originalCoordinates[6][0][0] = 60;
        originalCoordinates[6][0][1] = 140;
        originalCoordinates[6][1][0] = 78;
        originalCoordinates[6][1][1] = 18;

        g2d.setPaint(getColor(val, 0));
        g2d.fill(g);
        g2d.draw(g);
    }

    // Number Hours Right

    private void drawAHoursLow(Graphics2D g2d, byte val, double offset) {
        Shape a = new RoundRectangle2D.Double(originalCoordinates[0][0][0] + offset, originalCoordinates[0][0][1],
                originalCoordinates[0][1][0], originalCoordinates[0][1][1], 10, 10);

        g2d.setPaint(getColor(val, 6));
        g2d.fill(a);
        g2d.draw(a);
    }

    private void drawBHoursLow(Graphics2D g2d, byte val, double offset) {
        XbHL = originalCoordinates[1][0][0] + offset;
        widthbHL = originalCoordinates[1][1][0];
        YbHL = originalCoordinates[1][0][1];
        heightbHL = originalCoordinates[1][1][1];

        Shape b = new RoundRectangle2D.Double(XbHL, YbHL, widthbHL, heightbHL, 10, 10);
        g2d.setPaint(getColor(val, 5));
        g2d.fill(b);
        g2d.draw(b);
    }

    private void drawCHoursLow(Graphics2D g2d, byte val, double offset) {
        XcHL = originalCoordinates[2][0][0] + offset;
        widthcHL = originalCoordinates[2][1][0];
        YcHL = originalCoordinates[2][0][1];
        heightcHL = originalCoordinates[2][1][1];

        Shape c = new RoundRectangle2D.Double(XcHL, YcHL, widthcHL, heightcHL, 10, 10);
        g2d.setPaint(getColor(val, 4));
        g2d.fill(c);
        g2d.draw(c);
    }

    private void drawDHoursLow(Graphics2D g2d, byte val, double offset) {
        Shape d = new RoundRectangle2D.Double(originalCoordinates[3][0][0] + offset, originalCoordinates[3][0][1],
                originalCoordinates[3][1][0], originalCoordinates[3][1][1], 10, 10);
        g2d.setPaint(getColor(val, 3));
        g2d.fill(d);
        g2d.draw(d);
    }

    private void drawEHoursLow(Graphics2D g2d, byte val, double offset) {
        Shape e = new RoundRectangle2D.Double(originalCoordinates[4][0][0] + offset, originalCoordinates[4][0][1],
                originalCoordinates[4][1][0], originalCoordinates[4][1][1], 10, 10);
        g2d.setPaint(getColor(val, 2));
        g2d.fill(e);
        g2d.draw(e);
    }

    private void drawFHoursLow(Graphics2D g2d, byte val, double offset) {
        Shape f = new RoundRectangle2D.Double(originalCoordinates[5][0][0] + offset, originalCoordinates[5][0][1],
                originalCoordinates[5][1][0], originalCoordinates[5][1][1], 10, 10);
        g2d.setPaint(getColor(val, 1));
        g2d.fill(f);
        g2d.draw(f);
    }

    private void drawGHoursLow(Graphics2D g2d, byte val, double offset) {
        Shape g = new RoundRectangle2D.Double(originalCoordinates[6][0][0] + offset, originalCoordinates[6][0][1],
                originalCoordinates[6][1][0], originalCoordinates[6][1][1], 10, 10);
        g2d.setPaint(getColor(val, 0));
        g2d.fill(g);
        g2d.draw(g);
    }

    // 2 Points

    private void drawPointUp(Graphics2D g2d, double offset) {
        double radius = widthbHL / 2.0;
        double diameter = radius * 2;

        if (dotsIluminated % 2 == 0)
            g2d.setPaint(actualGradient);
        else
            g2d.setPaint(colorOff);
        Shape circle = new Ellipse2D.Double(radius + offset, (heightbHL / 2.0) - radius + YbHL, diameter, diameter);
        g2d.fill(circle);
        g2d.draw(circle);
    }

    private void drawPointDown(Graphics2D g2d, double offset) {

        double radius = widthcHL / 2.0;
        double diameter = radius * 2;
        if (dotsIluminated % 2 == 0)
            g2d.setPaint(actualGradient);
        else
            g2d.setPaint(colorOff);
        Shape circle = new Ellipse2D.Double(radius + offset, (heightcHL / 2.0) - radius + YcHL, diameter, diameter);
        g2d.fill(circle);
        g2d.draw(circle);

    }

    // Number Minutes High

    private void drawAMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape a = new RoundRectangle2D.Double(originalCoordinates[0][0][0] + offset, originalCoordinates[0][0][1],
                originalCoordinates[0][1][0], originalCoordinates[0][1][1], 10, 10);
        g2d.setPaint(getColor(val, 6));
        g2d.fill(a);
        g2d.draw(a);
    }

    private void drawBMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape b = new RoundRectangle2D.Double(originalCoordinates[1][0][0] + offset, originalCoordinates[1][0][1],
                originalCoordinates[1][1][0], originalCoordinates[1][1][1], 10, 10);
        g2d.setPaint(getColor(val, 5));
        g2d.fill(b);
        g2d.draw(b);
    }

    private void drawCMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape c = new RoundRectangle2D.Double(originalCoordinates[2][0][0] + offset, originalCoordinates[2][0][1],
                originalCoordinates[2][1][0], originalCoordinates[2][1][1], 10, 10);
        g2d.setPaint(getColor(val, 4));
        g2d.fill(c);
        g2d.draw(c);
    }

    private void drawDMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape d = new RoundRectangle2D.Double(originalCoordinates[3][0][0] + offset, originalCoordinates[3][0][1],
                originalCoordinates[3][1][0], originalCoordinates[3][1][1], 10, 10);
        g2d.setPaint(getColor(val, 3));
        g2d.fill(d);
        g2d.draw(d);
    }

    private void drawEMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape e = new RoundRectangle2D.Double(originalCoordinates[4][0][0] + offset, originalCoordinates[4][0][1],
                originalCoordinates[4][1][0], originalCoordinates[4][1][1], 10, 10);
        g2d.setPaint(getColor(val, 2));
        g2d.fill(e);
        g2d.draw(e);
    }

    private void drawFMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape f = new RoundRectangle2D.Double(originalCoordinates[5][0][0] + offset, originalCoordinates[5][0][1],
                originalCoordinates[5][1][0], originalCoordinates[5][1][1], 10, 10);
        g2d.setPaint(getColor(val, 1));
        g2d.fill(f);
        g2d.draw(f);
    }

    private void drawGMinutesHigh(Graphics2D g2d, byte val, double offset) {
        Shape g = new RoundRectangle2D.Double(originalCoordinates[6][0][0] + offset, originalCoordinates[6][0][1],
                originalCoordinates[6][1][0], originalCoordinates[6][1][1], 10, 10);
        g2d.setPaint(getColor(val, 0));
        g2d.fill(g);
        g2d.draw(g);
    }

    // Number Minutes Low

    private void drawAMinutesLow(Graphics2D g2d, byte val, double offset) {
        Shape a = new RoundRectangle2D.Double(originalCoordinates[0][0][0] + offset, originalCoordinates[0][0][1],
                originalCoordinates[0][1][0], originalCoordinates[0][1][1], 10, 10);
        g2d.setPaint(getColor(val, 6));
        g2d.fill(a);
        g2d.draw(a);
    }

    private void drawBMinutesLow(Graphics2D g2d, byte val, double offset) {
        XbML = originalCoordinates[1][0][0] + offset;
        widthbML = originalCoordinates[1][1][0];
        YbML = originalCoordinates[1][0][1];
        heightbML = originalCoordinates[1][1][1];

        Shape b = new RoundRectangle2D.Double(XbML, YbML, widthbML, heightbML, 10, 10);
        g2d.setPaint(getColor(val, 5));
        g2d.fill(b);
        g2d.draw(b);
    }

    private void drawCMinutesLow(Graphics2D g2d, byte val, double offset) {
        XcML = originalCoordinates[2][0][0] + offset;
        widthcML = originalCoordinates[2][1][0];
        YcML = originalCoordinates[2][0][1];
        heightcML = originalCoordinates[2][1][1];

        Shape c = new RoundRectangle2D.Double(XcML, YcML, widthcML, heightcML, 10, 10);
        g2d.setPaint(getColor(val, 4));
        g2d.fill(c);
        g2d.draw(c);
    }

    private void drawDMinutesLow(Graphics2D g2d, byte val, double offset) {
        Shape d = new RoundRectangle2D.Double(originalCoordinates[3][0][0] + offset, originalCoordinates[3][0][1],
                originalCoordinates[3][1][0], originalCoordinates[3][1][1], 10, 10);
        g2d.setPaint(getColor(val, 3));
        g2d.fill(d);
        g2d.draw(d);
    }

    private void drawEMinutesLow(Graphics2D g2d, byte val, double offset) {
        Shape e = new RoundRectangle2D.Double(originalCoordinates[4][0][0] + offset, originalCoordinates[4][0][1],
                originalCoordinates[4][1][0], originalCoordinates[4][1][1], 10, 10);
        g2d.setPaint(getColor(val, 2));
        g2d.fill(e);
        g2d.draw(e);
    }

    private void drawFMinutesLow(Graphics2D g2d, byte val, double offset) {
        Shape f = new RoundRectangle2D.Double(originalCoordinates[5][0][0] + offset, originalCoordinates[5][0][1],
                originalCoordinates[5][1][0], originalCoordinates[5][1][1], 10, 10);
        g2d.setPaint(getColor(val, 1));
        g2d.fill(f);
        g2d.draw(f);
    }

    private void drawGMinutesLow(Graphics2D g2d, byte val, double offset) {
        Shape g = new RoundRectangle2D.Double(originalCoordinates[6][0][0] + offset, originalCoordinates[6][0][1],
                originalCoordinates[6][1][0], originalCoordinates[6][1][1], 10, 10);
        g2d.setPaint(getColor(val, 0));
        g2d.fill(g);
        g2d.draw(g);
    }

    private void drawPointSUp(Graphics2D g2d, double offset) {
        double radius = widthbML / 2.0;
        double diameter = radius * 2;

        if (dotsIluminated % 2 == 0)
            g2d.setPaint(actualGradient);
        else
            g2d.setPaint(colorOff);
        Shape circle = new Ellipse2D.Double(radius + offset, (heightbML / 2.0) - radius + YbML, diameter, diameter);
        g2d.fill(circle);
        g2d.draw(circle);
    }

    private void drawPointSDown(Graphics2D g2d, double offset) {
        double radius = widthcML / 2.0;
        double diameter = radius * 2;
        if (dotsIluminated % 2 == 0)
            g2d.setPaint(actualGradient);
        else
            g2d.setPaint(colorOff);
        Shape circle = new Ellipse2D.Double(radius + offset, (heightcML / 2.0) - radius + YcML, diameter, diameter);
        g2d.fill(circle);
        g2d.draw(circle);
    }

    // Number Seconds High

    private void drawASecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape a = new RoundRectangle2D.Double(originalCoordinates[0][0][0] + offset, originalCoordinates[0][0][1],
                originalCoordinates[0][1][0], originalCoordinates[0][1][1], 10, 10);
        g2d.setPaint(getColor(val, 6));
        g2d.fill(a);
        g2d.draw(a);
    }

    private void drawBSecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape b = new RoundRectangle2D.Double(originalCoordinates[1][0][0] + offset, originalCoordinates[1][0][1],
                originalCoordinates[1][1][0], originalCoordinates[1][1][1], 10, 10);
        g2d.setPaint(getColor(val, 5));
        g2d.fill(b);
        g2d.draw(b);
    }

    private void drawCSecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape c = new RoundRectangle2D.Double(originalCoordinates[2][0][0] + offset, originalCoordinates[2][0][1],
                originalCoordinates[2][1][0], originalCoordinates[2][1][1], 10, 10);
        g2d.setPaint(getColor(val, 4));
        g2d.fill(c);
        g2d.draw(c);
    }

    private void drawDSecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape d = new RoundRectangle2D.Double(originalCoordinates[3][0][0] + offset, originalCoordinates[3][0][1],
                originalCoordinates[3][1][0], originalCoordinates[3][1][1], 10, 10);
        g2d.setPaint(getColor(val, 3));
        g2d.fill(d);
        g2d.draw(d);
    }

    private void drawESecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape e = new RoundRectangle2D.Double(originalCoordinates[4][0][0] + offset, originalCoordinates[4][0][1],
                originalCoordinates[4][1][0], originalCoordinates[4][1][1], 10, 10);
        g2d.setPaint(getColor(val, 2));
        g2d.fill(e);
        g2d.draw(e);
    }

    private void drawFSecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape f = new RoundRectangle2D.Double(originalCoordinates[5][0][0] + offset, originalCoordinates[5][0][1],
                originalCoordinates[5][1][0], originalCoordinates[5][1][1], 10, 10);
        g2d.setPaint(getColor(val, 1));
        g2d.fill(f);
        g2d.draw(f);
    }

    private void drawGSecondsHigh(Graphics2D g2d, byte val, double offset) {
        Shape g = new RoundRectangle2D.Double(originalCoordinates[6][0][0] + offset, originalCoordinates[6][0][1],
                originalCoordinates[6][1][0], originalCoordinates[6][1][1], 10, 10);
        g2d.setPaint(getColor(val, 0));
        g2d.fill(g);
        g2d.draw(g);
    }

    // Number Seconds Low

    private void drawASecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape a = new RoundRectangle2D.Double(originalCoordinates[0][0][0] + offset, originalCoordinates[0][0][1],
                originalCoordinates[0][1][0], originalCoordinates[0][1][1], 10, 10);
        g2d.setPaint(getColor(val, 6));
        g2d.fill(a);
        g2d.draw(a);
    }

    private void drawBSecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape b = new RoundRectangle2D.Double(originalCoordinates[1][0][0] + offset, originalCoordinates[1][0][1],
                originalCoordinates[1][1][0], originalCoordinates[1][1][1], 10, 10);
        g2d.setPaint(getColor(val, 5));
        g2d.fill(b);
        g2d.draw(b);
    }

    private void drawCSecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape c = new RoundRectangle2D.Double(originalCoordinates[2][0][0] + offset, originalCoordinates[2][0][1],
                originalCoordinates[2][1][0], originalCoordinates[2][1][1], 10, 10);
        g2d.setPaint(getColor(val, 4));
        g2d.fill(c);
        g2d.draw(c);
    }

    private void drawDSecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape d = new RoundRectangle2D.Double(originalCoordinates[3][0][0] + offset, originalCoordinates[3][0][1],
                originalCoordinates[3][1][0], originalCoordinates[3][1][1], 10, 10);
        g2d.setPaint(getColor(val, 3));
        g2d.fill(d);
        g2d.draw(d);
    }

    private void drawESecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape e = new RoundRectangle2D.Double(originalCoordinates[4][0][0] + offset, originalCoordinates[4][0][1],
                originalCoordinates[4][1][0], originalCoordinates[4][1][1], 10, 10);
        g2d.setPaint(getColor(val, 2));
        g2d.fill(e);
        g2d.draw(e);
    }

    private void drawFSecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape f = new RoundRectangle2D.Double(originalCoordinates[5][0][0] + offset, originalCoordinates[5][0][1],
                originalCoordinates[5][1][0], originalCoordinates[5][1][1], 10, 10);
        g2d.setPaint(getColor(val, 1));
        g2d.fill(f);
        g2d.draw(f);
    }

    private void drawGSecondsLow(Graphics2D g2d, byte val, double offset) {
        Shape g = new RoundRectangle2D.Double(originalCoordinates[6][0][0] + offset, originalCoordinates[6][0][1],
                originalCoordinates[6][1][0], originalCoordinates[6][1][1], 10, 10);
        g2d.setPaint(getColor(val, 0));
        g2d.fill(g);
        g2d.draw(g);
    }
}

}

1 个答案:

答案 0 :(得分:0)

  

我找不到快速而平滑的颜色过渡的方法,无论是平滑但缓慢还是快速但缓慢,我该怎么办?

在我看来,您有两个计时器,一个用于更改时间,另一个用于更改坡度。

但是,您为第二个计时器发布的代码实际上并没有执行任何操作。您要做的就是设置一些变量的值。您从未在任何地方使用新变量对组件进行过任何重新绘制,因此,随着时间的变化,梯度仅每秒变化一次。

因此,我建议您需要向第二个计时器的tasktopic添加repaint()。除了每秒绘制一次时间之外,这还将允许您以指定的时间间隔进行渐变更改。