CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Exclamation I Need Java Help

    OK so I'm new to Java and I thought that I was doing this right but now I am completely lost and don't know what else I should do to fix it

    *Edit* Oops sorry about that I'm new to forums here I'll try again

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    public class ColorCalculator extends JFrame
    {
        private JPanel top;
        private Color color;
        private JTextArea textArea;
        private JPanel whiteCastle;
        private JSlider redSlider;
        private JSlider greenSlider;
        private JSlider blueSlider;
        public void go()
        {
            setSize(400,200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            top.add(textArea);
            top.add(new JButton("Change Color"));
            JMenuBar mbar = new JMenuBar;
            setMenuBar(mbar);
            JMenu file = new JMenu("file");
            mbar.add(file);
            JMenuItem quit = new JMenuItem("Quit");
            file.add(quit);
            quit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
              System.exit(0);
            }
            });
            buildWhiteCastle();
            Container c = getContentPane();
            c.add(BorderLayout.NORTH, top);
            c.add(BorderLayout.CENTER, new ColorPanel());
            c.add(BorderLayout.SOUTH, whiteCastle);
            setVisible(true);
        }
        public ColorCalculator()
        {
            super("Color Calculator");
            top = new JPanel(new GridLayout(1,2));
            color = new Color(0x7F7F7F);
            textArea = new JTextArea();
            whiteCastle = new JPanel(new GridLayout(3,2));
            redSlider = new JSlider(0,0xFF);
            greenSlider = new JSlider(0,0xFF);
            blueSlider = new JSlider(0,0xFF);
        }
        public void repaintFrame()
        {
            repaint();
        }
        public void buildWhiteCastle()
        {
            whiteCastle.add(new JLabel("RED"));
            whiteCastle.add(redSlider);
            whiteCastle.add(new JLabel("GREEN"));
            whiteCastle.add(greenSlider);
            whiteCastle.add(new JLabel("BLUE"));
            whiteCastle.add(blueSlider);
        }
        public Color mixColor(int red, int green, int blue)
        {
          return new Color(blue + 0x100*(green + 0x100*red));
        }
        public void update()
        {
            int colorCode = 0;
            String hexString = textField.getText();
                try
                {
                    colorCode = Integer.parseInt(hexString, 16);
                }
                catch(NumberFormatException ex)
                {
                    colorCode = 0x7f7f7f;
                    textField.setText("7F7F7F");
                }
                finally
                {
                    color = new Color(colorCode);
                    colorPanel.setPanelColor(color);
                    repaintFrame();
                }
        }
        public void updateFromSliders()
        {
          int red = redSlider.getValue(); 
          int green = greenSlider.getValue();
          int blue = blueSlider.getValue();
          textField.setText(Integer.toString(blue + 0x100*(green + 0x100*red)));
          color = new Color(red, green, blue);
          colorPanel.setPanelColor(color);
          repaintFrame();
        }
        public void updateFromTextBox()
        {
          
        }
        public static void main(String[] args)
        {
            ColorCalculator c = new ColorCalculator();
            c.go();
        }
       class ColorButton extends JButton implements ActionListener
        {
            Color buttonColor;
            public ColorButton(Color _buttonColor, String colorName)
            {
                super(colorName);
                buttonColor = _buttonColor;
                addActionListener(this);
            }
            public Color getColor()
            {
                return buttonColor;
            }
            public void actionPerformed(ActionEvent e)
            {
                color = buttonColor;
                repaintFrame();
            }
        }
        class ColorPanel extends JPanel
        {
            Color panelColor;
            public ColorPanel()
            {
                panelColor = color;
            }
            public void setPanelColor(Color _panelColor)
            {
                panelColor = _panelColor;
            }
            public void paintComponent(Graphics g)
            {
                g.setColor(panelColor);
                g.fillRect(0,0,getWidth(), getHeight());
            }
        }
    }
    I'm trying to make a 'Color Calculator' where
    A) It looks like this (I hope it comes out looking right)
    Code:
               --------------------------------------------------------------------
               |  Color Calculator                                                |
               -------------------------------------------------------------------|
               |  File    (menu bar with Quit)                                    |
               -------------------------------------------------------------------|
               |  JTextField                      | button (Change Color)         |
               -------------------------------------------------------------------|
               |         Field of color                                           |
               |                                                                  |
               |                                                                  |
               |                                                                  |
               |                                                                  |
               |                                                                  |
               |                                                                  |
               -------------------------------------------------------------------|
               |  red:       -----V-----------------------------------------------|
               |  green:     -----------------V---------------------------------- |
               |  blue:      --------------------------------------------V------  |
               -------------------------------------------------------------------|
    B)The JTextField should accept hex codes and when the user hits enter in the JTextField the color field should change the indicated color.

    C)If a hex code is present in the JText field and the button is hit, the color field should react.

    D)If a malformed hex code is entered, use exception handling and change the color field to 7F7F7F (middle grey). This should be the initial setting on your calculator.

    E)The JSliders on the bottom need to have 0-255 markings and should match the color field at all times.

    I know its a lot to ask but I really need help in seeing how someone would go about doing this so any input is great.
    Last edited by holdenb; January 27th, 2009 at 07:52 AM. Reason: I forgot code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured