Java中的fillArc()不按预期混合颜色 [英] fillArc() in Java not blending color as expected

查看:143
本文介绍了Java中的fillArc()不按预期混合颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用圆弧将所有颜色混合成一个圆。
然而,弧线是一个纯色,并不像我想象的那样是一种颜色的混合。
是否有可能?

  public static void main(String [] args){
DrawingPanel面板=新的DrawingPanel(512,512);
图形g = panel.getGraphics();

int width = 100;
int height = 100;

g.drawOval(0,0,width,height);
// yellow
for(int i = 0; i <100; i ++){
Color c = new Color(255/100 * i,255,0);
g.setColor(c);
g.fillArc(0,0,width,height,95,11);
}


解决方案

  g.fillArc(0,0,宽度,高度,95,11); 

您需要更改每次迭代的弧角并将弧大小固定为某个值。我不确定这个值会是什么,因为我预计你应该重复360次(在这种情况下,大小为1),而不是100次。



你可以只需使用



HSL颜色可以让你改变以度为单位的颜色的色调。所以你只需要一个简单的循环来设置/绘制1度弧的颜色:

  import java.awt。*; 
import java.awt.geom。*;
import java.awt.event。*;
import javax.swing。*;

public class ColorWheel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

HSLColor color = new HSLColor(Color.YELLOW); (int i = 0; i< 360; i ++)
{
g.setColor(color.adjustHue(i))的

;
g.fillArc(25,25,200,200,i,1);
}
}

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

private static void createAndShowGUI()
{
JComponent wheel = new ColorWheel();

JFrame frame = new JFrame(Color Wheel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(wheel);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);


public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});


我使用 HSL颜色因为它有一个简单的API来改变色调。



如果你不想使用那个类,那么你可以使用 Color.getHSBColor(...)方法来获取每个变化程度的颜色。饱和度和亮度再次是固定值,然后你就改变色调。

  @Override 
保护void paintComponent(Graphics g)
{
super.paintComponent G);

float hueDegree = 1 / 360.0f;

(int i = 0; i <360; i ++)
{
Color color = Color.getHSBColor(i * hueDegree,1.0f,1.0f);
g.setColor(color);
g.fillArc(25,25,200,200,i,1);
}
}


I'm trying to blend all the colors into a circle using arcs. However, the arc comes as one solid color and not a blend of color as I thought. Is it possible to?

 public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel(512,512);
    Graphics g = panel.getGraphics(); 

    int width = 100; 
    int height = 100; 

        g.drawOval(0,0,width, height);
        //yellow
        for( int i = 0; i < 100 ; i++){ 
            Color c = new Color(255/100*i,255,0);
            g.setColor(c);
            g.fillArc(0,0,width,height,95,11);
        }

解决方案

g.fillArc(0,0,width,height,95,11);

You need to change the arc angle for every iteration and the arc size should be fixed at a certain value. I'm not sure what the value would be because I would expect you should iterate 360 times (in which case the size would be 1), not 100.

You can use the HSL Color class to do this simply.

An HSL color allows you to change the "hue" of the color in degrees. So you just need a simple loop to set/paint the color in a 1 degree arc:

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class ColorWheel extends JPanel
{
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        HSLColor color = new HSLColor( Color.YELLOW );

        for (int i = 0; i < 360; i++)
        {
            g.setColor( color.adjustHue(i) );
            g.fillArc( 25, 25, 200, 200, i, 1);
        }
    }

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

    private static void createAndShowGUI()
    {
        JComponent wheel = new ColorWheel();

        JFrame frame = new JFrame("Color Wheel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane( wheel );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

I used the HSL Color because it has a simple API to change the hue.

If you don't want to use that class then you can use Color.getHSBColor(...) method to get the color for each degree of change. Again the saturation and brightness would be fixed values and then you just change the hue.

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);

    float hueDegree = 1 / 360.0f;

    for (int i = 0; i < 360; i++)
    {
        Color color = Color.getHSBColor(i * hueDegree, 1.0f, 1.0f);
        g.setColor( color );
        g.fillArc( 25, 25, 200, 200, i, 1);
    }
}

这篇关于Java中的fillArc()不按预期混合颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆