【问题标题】:Change Hue of Picture in Graphics更改图形中图片的色调
【发布时间】:2014-04-08 10:47:08
【问题描述】:

好的,所以我需要帮助更改此滑块的色调。我似乎无法弄清楚。请不要@override。我需要可以在 Ready to Program 上运行的东西。当滑块回到 0 时,色调将恢复正常。我不需要太复杂。只需一个简单的色调滑块就可以了。谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import javax.swing.event.*;
import java.applet.*;

public class test extends Applet implements ActionListener, ChangeListener
{

    //Widgets, Panels
    JSlider slider;
    Panel flow;
    int colorr;
    int colorg;
    int colorb;
    int stars;
    //House Coordinates, initialized to 1. (Top Right and No Scaling)


    public void init ()
    { //Set Up Input Fields for House Coordinates
        resize (380, 240);
        setBackground (new Color (102, 179, 255));

        slider = new JSlider ();
        slider.setValue (0);
        slider.setBackground (new Color (102, 179, 255));
        slider.setForeground (Color.white);
        slider.setMajorTickSpacing (20);
        slider.setMinorTickSpacing (5);
        slider.setPaintTicks (true);
        slider.addChangeListener (this);


        //Set up layout, add widgets
        setLayout (new BorderLayout ());
        flow = new Panel (new FlowLayout ());
        flow.add (slider);
        add (flow, "South");

    }


    public void paint (Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        Color color1 = getBackground ();
        Color color2 = color1.darker ();
        int x = getWidth ();
        int y = getHeight () - 30;
        GradientPaint gp = new GradientPaint (
                0, 0, color1,
                0, y, color2);

        g2d.setPaint (gp);
        g2d.fillRect (0, 0, x, y);

        stars (10, 10);
    }


    public void stars (int x, int y)
    {
        Graphics g = getGraphics ();

        //sun
        g.setColor (new Color (139, 166, 211));
        g.fillOval (-200, 170, 1000, 400);

        g.setColor (new Color (206, 75, 239));
        g.fillOval (x, y, 10, 10);                      //First medium star
        g.drawLine (x + 5, y, x + 5, 0);
        g.drawLine (x, y + 5, 0, y + 5);
        g.drawLine (x + 5, y + 10, x + 5, y + 20);
        g.drawLine (x + 10, y + 5, x + 20, y + 5);

        g.fillOval (x + 80, y + 30, 12, 12);            //Middle medium star
        g.drawLine (x + 86, y + 30, x + 86, y + 18);
        g.drawLine (x + 80, y + 36, x + 68, y + 36);
        g.drawLine (x + 92, y + 36, x + 104, y + 36);
        g.drawLine (x + 86, y + 42, x + 86, y + 52);

        colorr = (int) (Math.random () * 255) + 1;
        colorg = (int) (Math.random () * 255) + 1;
        colorb = (int) (Math.random () * 255) + 1;
        int randomx = (int) (Math.random () * 300) + 10;
        int randomy = (int) (Math.random () * 150) + 10;

        stars = 50; //Change for more stars

        int ax[] = new int [stars];
        int ay[] = new int [stars];

        for (int i = 0 ; i < stars ; i++)
        {
            g.setColor (new Color (colorr, colorg, colorb));
            colorr = (int) (Math.random () * 255) + 1;
            colorg = (int) (Math.random () * 255) + 1;
            colorb = (int) (Math.random () * 255) + 1;

            while ((randomx > 88 && randomx < 116) && (randomy < 65 && randomy > 15))
            {
                randomx = (int) (Math.random () * 300) + 10;
                randomy = (int) (Math.random () * 150) + 10;
            }

            while ((randomx > 0 && randomx < 25) && (randomy > 5 && randomy < 35))
            {
                randomx = (int) (Math.random () * 300) + 10;
                randomy = (int) (Math.random () * 150) + 10;
            }
            g.drawOval (randomx, randomy, 5, 5);
            randomx = (int) (Math.random () * 300) + 10;
            randomy = (int) (Math.random () * 150) + 10;
        }

        g.setColor (Color.white);
        g.drawLine (320, 0, 315, 40);
        g.drawLine (320, 0, 325, 40);
        g.drawLine (320, 120, 315, 80);
        g.drawLine (320, 120, 325, 80);
        g.drawLine (260, 60, 300, 55);
        g.drawLine (260, 60, 300, 65);
        g.drawLine (380, 60, 340, 55);
        g.drawLine (380, 60, 340, 65);
        fillGradOval (280, 20, 80, 80, new Color (254, 238, 44), new Color (255, 251, 191), g);

        g.setColor (new Color (255, 251, 191));
        fillGradOval (300, 40, 40, 40, new Color (255, 251, 191), new Color (254, 238, 44), g);
    }


    public void fillGradOval (int X, int Y, int H2, int W2, Color c1, Color c2, Graphics g)
    {
        g.setColor (c1);
        g.fillOval (X, Y, W2, H2);
        Color Gradient = c1;
        float red = (c2.getRed () - c1.getRed ()) / (W2 / 2);
        float blue = (c2.getBlue () - c1.getBlue ()) / (W2 / 2);
        float green = (c2.getGreen () - c1.getGreen ()) / (W2 / 2);

        int scale = 1;
        int r = c1.getRed ();
        int gr = c1.getGreen ();
        int b = c1.getBlue ();

        while (W2 > 10)
        {
            r = (int) (r + red);
            gr = (int) (gr + green);
            b = (int) (b + blue);

            Gradient = new Color (r, gr, b);
            g.setColor (Gradient);

            W2 = W2 - 2 * scale;
            H2 = H2 - 2 * scale;
            X = X + scale;
            Y = Y + scale;

            g.fillOval (X, Y, W2, H2);
        }
    }


    public void actionPerformed (ActionEvent e)
    {

    }


    public void stateChanged (ChangeEvent e)
    {
        JSlider source = (JSlider) e.getSource ();
        if (!source.getValueIsAdjusting ())
        {

        }
    }
}

【问题讨论】:

  • “请不要@override。” 您的示例已经覆盖了方法。 “我需要可以在 Ready to Program 上运行的东西。” 那是什么?
  • 1) 为什么要编写小程序?如果是由于规范。老师请发给Why CS teachers should stop teaching Java applets。 2) 不要无故混用 Swing 和 AWT。所以Applet -> JAppletPanel -> JPanel..
  • 不要破坏你自己的问题,也不要破坏我的回答!如果您对我的回答有任何疑问,请在评论中提出。如果问题需要澄清,请随意编辑,但要合理编辑!
  • 停止填充编辑! :-/
  • 我很仁慈,会给你小费。 1)您在这里发布您的私人代码是错误的。发布到 SO 的所有代码都应该是公开的,如果您稍后尝试对其进行编辑,人们将简单地回滚您的编辑(就像我现在所做的那样,多次)。 2) 但是您可以删除您的问题。声望足够高的人。仍然可以看到它,但它不会出现在网站搜索、主要问题列表或您的个人资料中。因此,如果您真的想要,请删除该问题。那么我的回答也会消失。 3)但我不喜欢那样,所以不要指望我将来会帮助你。

标签: java swing colors jslider hue


【解决方案1】:

我不确定你指的是什么“颜色”,所以我做了一些猜测。这是一个混合应用程序/小程序(更容易开发和测试),它将底部面板的颜色以及渐变涂料的底部颜色与使用滑块定义的色调相关联。

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

/* <applet code=HueSlider width=380 height=240></applet> */
public class HueSlider extends JApplet
{
    public void init() {
        add(new HueSliderGui());
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                HueSliderGui hsg = new HueSliderGui();
                JOptionPane.showMessageDialog(null, hsg);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class HueSliderGui extends JPanel implements ChangeListener {

    //Widgets, Panels
    JSlider slider;
    JPanel flow;
    int colorr;
    int colorg;
    int colorb;
    Color bg = new Color (102, 179, 255);
    int stars;
    //House Coordinates, initialized to 1. (Top Right and No Scaling)
    Dimension prefSize = new Dimension(380, 240);

    HueSliderGui() {
        initGui();
    }

    public void initGui()
    {
        //Set Up Input Fields for House Coordinates
        // an applet size is set in HTML
        //resize (380, 240);
        setBackground (bg);

        slider = new JSlider ();
        slider.setValue (0);
        slider.setBackground (new Color (102, 179, 255));
        slider.setForeground (Color.white);
        slider.setMajorTickSpacing (20);
        slider.setMinorTickSpacing (5);
        slider.setPaintTicks (true);
        slider.addChangeListener (this);


        //Set up layout, add widgets
        setLayout (new BorderLayout ());
        flow = new JPanel (new FlowLayout ());
        flow.add (slider);
        add (flow, "South");
        validate();
    }

    @Override
    public Dimension getPreferredSize() {
        return prefSize;
    }


    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Color color1 = getBackground ();
        Color color2 = color1.darker ();
        int x = getWidth ();
        int y = getHeight () - 30;
        GradientPaint gp = new GradientPaint (
                0, 0, color1,
                0, y, flow.getBackground());

        g2d.setPaint (gp);
        g2d.fillRect (0, 0, x, y);

        stars (10, 10, g2d);
    }

    public void stars (int x, int y, Graphics g)
    {
//        Graphics g = getGraphics ();  we should never call getGraphics

        //sun
        g.setColor (new Color (139, 166, 211));
        g.fillOval (-200, 170, 1000, 400);

        g.setColor (new Color (206, 75, 239));
        g.fillOval (x, y, 10, 10);                      //First medium star
        g.drawLine (x + 5, y, x + 5, 0);
        g.drawLine (x, y + 5, 0, y + 5);
        g.drawLine (x + 5, y + 10, x + 5, y + 20);
        g.drawLine (x + 10, y + 5, x + 20, y + 5);

        g.fillOval (x + 80, y + 30, 12, 12);            //Middle medium star
        g.drawLine (x + 86, y + 30, x + 86, y + 18);
        g.drawLine (x + 80, y + 36, x + 68, y + 36);
        g.drawLine (x + 92, y + 36, x + 104, y + 36);
        g.drawLine (x + 86, y + 42, x + 86, y + 52);

        colorr = (int) (Math.random () * 255) + 1;
        colorg = (int) (Math.random () * 255) + 1;
        colorb = (int) (Math.random () * 255) + 1;
        int randomx = (int) (Math.random () * 300) + 10;
        int randomy = (int) (Math.random () * 150) + 10;

        stars = 50; //Change for more stars

        int ax[] = new int [stars];
        int ay[] = new int [stars];

        for (int i = 0 ; i < stars ; i++)
        {
            g.setColor (new Color (colorr, colorg, colorb));
            colorr = (int) (Math.random () * 255) + 1;
            colorg = (int) (Math.random () * 255) + 1;
            colorb = (int) (Math.random () * 255) + 1;

            while ((randomx > 88 && randomx < 116) && (randomy < 65 && randomy > 15))
            {
                randomx = (int) (Math.random () * 300) + 10;
                randomy = (int) (Math.random () * 150) + 10;
            }

            while ((randomx > 0 && randomx < 25) && (randomy > 5 && randomy < 35))
            {
                randomx = (int) (Math.random () * 300) + 10;
                randomy = (int) (Math.random () * 150) + 10;
            }
            g.drawOval (randomx, randomy, 5, 5);
            randomx = (int) (Math.random () * 300) + 10;
            randomy = (int) (Math.random () * 150) + 10;
        }

        g.setColor (Color.white);
        g.drawLine (320, 0, 315, 40);
        g.drawLine (320, 0, 325, 40);
        g.drawLine (320, 120, 315, 80);
        g.drawLine (320, 120, 325, 80);
        g.drawLine (260, 60, 300, 55);
        g.drawLine (260, 60, 300, 65);
        g.drawLine (380, 60, 340, 55);
        g.drawLine (380, 60, 340, 65);
        fillGradOval (280, 20, 80, 80, new Color (254, 238, 44), new Color (255, 251, 191), g);

        g.setColor (new Color (255, 251, 191));
        fillGradOval (300, 40, 40, 40, new Color (255, 251, 191), new Color (254, 238, 44), g);
    }

    public void fillGradOval (int X, int Y, int H2, int W2, Color c1, Color c2, Graphics g)
    {
        g.setColor (c1);
        g.fillOval (X, Y, W2, H2);
        Color Gradient = c1;
        float red = (c2.getRed () - c1.getRed ()) / (W2 / 2);
        float blue = (c2.getBlue () - c1.getBlue ()) / (W2 / 2);
        float green = (c2.getGreen () - c1.getGreen ()) / (W2 / 2);

        int scale = 1;
        int r = c1.getRed ();
        int gr = c1.getGreen ();
        int b = c1.getBlue ();

        while (W2 > 10)
        {
            r = (int) (r + red);
            gr = (int) (gr + green);
            b = (int) (b + blue);

            Gradient = new Color (r, gr, b);
            g.setColor (Gradient);

            W2 = W2 - 2 * scale;
            H2 = H2 - 2 * scale;
            X = X + scale;
            Y = Y + scale;

            g.fillOval (X, Y, W2, H2);
        }
    }

    public void stateChanged (ChangeEvent e)
    {
        JSlider source = (JSlider) e.getSource ();
        if (!source.getValueIsAdjusting ())
        {
            int i = source.getValue();
            System.out.println(i);
            float[] hsb = Color.RGBtoHSB(bg.getRed(),bg.getGreen(),bg.getBlue(),null);
            int colorHue = Color.HSBtoRGB((float)i/100f, hsb[1], hsb[2]);
            Color c = new Color(colorHue);
            flow.setBackground(c);
            this.repaint();
        }
    }
}

【讨论】:

  • 我知道你是个艺术家也是 :)
  • @peeskillet 哦,不!该图像是在原始代码中生成的。我只改变了渐变底部颜色的色调。对于我的艺术努力,请参阅this answer(这让我想起了一个不是艺术大师的孩子的手指画)或this(好吧,这确实是一个数学公式......但我选择了颜色!) .
  • 请删除评论。请。我求求你了。
  • “我想将它用于我的项目” 继续。但是,如果您的意思是您打算将其用于家庭作业并假装所有的工作都是您的,那么不,这不会发生。你不应该那样使用 SO。
  • 一方面,OP 显然是该网站的新手,并且正在解决问题,因此期望他们拥有与这里的“老帽子”相同水平的专业知识是不合理的。另一方面,他们自己的错是没有提前考虑并且对发布他们可能不希望在公共场合看到的东西保持警惕。我怀疑 OP 很年轻,和大多数年轻人一样,完全不知道自己的隐私和将东西放到互联网上并且无法收回的概念。好吧,希望他们在这里吸取了教训。
猜你喜欢
  • 2018-10-30
  • 2011-08-03
  • 2012-01-19
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
相关资源
最近更新 更多