CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: I Need Java Help

    We could see what you want to do easier if you posted your code inside CODE tags so it is properly formatted. I doubt anyone will read it as it is.

    It would also help if you described what the code is supposed to do and what the problem is.

    Programming is an explanatory activity...
    R. Harper
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: I Need Java Help

    You've commented out 2 blocks of code and 2 individual lines so do you mean the problem is with all of these lines or just some of them?

  4. #4
    Join Date
    Jan 2009
    Posts
    4

    Re: I Need Java Help

    how do you make a code tag when I tried it didn't work so I'm obviously doing something wrong?

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: I Need Java Help

    Quote Originally Posted by holdenb View Post
    how do you make a code tag when I tried it didn't work so I'm obviously doing something wrong?
    Just read the posting instructions. Whatever happened to initiative?

    Mistakes are the portals of discovery...
    J. Joyce
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Jan 2009
    Posts
    4

    Re: I Need Java Help

    never mind I got it but thank you anyway!!

    here's what i was aiming for
    Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    public class ColorCalculator extends JFrame
    {
     private JPanel top;
     private Color chosenColor;
     private JTextField textField;
     private JPanel whiteCastle;
     private ColorPanel chosenColorPanel;
     private JSlider redSlider;
     private JSlider greenSlider;
     private JSlider blueSlider;
     private JLabel redLabel = new JLabel("RED: 127");
     private JLabel greenLabel = new JLabel("GREEN: 127");
     private JLabel blueLabel = new JLabel("BLUE: 127");
     public void go()
     {
      setSize(400,400);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      top.add(textField);
      JMenuBar mbar = new JMenuBar();
      setJMenuBar(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);
       }
      });
      
      JButton goButton = new JButton("Color Me Elmo");
      goButton.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e)
       {
         System.out.println("Hehe that tickles!!");
         updateFromTextbox();
       }
      });
      top.add(goButton);
      textField.setFont(new Font("Arial", Font.BOLD, 40));
      textField.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e)
       {
        updateFromTextbox();
       }
      });
      buildWhiteCastle();
      Container c = getContentPane();
      c.add(BorderLayout.NORTH, top);
      c.add(BorderLayout.CENTER, chosenColorPanel);
      c.add(BorderLayout.SOUTH, whiteCastle);
      setVisible(true);
     }
     public ColorCalculator()
     {
      super("Color Calculator");
      top = new JPanel(new GridLayout(1,2));
      chosenColor = new Color(0x7F7F7F);
      textField = new JTextField();
      whiteCastle = new JPanel(new GridLayout(3,2));
      chosenColorPanel = new ColorPanel();
      redSlider = new JSlider(0,0xFF);
      greenSlider = new JSlider(0,0xFF);
      blueSlider = new JSlider(0,0xFF);
     }
        public void repaintFrame()
        {
          repaint();
        }
     public void buildWhiteCastle()
     {
      blueSlider.setMajorTickSpacing(0x10);
      blueSlider.setPaintTicks(true);
      greenSlider.setMajorTickSpacing(0x10);
      greenSlider.setPaintTicks(true);
      redSlider.setMajorTickSpacing(0x10);
      redSlider.setPaintTicks(true);
      redSlider.addChangeListener( new ChangeListener(){
       public void stateChanged(ChangeEvent e)
       {
        updateFromSliders();
       }
      });
      blueSlider.addChangeListener( new ChangeListener(){
       public void stateChanged(ChangeEvent e)
       {
        updateFromSliders();
       }
      });
      greenSlider.addChangeListener( new ChangeListener(){
       public void stateChanged(ChangeEvent e)
       {
        updateFromSliders();
       }
      });
      whiteCastle.add(redLabel);
      whiteCastle.add(redSlider);
      whiteCastle.add(greenLabel);
      whiteCastle.add(greenSlider);
      whiteCastle.add(blueLabel);
      whiteCastle.add(blueSlider);
     }
     private void updateFromTextbox()
     {
    
      int chosenColorCode = 0;
      String hexString = textField.getText();
       try
       { 
        chosenColorCode = Integer.parseInt(hexString, 16);
       }
       catch(NumberFormatException ex)
       {
         System.out.println("This action is MORON proof, please try again later");
         chosenColorCode = 0x7f7f7f;
         textField.setText("7F7F7F");
       }
        Color newColor = new Color(chosenColorCode);
        chosenColorPanel.makePanelThisColor(newColor);
        greenSlider.setValue(newColor.getGreen());
        blueSlider.setValue(newColor.getBlue());
        redSlider.setValue(newColor.getRed());
        greenLabel.setText("GREEN: " + newColor.getGreen());
        blueLabel.setText("BLUE: " + newColor.getBlue());
        redLabel.setText("RED: " + newColor.getRed());
        textField.setText(Integer.toString(chosenColorCode, 16));
        chosenColor = newColor;
        repaintFrame();
     }
     private void updateFromSliders()
     {
      int red = redSlider.getValue();
      int green = greenSlider.getValue();
      int blue = blueSlider.getValue();
      greenLabel.setText("GREEN: " + green);
      blueLabel.setText("BLUE: " + blue);
      redLabel.setText("RED: " + red);
      textField.setText(Integer.toString(blue + 0x100*(green + 0x100*red), 16));
      chosenColor = new Color(red, green, blue);
      chosenColorPanel.makePanelThisColor(chosenColor);
      repaintFrame();
     }
     public static void main(String[] args)
     {
      ColorCalculator c = new ColorCalculator();
      c.go();
     }
        class ColorPanel extends JPanel
        {
            Color chosenColorOfThePanel;
            public ColorPanel()
            {
                chosenColorOfThePanel = chosenColor;
            }
            public void makePanelThisColor(Color _chosenColorOfThePanel)
            {
                chosenColorOfThePanel = _chosenColorOfThePanel;
            }
            public void paintComponent(Graphics g)
            {
                g.setColor(chosenColorOfThePanel);
                g.fillRect(0,0,getWidth(), getHeight());
            }
        }
    }

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