-1

Ok so i need help changing the hue of this slider. I cant seem to figure it out. Please no @override. I need something that will run on Ready to Program. The hue will change back to normal when the slider is back at 0. I dont need to get too complex. Just a simple Hue slider will be great. Thanks!

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 ())
        {

        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"Please no @override."* Your example already overrides methods. *"I need something that will run on Ready to Program."* What's that? – Andrew Thompson Mar 07 '14 at 05:19
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Don't mix Swing and AWT without good reason. So `Applet` -> `JApplet`. `Panel` -> `JPanel`.. – Andrew Thompson Mar 07 '14 at 05:22
  • Don't vandalize your own question and **do not vandalize my answer!** If you have a problem with my answer, take it up in a comment. If the question needs clarifying, feel free to edit it, but make it a sensible edit! – Andrew Thompson Mar 07 '14 at 23:52
  • @AndrewThompson If I had to guess, I'd say that the "Please no `@Override`" indicates that they have to use (or simply are using) a Java version <= 1.5, where `@Override` was not allowed at implemented interface methods. – Marco13 Mar 08 '14 at 00:28
  • 1
    Stop **stuffing about** with the edits! :-/ – Andrew Thompson Mar 08 '14 at 04:09
  • @peeskillet That's such a great idea, I'll probably be claiming it as my own in another 10 minutes. ;) – Andrew Thompson Mar 08 '14 at 04:22
  • can u please remove your comments? – user3386236 Mar 09 '14 at 03:29
  • 1
    I'm feeling benevolent and will give you a tip. 1) You were wrong to post your private code here. All code posted to SO should be public, and if you try to edit it out later, people will simply roll back your edits (like I have done now, multiple times). 2) But you ***can*** delete your question. People with high enough rep. can still see it, but it does not appear in searches of the site, the main question listing, or your profile. So if you really want, delete the question. Then my answer will also disappear. 3) But I don't like that, so don't expect me to help you in future. – Andrew Thompson Mar 12 '14 at 06:46

1 Answers1

5

I wasn't sure what 'color' you were referring to, so I made some guesses. Here is an hybrid application/applet (much easier for development and testing) that links the color of the bottom panel, as well as the bottom color of the gradient paint, to a hue as defined using the slider.

enter image description here enter image description here enter image description here

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();
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Did know you were an artist _too_ :) – Paul Samsotha Mar 08 '14 at 02:13
  • @peeskillet Oh no! That image was generated in the original code. I only altered the hue of the bottom color of the gradient. For my artistic efforts, see [this answer](http://stackoverflow.com/a/12216139/418556) (which reminds me of finger painting from a child that isn't a master of the art), or [this](http://stackoverflow.com/a/20799711/418556) (OK that's really a mathematical formula ..but I chose the colors!). – Andrew Thompson Mar 08 '14 at 02:34
  • please remove the comment. please. i beg you. – user3386236 Mar 09 '14 at 03:30
  • Your priorities confound me and your request makes zero sense. So no, I won't remove the comment. If you have a problem with my comment, flag it. (Hover the mouse over the left hand side of the comment and two icons appear, point at each one for an explanation.) – Andrew Thompson Mar 09 '14 at 04:11
  • Oh, and if by 'comment' you mean 'answer' (i.e. the same answer you attempted to vandalize twice yesterday), I would not consider removing it until you explained exactly why it should be removed. – Andrew Thompson Mar 09 '14 at 04:13
  • I would like to use it for my project. Please remove it – user3386236 Mar 09 '14 at 15:38
  • 2
    *"I would like to use it for my project"* Go right ahead. But if you mean you intend to use it for your homework and pretend all the work is yours, then no, that is not going to happen. You should not be using SO that way. – Andrew Thompson Mar 09 '14 at 20:36
  • i dont mind if you see this, please make it so the public cant see mine. – user3386236 Mar 13 '14 at 13:30
  • *"please make it so the public cant see mine."* I **can't** delete your post, but **you** can. There should be a group of links on the lower left, beneath the question body. One of them will be `delete` in a group like `share | edit | delete | flag`. – Andrew Thompson Mar 13 '14 at 14:00
  • I don't understand that comment, and have neither the time nor patience to try and figure it out. But I note that a moderator has locked the thread as of about 12 hours after I made the suggestion to delete it, so I suspect the matter is out of our hands now. Tough luck. – Andrew Thompson Mar 18 '14 at 09:05
  • does it still allow you to edit your answer? if so, people remove the top area of the code. i dont mind if the slider you made is still there. please. – user3386236 Mar 20 '14 at 02:34
  • 1
    On the one hand, the OP is obviously a newcomer to the site and is figuring things out, so it is unreasonable to expect them to have the same level of expertise as “old-hats” here. On the other hand, it’s their own fault for not thinking ahead and being wary of posting things they may not want seen in public. I suspect that the OP is young and like most youth, completely oblivious to their own privacy and the concept of putting things on the Internet and being unable to take them back. Well, hopefully they learned a lesson here. – Synetech May 07 '14 at 21:14