Java绘图面板?

时间:2014-10-03 07:32:57

标签: java for-loop colors drawing color-wheel

我已经完成了一些色轮的代码,但我对如何完成它感到困惑?我还评论了有助于理解的代码。到目前为止,它打印出红色,橙色和黄色渐变。我该如何完成?

 import java.awt.*;

    public class ColorDrawing2 {
        public static final int CENTER = 256;
        public static final int RADIUS = 120;
        public static final int SHAPES = 32;
        public static final int SIZE = 40;
        public static void main(String[] args) {

            DrawingPanel panel = new DrawingPanel(512,512);         // create a drawing panel
            panel.setBackground(new Color(0,0,0));          // set background color (orange)
            Graphics g = panel.getGraphics();               // get graphics toolkit

            for (int i = 0; i <= SHAPES; i++) {
                                        // System.out.println("i = " + i);
                double angle = (i)*(360/SHAPES)*(Math.PI/180);      // angle
                                        // System.out.println("Angle = " + angle);
                double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
                double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

                int red = (int) (199 + 56/SHAPES*i);            // 199 < red < 255
                                        // System.out.println("Red = " + red);
                int grn = (int) (7.97*i);               // 0   < grn < 255
                                        // System.out.println("Green = " + grn);
                int blu = 0;
                g.setColor(new Color((int) red, grn, blu));
                g.fillOval((int)x, (int)y, SIZE, SIZE);
                panel.sleep(200);                   // pause 200 msec
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

是的,似乎6 for循环应该足够了。也许颜色应该达到255?

答案 1 :(得分:0)

6 for for循环:

for (int i = 0; i <= SHAPES; i++) {
                                // System.out.println("i = " + i);
        double angle = ((i)*(60/SHAPES) +65)*(Math.PI/180);     // angle
                                // System.out.println("Angle = " + angle);
        double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
        double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

        int red = (int) (255);          // 199 < red < 255
                                // System.out.println("Red = " + red);
        int grn = (int) (7.97*i);               // 0   < grn < 255
                                // System.out.println("Green = " + grn);
        int blu = 0;
        g.setColor(new Color((int) red, grn, blu));
        g.fillOval((int)x, (int)y, SIZE, SIZE);
        panel.sleep(0);                 // pause 200 msec
    }
    for (int i = 0; i <= SHAPES; i++) {
                                // System.out.println("i = " + i);
        double angle = ((i)*(60/SHAPES) + 130)*(Math.PI/180);       // angle
                                // System.out.println("Angle = " + angle);
        double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
        double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

        int red = (int) (255 - (255/SHAPES*i));         // 199 < red < 255
                                // System.out.println("Red = " + red);
        int grn = (int) (255);              // 0   < grn < 255
                                // System.out.println("Green = " + grn);
        int blu = 0;
        g.setColor(new Color((int) red, grn, blu));
        g.fillOval((int)x, (int)y, SIZE, SIZE);
        panel.sleep(0);                 // pause 200 msec
    }
    for (int i = 0; i <= SHAPES; i++) {
                                // System.out.println("i = " + i);
        double angle = ((i)*(60/SHAPES) + 185)*(Math.PI/180);       // angle
                                // System.out.println("Angle = " + angle);
        double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
        double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

        int red = (int) (0);            // 199 < red < 255
                                // System.out.println("Red = " + red);
        int grn = (int) (255);              // 0   < grn < 255
                                // System.out.println("Green = " + grn);
        int blu = (int) (7.97*i);
        g.setColor(new Color((int) red, grn, blu));
        g.fillOval((int)x, (int)y, SIZE, SIZE);
        panel.sleep(0);                 // pause 200 msec
    }
    for (int i = 0; i <= SHAPES; i++) {
                                // System.out.println("i = " + i);
        double angle = ((i)*(60/SHAPES) + 250)*(Math.PI/180);       // angle
                                // System.out.println("Angle = " + angle);
        double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
        double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

        int red = (int) (0);            // 199 < red < 255
                                // System.out.println("Red = " + red);
        int grn = (int) (255 - (255/SHAPES*i));             // 0   < grn < 255
                                // System.out.println("Green = " + grn);
        int blu = (int) (255);
        g.setColor(new Color((int) red, grn, blu));
        g.fillOval((int)x, (int)y, SIZE, SIZE);
        panel.sleep(0);                 // pause 200 msec
    }
    for (int i = 0; i <= SHAPES; i++) {
                                // System.out.println("i = " + i);
        double angle = ((i)*(60/SHAPES) + 315)*(Math.PI/180);       // angle
                                // System.out.println("Angle = " + angle);
        double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
        double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

        int red = (int) (7.97*i);           // 199 < red < 255
                                // System.out.println("Red = " + red);
        int grn = (int) (0);                // 0   < grn < 255
                                // System.out.println("Green = " + grn);
        int blu = (int) (255);
        g.setColor(new Color((int) red, grn, blu));
        g.fillOval((int)x, (int)y, SIZE, SIZE);
        panel.sleep(0);                 // pause 200 msec
    }
    for (int i = 0; i <= SHAPES; i++) {
                                // System.out.println("i = " + i);
        double angle = ((i)*(60/SHAPES) +370)*(Math.PI/180);        // angle
                                // System.out.println("Angle = " + angle);
        double x = CENTER - (SIZE/2) + RADIUS*Math.cos(angle);  // x-cooordinate
        double y = CENTER - (SIZE/2) + RADIUS*Math.sin(angle);  // y-coordinate

        int red = (int) (7.97*i);           // 199 < red < 255
                                // System.out.println("Red = " + red);
        int grn = (int) (0);                // 0   < grn < 255
                                // System.out.println("Green = " + grn);
        int blu = (int) (0);
        g.setColor(new Color((int) red, grn, blu));
        g.fillOval((int)x, (int)y, SIZE, SIZE);
        panel.sleep(0);                 // pause 200 msec
    }